| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2015, 2016 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "MoveGlobalVar.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "Try to move global var/struct/union declarations above all function \ |
| 27 | declarations except printf. Also move the declaration of printf to the top of \ |
| 28 | the reduce code if it exists and is not at the top of the code.\n"; |
| 29 | |
| 30 | static RegisterTransformation<MoveGlobalVar> |
| 31 | Trans("move-global-var", DescriptionMsg); |
| 32 | |
| 33 | void MoveGlobalVar::Initialize(ASTContext &context) |
| 34 | { |
| 35 | Transformation::Initialize(context); |
| 36 | } |
| 37 | |
| 38 | bool MoveGlobalVar::isSpecialDecl(const std::string &Name) |
| 39 | { |
| 40 | return ((Name == "__va_list_tag") || |
| 41 | (Name == "__builtin_va_list")); |
| 42 | } |
| 43 | |
| 44 | bool MoveGlobalVar::HandleTopLevelDecl(DeclGroupRef D) |
| 45 | { |
| 46 | if (TransformationManager::isCXXLangOpt()) { |
| 47 | ValidInstanceNum = 0; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | DeclGroupRef::iterator I = D.begin(); |
| 52 | TransAssert((I != D.end()) && "Bad DeclGroupRef!"); |
| 53 | |
| 54 | if (isInIncludedFile(*I)) |
| 55 | return true; |
| 56 | |
| 57 | const NamedDecl *ND = dyn_cast<NamedDecl>(*I); |
| 58 | if (!TheFirstDecl && ND && isSpecialDecl(ND->getNameAsString())) |
| 59 | return true; |
| 60 | |
| 61 | FunctionDecl *FD = dyn_cast<FunctionDecl>(*I); |
| 62 | if (FD) { |
| 63 | handleFunctionDecl(FD); |
| 64 | } |
| 65 | else { |
| 66 | handleOtherDecl(D); |
| 67 | } |
| 68 | |
| 69 | if (!TheFirstDecl) |
| 70 | TheFirstDecl = (*I); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void MoveGlobalVar::HandleTranslationUnit(ASTContext &Ctx) |
| 75 | { |
| 76 | if (QueryInstanceOnly) |
| 77 | return; |
| 78 | |
| 79 | if (TransformationCounter > ValidInstanceNum) { |
| 80 | TransError = TransMaxInstanceError; |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 85 | |
| 86 | if (ThePrintfDecl) |
| 87 | liftPrintfDecl(); |
| 88 | else |
| 89 | liftOtherDecl(); |
| 90 | |
| 91 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 92 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 93 | TransError = TransInternalError; |
| 94 | } |
| 95 | |
| 96 | void MoveGlobalVar::handleFunctionDecl(const FunctionDecl *FD) |
| 97 | { |
| 98 | if (FD->getNameAsString() == "printf") { |
| 99 | if (ThePrintfDecl || !TheFirstDecl || FD->isThisDeclarationADefinition()) |
| 100 | return; |
| 101 | |
| 102 | ValidInstanceNum++; |
| 103 | if (TransformationCounter == ValidInstanceNum) |
| 104 | ThePrintfDecl = FD; |
| 105 | } |
| 106 | else if (!TheFirstFunctionDecl) { |
| 107 | TheFirstFunctionDecl = FD; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void MoveGlobalVar::handleOtherDecl(DeclGroupRef DGR) |
| 112 | { |
| 113 | if (!TheFirstFunctionDecl) |
| 114 | return; |
| 115 | |
| 116 | ValidInstanceNum++; |
| 117 | if (TransformationCounter == ValidInstanceNum) |
| 118 | TheDGRPointer = DGR.getAsOpaquePtr(); |
| 119 | } |
| 120 | |
| 121 | void MoveGlobalVar::liftPrintfDecl(void) |
| 122 | { |
| 123 | TransAssert(ThePrintfDecl && TheFirstDecl && |
| 124 | (ThePrintfDecl != TheFirstDecl) && "Invalid printf decl!"); |
| 125 | |
| 126 | std::string PrintfDeclStr; |
| 127 | RewriteHelper->getFunctionDeclStrAndRemove(ThePrintfDecl, PrintfDeclStr); |
| 128 | |
| 129 | SourceRange DeclRange = TheFirstDecl->getSourceRange(); |
| 130 | SourceLocation StartLoc = DeclRange.getBegin(); |
| 131 | PrintfDeclStr += ";\n"; |
| 132 | TheRewriter.InsertTextBefore(StartLoc, PrintfDeclStr); |
| 133 | } |
| 134 | |
| 135 | void MoveGlobalVar::liftOtherDecl(void) |
| 136 | { |
| 137 | TransAssert(TheDGRPointer && "NULL DGR pointer!"); |
| 138 | TransAssert(TheFirstFunctionDecl && "NULL First Decl!"); |
| 139 | DeclGroupRef DGR = DeclGroupRef::getFromOpaquePtr(TheDGRPointer); |
| 140 | |
| 141 | std::string DGRStr; |
| 142 | RewriteHelper->getEntireDeclGroupStrAndRemove(DGR, DGRStr); |
| 143 | |
| 144 | SourceRange DeclRange = TheFirstFunctionDecl->getSourceRange(); |
| 145 | SourceLocation StartLoc = DeclRange.getBegin(); |
| 146 | DGRStr += ";\n"; |
| 147 | TheRewriter.InsertTextBefore(StartLoc, DGRStr); |
| 148 | } |
| 149 | |
| 150 | MoveGlobalVar::~MoveGlobalVar(void) |
| 151 | { |
| 152 | // Nothing to do |
| 153 | } |
| 154 |