| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017 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 "CombineGlobalVarDecl.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | |
| 23 | #include "TransformationManager.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static const char *DescriptionMsg = |
| 28 | "Combine global variable declarations with the same type. \ |
| 29 | Each iteration only does one combination, i.e., \ |
| 30 | for the code below: \n\ |
| 31 | int a, b; \n\ |
| 32 | int c; \n\ |
| 33 | int d; \n\ |
| 34 | We will need to invoke this transformation twice \ |
| 35 | to achieve a complete combination. \ |
| 36 | This pass only combines declarations with exactly \ |
| 37 | the same type, e.g., it won't combine int *x and int y,\ |
| 38 | although it's valid we can have int *x, y in a DeclGroup. \ |
| 39 | Note that this transformation always tries to combine \ |
| 40 | the rest of declarations with the very first one, \ |
| 41 | so it is an unsound transformation and could result \ |
| 42 | in compilation failures. \n"; |
| 43 | |
| 44 | static RegisterTransformation<CombineGlobalVarDecl> |
| 45 | Trans("combine-global-var", DescriptionMsg); |
| 46 | |
| 47 | void CombineGlobalVarDecl::Initialize(ASTContext &context) |
| 48 | { |
| 49 | Transformation::Initialize(context); |
| 50 | } |
| 51 | |
| 52 | bool CombineGlobalVarDecl::HandleTopLevelDecl(DeclGroupRef DGR) |
| 53 | { |
| 54 | DeclGroupRef::iterator DI = DGR.begin(); |
| 55 | VarDecl *VD = dyn_cast<VarDecl>(*DI); |
| 56 | if (!VD || isInIncludedFile(VD)) |
| 57 | return true; |
| 58 | SourceRange Range = VD->getSourceRange(); |
| 59 | if (Range.getBegin().isInvalid() || Range.getEnd().isInvalid()) |
| 60 | return true; |
| 61 | |
| 62 | const Type *T = VD->getType().getTypePtr(); |
| 63 | const Type *CanonicalT = Context->getCanonicalType(T); |
| 64 | |
| 65 | DeclGroupVector *DV; |
| 66 | TypeToDeclMap::iterator TI = AllDeclGroups.find(CanonicalT); |
| 67 | if (TI == AllDeclGroups.end()) { |
| 68 | DV = new DeclGroupVector(); |
| 69 | AllDeclGroups[CanonicalT] = DV; |
| 70 | } |
| 71 | else { |
| 72 | ValidInstanceNum++; |
| 73 | DV = (*TI).second; |
| 74 | |
| 75 | if (ValidInstanceNum == TransformationCounter) { |
| 76 | if (DV->size() >= 1) { |
| 77 | void* DP1 = *(DV->begin()); |
| 78 | TheDeclGroupRefs.push_back(DP1); |
| 79 | TheDeclGroupRefs.push_back(DGR.getAsOpaquePtr()); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Note that it's unnecessary to keep all encountered |
| 85 | // DeclGroupRefs. We could choose a light way similar |
| 86 | // to what we implemented in CombineLocalVarDecl. |
| 87 | // I kept the code here because I feel we probably |
| 88 | // need more combinations, i.e., not only combine the |
| 89 | // first DeclGroup with others, but we could combine |
| 90 | // the second one and the third one. |
| 91 | DV->push_back(DGR.getAsOpaquePtr()); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | void CombineGlobalVarDecl::HandleTranslationUnit(ASTContext &Ctx) |
| 96 | { |
| 97 | if (QueryInstanceOnly) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (TransformationCounter > ValidInstanceNum) { |
| 102 | TransError = TransMaxInstanceError; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 107 | |
| 108 | doCombination(); |
| 109 | |
| 110 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 111 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 112 | TransError = TransInternalError; |
| 113 | } |
| 114 | |
| 115 | void CombineGlobalVarDecl::doCombination(void) |
| 116 | { |
| 117 | TransAssert(TheDeclGroupRefs.size() == 2); |
| 118 | void *P2 = TheDeclGroupRefs.pop_back_val(); |
| 119 | void *P1 = TheDeclGroupRefs.pop_back_val(); |
| 120 | |
| 121 | DeclGroupRef FirstDGR = DeclGroupRef::getFromOpaquePtr(P1); |
| 122 | DeclGroupRef SecondDGR = DeclGroupRef::getFromOpaquePtr(P2); |
| 123 | |
| 124 | SourceLocation EndLoc = |
| 125 | RewriteHelper->getDeclGroupRefEndLoc(FirstDGR); |
| 126 | |
| 127 | std::string DStr; |
| 128 | RewriteHelper->getDeclGroupStrAndRemove(SecondDGR, DStr); |
| 129 | TheRewriter.InsertText(EndLoc, ", " + DStr, /*InsertAfter=*/false); |
| 130 | } |
| 131 | |
| 132 | CombineGlobalVarDecl::~CombineGlobalVarDecl(void) |
| 133 | { |
| 134 | for(TypeToDeclMap::iterator I = AllDeclGroups.begin(), |
| 135 | E = AllDeclGroups.end(); I != E; ++I) { |
| 136 | delete (*I).second; |
| 137 | } |
| 138 | } |
| 139 |