Debian Codegraph @

clang_delta/CallExprToValue.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2015, 2017, 2019 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 "CallExprToValue.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 "Replace a call expression with a value or variable which \
27 has the same type as CallExpr's type. If CallExpr is type \
28 of integer/pointer, it will be replaced with 0. If it has \
29 type of union/struct, it will be replaced with a newly created \
30 global variable with a correct type. \n";
31
32 static RegisterTransformation<CallExprToValue>
33 Trans("callexpr-to-value", DescriptionMsg);
34
35 class CallExprToValueVisitor : public
36 RecursiveASTVisitor<CallExprToValueVisitor> {
37
38 public:
39
40 explicit CallExprToValueVisitor(CallExprToValue *Instance)
41 : ConsumerInstance(Instance),
42 CurrentFD(NULL)
43 { }
44
45 bool VisitCallExpr(CallExpr *CE);
46
47 bool VisitFunctionDecl(FunctionDecl *FD);
48
49 private:
50
51 CallExprToValue *ConsumerInstance;
52
53 const FunctionDecl *CurrentFD;
54 };
55
56 bool CallExprToValueVisitor::VisitCallExpr(CallExpr *CE)
57 {
58 if (ConsumerInstance->isInIncludedFile(CE))
59 return true;
60
61 ConsumerInstance->ValidInstanceNum++;
62 if (ConsumerInstance->TransformationCounter !=
63 ConsumerInstance->ValidInstanceNum)
64 return true;
65
66 ConsumerInstance->TheCallExpr = CE;
67 ConsumerInstance->CurrentFD = CurrentFD;
68 return true;
69 }
70
71 bool CallExprToValueVisitor::VisitFunctionDecl(FunctionDecl *FD)
72 {
73 // Note that CurrentFD could not be the function decl where TheCallExpr
74 // shows up, e.g., we could have:
75 // struct A {
76 // void foo();
77 // static int value = bar();
78 // };
79 CurrentFD = FD;
80 return true;
81 }
82
83 void CallExprToValue::Initialize(ASTContext &context)
84 {
85 Transformation::Initialize(context);
86 CollectionVisitor = new CallExprToValueVisitor(this);
87 NameQueryWrap =
88 new TransNameQueryWrap(RewriteHelper->getTmpVarNamePrefix());
89 }
90
91 void CallExprToValue::HandleTranslationUnit(ASTContext &Ctx)
92 {
93 CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
94
95 if (QueryInstanceOnly)
96 return;
97
98 if (TransformationCounter > ValidInstanceNum) {
99 TransError = TransMaxInstanceError;
100 return;
101 }
102
103 TransAssert(TheCallExpr && "NULL TheCallExpr!");
104
105 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
106
107 NameQueryWrap->TraverseDecl(Ctx.getTranslationUnitDecl());
108 NamePostfix = NameQueryWrap->getMaxNamePostfix() + 1;
109
110 replaceCallExpr();
111
112 if (Ctx.getDiagnostics().hasErrorOccurred() ||
113 Ctx.getDiagnostics().hasFatalErrorOccurred())
114 TransError = TransInternalError;
115 }
116
117 void CallExprToValue::replaceCallExpr(void)
118 {
119 std::string CommaStr = "";
120
121 QualType RVQualType = TheCallExpr->getType();
122 const Type *RVType = RVQualType.getTypePtr();
123 if (RVType->isVoidType()) {
124 // Nothing to do
125 }
126 else if (RVType->isUnionType() || RVType->isStructureType()) {
127 std::string RVStr("");
128 RewriteHelper->getTmpTransName(NamePostfix, RVStr);
129 NamePostfix++;
130
131 CommaStr = RVStr;
132 RVQualType.getAsStringInternal(RVStr, Context->getPrintingPolicy());
133 RVStr += ";\n";
134 if (CurrentFD) {
135 RewriteHelper->insertStringBeforeFunc(CurrentFD, RVStr);
136 }
137 else {
138 TheRewriter.InsertTextBefore(TheCallExpr->getBeginLoc(), RVStr);
139 }
140 }
141 else {
142 CommaStr = "0";
143 }
144
145 RewriteHelper->replaceExpr(TheCallExpr, CommaStr);
146 }
147
148 CallExprToValue::~CallExprToValue(void)
149 {
150 delete CollectionVisitor;
151 delete NameQueryWrap;
152 }
153