diff --git a/clang/include/clang/CConv/CastPlacement.h b/clang/include/clang/CConv/CastPlacement.h index c803974ade67..855d235eb929 100644 --- a/clang/include/clang/CConv/CastPlacement.h +++ b/clang/include/clang/CConv/CastPlacement.h @@ -26,9 +26,8 @@ class CastPlacementVisitor : public RecursiveASTVisitor { 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 \ No newline at end of file diff --git a/clang/include/clang/CConv/GatherTypes.h b/clang/include/clang/CConv/GatherTypes.h deleted file mode 100644 index 3c52445b87ed..000000000000 --- a/clang/include/clang/CConv/GatherTypes.h +++ /dev/null @@ -1,18 +0,0 @@ -//=--GatherTypes.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 -// -//===----------------------------------------------------------------------===// -// Types used in GatherTool -//===----------------------------------------------------------------------===// - -#ifndef __GATHERTYPES_H_ -#define __GATHERTYPES_H_ - -typedef enum { CHECKED, WILD } IsChecked; - -typedef std::map> ParameterMap; - -#endif diff --git a/clang/include/clang/CConv/GatherTool.h b/clang/include/clang/CConv/IntermediateToolHook.h similarity index 54% rename from clang/include/clang/CConv/GatherTool.h rename to clang/include/clang/CConv/IntermediateToolHook.h index b6351ef86680..09eb20c44172 100644 --- a/clang/include/clang/CConv/GatherTool.h +++ b/clang/include/clang/CConv/IntermediateToolHook.h @@ -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 diff --git a/clang/include/clang/CConv/ProgramInfo.h b/clang/include/clang/CConv/ProgramInfo.h index 3637d5f1ecc4..8987f3647182 100644 --- a/clang/include/clang/CConv/ProgramInfo.h +++ b/clang/include/clang/CConv/ProgramInfo.h @@ -22,7 +22,6 @@ #include "Utils.h" #include "PersistentSourceLoc.h" #include "CConvInteractiveData.h" -#include "GatherTypes.h" class ProgramVariableAdder { @@ -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; } @@ -137,7 +132,6 @@ class ProgramInfo : public ProgramVariableAdder { StaticFunctionMapType StaticFunctionFVCons; std::map> GlobalVariableSymbols; - ParameterMap MF; // Object that contains all the bounds information of various // array variables. AVarBoundsInfo ArrBInfo; diff --git a/clang/lib/CConv/CConv.cpp b/clang/lib/CConv/CConv.cpp index 46353816e517..ee5d6219c7dd 100644 --- a/clang/lib/CConv/CConv.cpp +++ b/clang/lib/CConv/CConv.cpp @@ -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" @@ -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 GatherTool = + std::unique_ptr IMTool = newFrontendActionFactoryA - >(GlobalProgramInfo); - if (GatherTool) - Tool.run(GatherTool.get()); + >(GlobalProgramInfo); + if (IMTool) + Tool.run(IMTool.get()); else llvm_unreachable("No Action"); diff --git a/clang/lib/CConv/CMakeLists.txt b/clang/lib/CConv/CMakeLists.txt index 061221b156d3..b6f0a6ab56ab 100644 --- a/clang/lib/CConv/CMakeLists.txt +++ b/clang/lib/CConv/CMakeLists.txt @@ -19,7 +19,7 @@ set( LLVM_LINK_COMPONENTS ConstraintsGraph.cpp ConstraintVariables.cpp DeclRewriter.cpp - GatherTool.cpp + IntermediateToolHook.cpp MappingVisitor.cpp PersistentSourceLoc.cpp ProgramInfo.cpp diff --git a/clang/lib/CConv/CastPlacement.cpp b/clang/lib/CConv/CastPlacement.cpp index eaa94c966e76..da5a239398a4 100644 --- a/clang/lib/CConv/CastPlacement.cpp +++ b/clang/lib/CConv/CastPlacement.cpp @@ -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()) { @@ -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; } @@ -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)) { @@ -84,7 +81,7 @@ 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; @@ -92,9 +89,8 @@ bool CastPlacementVisitor::needCasting(ConstraintVariable *Src, // 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() + ")"; } diff --git a/clang/lib/CConv/GatherTool.cpp b/clang/lib/CConv/GatherTool.cpp deleted file mode 100644 index fdba0901a0b4..000000000000 --- a/clang/lib/CConv/GatherTool.cpp +++ /dev/null @@ -1,80 +0,0 @@ -//=--GatherTool.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 GatherTool.h -//===----------------------------------------------------------------------===// - -#include "clang/AST/RecursiveASTVisitor.h" -#include "clang/CConv/ArrayBoundsInferenceConsumer.h" -#include "clang/CConv/GatherTool.h" - -using namespace llvm; -using namespace clang; - - -class ParameterGatherer : public clang::RecursiveASTVisitor { -public: - explicit ParameterGatherer(ASTContext *_C, ProgramInfo &_I, ParameterMap &_MF) - : Context(_C), Info(_I), MF(_MF) {} - - bool VisitFunctionDecl(FunctionDecl *FD) { - auto Fn = FD->getNameAsString(); - bool AnExternFunction = FD->isGlobal() && Info.isAnExternFunction(Fn); - if (FD->doesThisDeclarationHaveABody() || AnExternFunction) { - std::vector ParmTypes; - int Pi = 0; - auto &CS = Info.getConstraints(); - for (auto &Param : FD->parameters()) { - bool IsWild = false; - //std::set Cvs = Info.getVariable(Context, FD, Pi); - std::set Cvs = Info.getVariable(Param, Context); - for (auto Cv : Cvs) { - IsWild |= Cv->hasWild(CS.getVariables()); - // If this an extern function, then check if there is - // any explicit annotation to. If not? then add a cast. - if (AnExternFunction && !IsWild) { - if (PVConstraint *PV = dyn_cast(Cv)) { - for (auto cKey : PV->getCvars()) { - if (CS.getAssignment(cKey) == CS.getWild()) { - IsWild = true; - break; - } - } - } - } - } - ParmTypes.push_back(IsWild ? WILD : CHECKED); - Pi++; - } - MF[Fn] = ParmTypes; - } - - return false; - } - -private: - ASTContext *Context; - ProgramInfo &Info; - ParameterMap &MF; -}; - -void ArgGatherer::HandleTranslationUnit(ASTContext &Context) { - Info.enterCompilationUnit(Context); - HandleArrayVariablesBoundsDetection(&Context, Info); - ParameterGatherer PG(&Context, Info, MF); - for (auto &D : Context.getTranslationUnitDecl()->decls()) { - PG.TraverseDecl(D); - } - - Info.merge_MF(MF); - - Info.exitCompilationUnit(); -} - -ParameterMap ArgGatherer::getMF() { - return MF; -} diff --git a/clang/lib/CConv/IntermediateToolHook.cpp b/clang/lib/CConv/IntermediateToolHook.cpp new file mode 100644 index 000000000000..7cf6b7d14e2c --- /dev/null +++ b/clang/lib/CConv/IntermediateToolHook.cpp @@ -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(); +} diff --git a/clang/lib/CConv/ProgramInfo.cpp b/clang/lib/CConv/ProgramInfo.cpp index 9b1d95bc20c3..e83b1aa4cf36 100644 --- a/clang/lib/CConv/ProgramInfo.cpp +++ b/clang/lib/CConv/ProgramInfo.cpp @@ -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) {