Debian Codegraph @

clang_delta/MoveFunctionBody.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2015 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 "MoveFunctionBody.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 "Move function body towards its declaration. \
27 Note that this pass would generate incompilable code. \n";
28
29 static RegisterTransformation<MoveFunctionBody>
30 Trans("move-function-body", DescriptionMsg);
31
32 void MoveFunctionBody::Initialize(ASTContext &context)
33 {
34 Transformation::Initialize(context);
35 }
36
37 bool MoveFunctionBody::HandleTopLevelDecl(DeclGroupRef D)
38 {
39 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
40 FunctionDecl *FD = dyn_cast<FunctionDecl>(*I);
41 if (!FD || isInIncludedFile(FD)) {
42 PrevFunctionDecl = NULL;
43 continue;
44 }
45
46 FunctionDecl *CanonicalFD = FD->getCanonicalDecl();
47 if (FD->isThisDeclarationADefinition()) {
48 FunctionDecl *FDDecl = AllValidFunctionDecls[CanonicalFD];
49 if (!FDDecl) {
50 PrevFunctionDecl = NULL;
51 continue;
52 }
53
54 // Declaration and Definition are next to each other
55 if (PrevFunctionDecl) {
56 FunctionDecl *CanonicalPrevFD = PrevFunctionDecl->getCanonicalDecl();
57 if (CanonicalFD == CanonicalPrevFD) {
58 PrevFunctionDecl = NULL;
59 continue;
60 }
61 }
62
63 FuncDeclToFuncDef[FDDecl] = FD;
64 }
65
66 PrevFunctionDecl = FD;
67 // We only need the first FunctionDecl
68 if (AllValidFunctionDecls[CanonicalFD])
69 continue;
70
71 AllValidFunctionDecls[CanonicalFD] = FD;
72 }
73 return true;
74 }
75
76 void MoveFunctionBody::HandleTranslationUnit(ASTContext &Ctx)
77 {
78 doAnalysis();
79
80 if (QueryInstanceOnly)
81 return;
82
83 if (TransformationCounter > ValidInstanceNum) {
84 TransError = TransMaxInstanceError;
85 return;
86 }
87
88 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
89
90 TransAssert(TheFunctionDecl && "NULL TheFunctionDecl!");
91 TransAssert(!TheFunctionDecl->isThisDeclarationADefinition() &&
92 "Invalid Function Declaration!");
93 TransAssert(TheFunctionDef && "NULL TheFunctionDef!");
94 TransAssert(TheFunctionDef->isThisDeclarationADefinition() &&
95 "Invalid Function Definition!");
96
97 doRewriting();
98
99 if (Ctx.getDiagnostics().hasErrorOccurred() ||
100 Ctx.getDiagnostics().hasFatalErrorOccurred())
101 TransError = TransInternalError;
102 }
103
104 void MoveFunctionBody::doAnalysis(void)
105 {
106 for (FuncDeclToFuncDeclMap::iterator I = FuncDeclToFuncDef.begin(),
107 E = FuncDeclToFuncDef.end(); I != E; ++I) {
108 ValidInstanceNum++;
109
110 if (ValidInstanceNum == TransformationCounter) {
111 TheFunctionDecl = (*I).first;
112 TheFunctionDef = (*I).second;
113 }
114 }
115 }
116
117 void MoveFunctionBody::doRewriting(void)
118 {
119 std::string FuncDefStr;
120 RewriteHelper->getFunctionDefStrAndRemove(TheFunctionDef, FuncDefStr);
121 RewriteHelper->addStringAfterFuncDecl(TheFunctionDecl, FuncDefStr);
122 }
123
124 MoveFunctionBody::~MoveFunctionBody(void)
125 {
126 // Nothing to do
127 }
128