Skip to content

Commit bda375b

Browse files
authored
Merge pull request #257 from plum-umd/RefactorGatherTool
Refactoring GatherTool.cpp
2 parents 1ce7520 + 822cf1d commit bda375b

10 files changed

+47
-148
lines changed

clang/include/clang/CConv/CastPlacement.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class CastPlacementVisitor : public RecursiveASTVisitor<CastPlacementVisitor> {
2626
Rewriter& Writer;
2727
ConstraintResolver CR;
2828

29-
bool needCasting(ConstraintVariable*, ConstraintVariable*, IsChecked);
30-
std::string getCastString(ConstraintVariable *Src, ConstraintVariable *Dst,
31-
IsChecked Dinfo) ;
29+
bool needCasting(ConstraintVariable*, ConstraintVariable*);
30+
std::string getCastString(ConstraintVariable *Src, ConstraintVariable *Dst);
3231
void surroundByCast(const std::string&, Expr*);
3332
};
3433
#endif // _CASTPLACEMENT_H

clang/include/clang/CConv/GatherTypes.h

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1-
//=--GatherTool.h-------------------------------------------------*- C++-*-===//
1+
//=--IntermediateToolHook.h---------------------------------------*- C++-*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
// This class is used to gather arguments of functions which are WILD so that
9-
// explicit cast could be inserted when checked pointers are used as parameters
10-
// for corresponding calls
8+
// This class provides an intermediate hook for any visitors that need to be
9+
// run after constraint solving but before rewriting, such as trying out
10+
// heuristics in the case of array bounds inference.
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef _GATHERTOOL_H_
14-
#define _GATHERTOOL_H_
13+
#ifndef _INTERMEDIATETOOLHOOK_H
14+
#define _INTERMEDIATETOOLHOOK_H
1515

1616
#include "clang/AST/Decl.h"
1717
#include "clang/AST/Stmt.h"
1818
#include "clang/AST/ASTContext.h"
1919
#include "clang/Rewrite/Core/Rewriter.h"
2020

21-
#include "GatherTypes.h"
2221
#include "ProgramInfo.h"
2322

2423
using namespace clang;
2524

2625

27-
class ArgGatherer : public ASTConsumer {
26+
class IntermediateToolHook : public ASTConsumer {
2827
public:
29-
explicit ArgGatherer(ProgramInfo &I, std::string &OPostfix)
30-
: Info(I), OutputPostfix(OPostfix) {}
28+
explicit IntermediateToolHook(ProgramInfo &I, clang::ASTContext *C) : Info(I) { }
3129
virtual void HandleTranslationUnit(ASTContext &Context);
32-
ParameterMap getMF();
3330

3431
private:
3532
ProgramInfo &Info;
36-
std::string &OutputPostfix;
37-
ParameterMap MF;
3833
};
3934

40-
#endif // _GATHERTOOL_H_
35+
#endif // _INTERMEDIATETOOLHOOK_H

clang/include/clang/CConv/ProgramInfo.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "Utils.h"
2323
#include "PersistentSourceLoc.h"
2424
#include "CConvInteractiveData.h"
25-
#include "GatherTypes.h"
2625

2726

2827
class ProgramVariableAdder {
@@ -91,10 +90,6 @@ class ProgramInfo : public ProgramVariableAdder {
9190
Constraints &getConstraints() { return CS; }
9291
AVarBoundsInfo &getABoundsInfo() { return ArrBInfo; }
9392

94-
// Parameter map is used for cast insertion, post-rewriting
95-
void merge_MF(const ParameterMap &MF);
96-
ParameterMap &getMF();
97-
9893
ConstraintsInfo &getInterimConstraintState() {
9994
return CState;
10095
}
@@ -137,7 +132,6 @@ class ProgramInfo : public ProgramVariableAdder {
137132
StaticFunctionMapType StaticFunctionFVCons;
138133
std::map<std::string, std::set<PVConstraint *>> GlobalVariableSymbols;
139134

140-
ParameterMap MF;
141135
// Object that contains all the bounds information of various
142136
// array variables.
143137
AVarBoundsInfo ArrBInfo;

clang/lib/CConv/CConv.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "clang/CConv/CConv.h"
1414
#include "clang/CConv/ConstraintBuilder.h"
15-
#include "clang/CConv/GatherTool.h"
15+
#include "clang/CConv/IntermediateToolHook.h"
1616
#include "clang/CConv/RewriteUtils.h"
1717
#include "clang/Tooling/ArgumentsAdjusters.h"
1818

@@ -286,13 +286,14 @@ bool CConvInterface::SolveConstraints(bool ComputeInterimState) {
286286
GlobalProgramInfo.getABoundsInfo().performFlowAnalysis(&GlobalProgramInfo);
287287
}
288288

289-
// 3. Gather pre-rewrite data.
289+
// 3. Run intermediate tool hook to run visitors that need to be executed
290+
// after constraint solving but before rewriting.
290291
ClangTool &Tool = getGlobalClangTool();
291-
std::unique_ptr<ToolAction> GatherTool =
292+
std::unique_ptr<ToolAction> IMTool =
292293
newFrontendActionFactoryA
293-
<RewriteAction<ArgGatherer, ProgramInfo>>(GlobalProgramInfo);
294-
if (GatherTool)
295-
Tool.run(GatherTool.get());
294+
<GenericAction<IntermediateToolHook, ProgramInfo>>(GlobalProgramInfo);
295+
if (IMTool)
296+
Tool.run(IMTool.get());
296297
else
297298
llvm_unreachable("No Action");
298299

clang/lib/CConv/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set( LLVM_LINK_COMPONENTS
1919
ConstraintsGraph.cpp
2020
ConstraintVariables.cpp
2121
DeclRewriter.cpp
22-
GatherTool.cpp
22+
IntermediateToolHook.cpp
2323
MappingVisitor.cpp
2424
PersistentSourceLoc.cpp
2525
ProgramInfo.cpp

clang/lib/CConv/CastPlacement.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
3333
ProgramInfo::CallTypeParamBindingsT TypeVars;
3434
if (Info.hasTypeParamBindings(CE, Context))
3535
TypeVars = Info.getTypeParamBindings(CE, Context);
36-
auto PInfo = Info.getMF()[Fname];
3736
unsigned PIdx = 0;
3837
for (const auto &A : CE->arguments()) {
3938
if (PIdx < FD->getNumParams()) {
@@ -50,11 +49,10 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
5049
CVarSet ArgumentConstraints = CR.getExprConstraintVars(ArgExpr);
5150
ConstraintVariable *ParameterC = FV->getParamVar(PIdx);
5251
for (auto *ArgumentC : ArgumentConstraints) {
53-
auto Dinfo = PIdx < PInfo.size() ? PInfo[PIdx] : CHECKED;
54-
if (needCasting(ArgumentC, ParameterC, Dinfo)) {
52+
if (needCasting(ArgumentC, ParameterC)) {
5553
// We expect the cast string to end with "(".
5654
std::string CastString =
57-
getCastString(ArgumentC, ParameterC, Dinfo);
55+
getCastString(ArgumentC, ParameterC);
5856
surroundByCast(CastString, A);
5957
break;
6058
}
@@ -71,8 +69,7 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
7169
// Check whether an explicit casting is needed when the pointer represented
7270
// by src variable is assigned to dst.
7371
bool CastPlacementVisitor::needCasting(ConstraintVariable *Src,
74-
ConstraintVariable *Dst,
75-
IsChecked Dinfo) {
72+
ConstraintVariable *Dst) {
7673
auto &E = Info.getConstraints().getVariables();
7774
// Check if the src is a checked type.
7875
if (Src->isChecked(E)) {
@@ -84,17 +81,16 @@ bool CastPlacementVisitor::needCasting(ConstraintVariable *Src,
8481
// Is Dst Wild?
8582
// TODO: The Dinfo == WILD comparison seems to be the cause of a cast
8683
// insertion bug. Can it be removed?
87-
if (!Dst->isChecked(E) || Dinfo == WILD)
84+
if (!Dst->isChecked(E))
8885
return true;
8986
}
9087
return false;
9188
}
9289

9390
// Get the type name to insert for casting.
9491
std::string CastPlacementVisitor::getCastString(ConstraintVariable *Src,
95-
ConstraintVariable *Dst,
96-
IsChecked Dinfo) {
97-
assert(needCasting(Src, Dst, Dinfo) && "No casting needed.");
92+
ConstraintVariable *Dst) {
93+
assert(needCasting(Src, Dst) && "No casting needed.");
9894
return "(" + Dst->getRewritableOriginalTy() + ")";
9995
}
10096

clang/lib/CConv/GatherTool.cpp

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//=--IntermediateToolHook.cpp-------------------------------------*- C++-*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Implementation of methods in IntermediateToolHook.h
9+
//===----------------------------------------------------------------------===//
10+
11+
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/CConv/ArrayBoundsInferenceConsumer.h"
13+
#include "clang/CConv/IntermediateToolHook.h"
14+
15+
using namespace llvm;
16+
using namespace clang;
17+
18+
void IntermediateToolHook::HandleTranslationUnit(ASTContext &Context) {
19+
Info.enterCompilationUnit(Context);
20+
HandleArrayVariablesBoundsDetection(&Context, Info);
21+
Info.exitCompilationUnit();
22+
}

clang/lib/CConv/ProgramInfo.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ ProgramInfo::ProgramInfo() :
2424
StaticFunctionFVCons.clear();
2525
}
2626

27-
28-
void ProgramInfo::merge_MF(const ParameterMap &OtherMF) {
29-
for (auto KV : OtherMF)
30-
MF[KV.first] = KV.second;
31-
}
32-
33-
ParameterMap &ProgramInfo::getMF() {
34-
return MF;
35-
}
36-
3727
void dumpExtFuncMap(const ProgramInfo::ExternalFunctionMapType &EMap,
3828
raw_ostream &O) {
3929
for (const auto &DefM : EMap) {

0 commit comments

Comments
 (0)