Skip to content

Refactoring GatherTool.cpp #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clang/include/clang/CConv/CastPlacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ class CastPlacementVisitor : public RecursiveASTVisitor<CastPlacementVisitor> {
Rewriter& Writer;
ConstraintResolver CR;

bool needCasting(ConstraintVariable*, ConstraintVariable*, IsChecked);
std::string getCastString(ConstraintVariable *Src, ConstraintVariable *Dst,
IsChecked Dinfo) ;
bool needCasting(ConstraintVariable*, ConstraintVariable*);
std::string getCastString(ConstraintVariable *Src, ConstraintVariable *Dst);
void surroundByCast(const std::string&, Expr*);
};
#endif // _CASTPLACEMENT_H
18 changes: 0 additions & 18 deletions clang/include/clang/CConv/GatherTypes.h

This file was deleted.

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

#ifndef _GATHERTOOL_H_
#define _GATHERTOOL_H_
#ifndef _INTERMEDIATETOOLHOOK_H
#define _INTERMEDIATETOOLHOOK_H

#include "clang/AST/Decl.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/ASTContext.h"
#include "clang/Rewrite/Core/Rewriter.h"

#include "GatherTypes.h"
#include "ProgramInfo.h"

using namespace clang;


class ArgGatherer : public ASTConsumer {
class IntermediateToolHook : public ASTConsumer {
public:
explicit ArgGatherer(ProgramInfo &I, std::string &OPostfix)
: Info(I), OutputPostfix(OPostfix) {}
explicit IntermediateToolHook(ProgramInfo &I, clang::ASTContext *C) : Info(I) { }
virtual void HandleTranslationUnit(ASTContext &Context);
ParameterMap getMF();

private:
ProgramInfo &Info;
std::string &OutputPostfix;
ParameterMap MF;
};

#endif // _GATHERTOOL_H_
#endif // _INTERMEDIATETOOLHOOK_H
6 changes: 0 additions & 6 deletions clang/include/clang/CConv/ProgramInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "Utils.h"
#include "PersistentSourceLoc.h"
#include "CConvInteractiveData.h"
#include "GatherTypes.h"


class ProgramVariableAdder {
Expand Down Expand Up @@ -91,10 +90,6 @@ class ProgramInfo : public ProgramVariableAdder {
Constraints &getConstraints() { return CS; }
AVarBoundsInfo &getABoundsInfo() { return ArrBInfo; }

// Parameter map is used for cast insertion, post-rewriting
void merge_MF(const ParameterMap &MF);
ParameterMap &getMF();

ConstraintsInfo &getInterimConstraintState() {
return CState;
}
Expand Down Expand Up @@ -137,7 +132,6 @@ class ProgramInfo : public ProgramVariableAdder {
StaticFunctionMapType StaticFunctionFVCons;
std::map<std::string, std::set<PVConstraint *>> GlobalVariableSymbols;

ParameterMap MF;
// Object that contains all the bounds information of various
// array variables.
AVarBoundsInfo ArrBInfo;
Expand Down
13 changes: 7 additions & 6 deletions clang/lib/CConv/CConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "clang/CConv/CConv.h"
#include "clang/CConv/ConstraintBuilder.h"
#include "clang/CConv/GatherTool.h"
#include "clang/CConv/IntermediateToolHook.h"
#include "clang/CConv/RewriteUtils.h"
#include "clang/Tooling/ArgumentsAdjusters.h"

Expand Down Expand Up @@ -281,13 +281,14 @@ bool CConvInterface::SolveConstraints(bool ComputeInterimState) {
GlobalProgramInfo.getABoundsInfo().performFlowAnalysis(&GlobalProgramInfo);
}

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

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CConv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set( LLVM_LINK_COMPONENTS
ConstraintsGraph.cpp
ConstraintVariables.cpp
DeclRewriter.cpp
GatherTool.cpp
IntermediateToolHook.cpp
MappingVisitor.cpp
PersistentSourceLoc.cpp
ProgramInfo.cpp
Expand Down
16 changes: 6 additions & 10 deletions clang/lib/CConv/CastPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
ProgramInfo::CallTypeParamBindingsT TypeVars;
if (Info.hasTypeParamBindings(CE, Context))
TypeVars = Info.getTypeParamBindings(CE, Context);
auto PInfo = Info.getMF()[Fname];
unsigned PIdx = 0;
for (const auto &A : CE->arguments()) {
if (PIdx < FD->getNumParams()) {
Expand All @@ -50,11 +49,10 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
CVarSet ArgumentConstraints = CR.getExprConstraintVars(ArgExpr);
ConstraintVariable *ParameterC = FV->getParamVar(PIdx);
for (auto *ArgumentC : ArgumentConstraints) {
auto Dinfo = PIdx < PInfo.size() ? PInfo[PIdx] : CHECKED;
if (needCasting(ArgumentC, ParameterC, Dinfo)) {
if (needCasting(ArgumentC, ParameterC)) {
// We expect the cast string to end with "(".
std::string CastString =
getCastString(ArgumentC, ParameterC, Dinfo);
getCastString(ArgumentC, ParameterC);
surroundByCast(CastString, A);
break;
}
Expand All @@ -71,8 +69,7 @@ bool CastPlacementVisitor::VisitCallExpr(CallExpr *CE) {
// Check whether an explicit casting is needed when the pointer represented
// by src variable is assigned to dst.
bool CastPlacementVisitor::needCasting(ConstraintVariable *Src,
ConstraintVariable *Dst,
IsChecked Dinfo) {
ConstraintVariable *Dst) {
auto &E = Info.getConstraints().getVariables();
// Check if the src is a checked type.
if (Src->isChecked(E)) {
Expand All @@ -84,17 +81,16 @@ bool CastPlacementVisitor::needCasting(ConstraintVariable *Src,
// Is Dst Wild?
// TODO: The Dinfo == WILD comparison seems to be the cause of a cast
// insertion bug. Can it be removed?
if (!Dst->isChecked(E) || Dinfo == WILD)
if (!Dst->isChecked(E))
return true;
}
return false;
}

// Get the type name to insert for casting.
std::string CastPlacementVisitor::getCastString(ConstraintVariable *Src,
ConstraintVariable *Dst,
IsChecked Dinfo) {
assert(needCasting(Src, Dst, Dinfo) && "No casting needed.");
ConstraintVariable *Dst) {
assert(needCasting(Src, Dst) && "No casting needed.");
return "(" + Dst->getRewritableOriginalTy() + ")";
}

Expand Down
80 changes: 0 additions & 80 deletions clang/lib/CConv/GatherTool.cpp

This file was deleted.

22 changes: 22 additions & 0 deletions clang/lib/CConv/IntermediateToolHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//=--IntermediateToolHook.cpp-------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Implementation of methods in IntermediateToolHook.h
//===----------------------------------------------------------------------===//

#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/CConv/ArrayBoundsInferenceConsumer.h"
#include "clang/CConv/IntermediateToolHook.h"

using namespace llvm;
using namespace clang;

void IntermediateToolHook::HandleTranslationUnit(ASTContext &Context) {
Info.enterCompilationUnit(Context);
HandleArrayVariablesBoundsDetection(&Context, Info);
Info.exitCompilationUnit();
}
10 changes: 0 additions & 10 deletions clang/lib/CConv/ProgramInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ ProgramInfo::ProgramInfo() :
StaticFunctionFVCons.clear();
}


void ProgramInfo::merge_MF(const ParameterMap &OtherMF) {
for (auto KV : OtherMF)
MF[KV.first] = KV.second;
}

ParameterMap &ProgramInfo::getMF() {
return MF;
}

void dumpExtFuncMap(const ProgramInfo::ExternalFunctionMapType &EMap,
raw_ostream &O) {
for (const auto &DefM : EMap) {
Expand Down