From 88f40cf303c33cc1d8231e140ec19a78e1aa64f8 Mon Sep 17 00:00:00 2001 From: Eugene Zelenko Date: Tue, 3 Apr 2018 21:31:50 +0000 Subject: [PATCH] [StaticAnalyzer] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC). llvm-svn: 329115 --- .../clang/StaticAnalyzer/Core/CheckerRegistry.h | 22 +- .../StaticAnalyzer/Core/PathSensitive/MemRegion.h | 235 ++++++++++----------- .../Core/PathSensitive/TaintManager.h | 31 ++- .../StaticAnalyzer/Core/PathSensitive/TaintTag.h | 11 +- clang/lib/StaticAnalyzer/Core/CheckerRegistry.cpp | 66 +++--- clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 200 ++++++++++-------- 6 files changed, 297 insertions(+), 268 deletions(-) diff --git a/clang/include/clang/StaticAnalyzer/Core/CheckerRegistry.h b/clang/include/clang/StaticAnalyzer/Core/CheckerRegistry.h index 3b26ed3..912a601 100644 --- a/clang/include/clang/StaticAnalyzer/Core/CheckerRegistry.h +++ b/clang/include/clang/StaticAnalyzer/Core/CheckerRegistry.h @@ -1,4 +1,4 @@ -//===--- CheckerRegistry.h - Maintains all available checkers ---*- C++ -*-===// +//===- CheckerRegistry.h - Maintains all available checkers -----*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -12,6 +12,9 @@ #include "clang/Basic/LLVM.h" #include "clang/StaticAnalyzer/Core/CheckerManager.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include #include // FIXME: move this information to an HTML file in docs/. @@ -64,8 +67,9 @@ #endif namespace clang { -class DiagnosticsEngine; + class AnalyzerOptions; +class DiagnosticsEngine; namespace ento { @@ -81,17 +85,18 @@ class CheckerRegistry { public: /// Initialization functions perform any necessary setup for a checker. /// They should include a call to CheckerManager::registerChecker. - typedef void (*InitializationFunction)(CheckerManager &); + using InitializationFunction = void (*)(CheckerManager &); + struct CheckerInfo { InitializationFunction Initialize; StringRef FullName; StringRef Desc; CheckerInfo(InitializationFunction fn, StringRef name, StringRef desc) - : Initialize(fn), FullName(name), Desc(desc) {} + : Initialize(fn), FullName(name), Desc(desc) {} }; - typedef std::vector CheckerInfoList; + using CheckerInfoList = std::vector; private: template @@ -136,7 +141,8 @@ private: mutable llvm::StringMap Packages; }; -} // end namespace ento -} // end namespace clang +} // namespace ento -#endif +} // namespace clang + +#endif // LLVM_CLANG_STATICANALYZER_CORE_CHECKERREGISTRY_H diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h index 1fadb3e..b7f618a 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h @@ -1,4 +1,4 @@ -//== MemRegion.h - Abstract memory regions for static analysis --*- C++ -*--==// +//==- MemRegion.h - Abstract memory regions for static analysis -*- C++ -*--==// // // The LLVM Compiler Infrastructure // @@ -19,24 +19,39 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/CharUnits.h" #include "clang/AST/Decl.h" -#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/DeclarationName.h" +#include "clang/AST/Expr.h" #include "clang/AST/ExprObjC.h" -#include "clang/Analysis/AnalysisDeclContext.h" +#include "clang/AST/Type.h" #include "clang/Basic/LLVM.h" +#include "clang/Basic/SourceLocation.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/FoldingSet.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/PointerIntPair.h" #include "llvm/Support/Allocator.h" -#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Casting.h" +#include +#include +#include #include +#include namespace clang { +class AnalysisDeclContext; +class CXXRecordDecl; +class Decl; class LocationContext; class StackFrameContext; namespace ento { class CodeTextRegion; +class MemRegion; class MemRegionManager; class MemSpaceRegion; class SValBuilder; @@ -46,7 +61,7 @@ class VarRegion; /// Represent a region's offset within the top level base region. class RegionOffset { /// The base region. - const MemRegion *R; + const MemRegion *R = nullptr; /// The bit offset within the base region. Can be negative. int64_t Offset; @@ -54,9 +69,9 @@ class RegionOffset { public: // We're using a const instead of an enumeration due to the size required; // Visual Studio will only create enumerations of size int, not long long. - static const int64_t Symbolic = INT64_MAX; + static const int64_t Symbolic = std::numeric_limits::max(); - RegionOffset() : R(nullptr) {} + RegionOffset() = default; RegionOffset(const MemRegion *r, int64_t off) : R(r), Offset(off) {} const MemRegion *getRegion() const { return R; } @@ -153,7 +168,6 @@ public: virtual bool isBoundable() const { return false; } - /// Get descriptive name for memory region. The name is obtained from /// the variable/field declaration retrieved from the memory region. /// Regions that point to an element of an array are returned as: "arr[0]". @@ -164,7 +178,6 @@ public: /// \returns variable name for memory region std::string getDescriptiveName(bool UseQuotes = true) const; - /// Retrieve source range from memory region. The range retrieval /// is based on the decl obtained from the memory region. /// For a VarRegion the range of the base region is returned. @@ -173,7 +186,7 @@ public: /// The client is responsible for checking if the returned range is valid. /// /// \returns source range for declaration retrieved from memory region - clang::SourceRange sourceRange() const; + SourceRange sourceRange() const; }; /// MemSpaceRegion - A memory region that represents a "memory space"; @@ -242,7 +255,7 @@ class StaticGlobalSpaceRegion : public GlobalsSpaceRegion { const CodeTextRegion *CR; StaticGlobalSpaceRegion(MemRegionManager *mgr, const CodeTextRegion *cr) - : GlobalsSpaceRegion(mgr, StaticGlobalSpaceRegionKind), CR(cr) { + : GlobalsSpaceRegion(mgr, StaticGlobalSpaceRegionKind), CR(cr) { assert(cr); } @@ -265,7 +278,7 @@ public: /// RegionStoreManager::invalidateRegions (instead of finding all the dependent /// globals, we invalidate the whole parent region). class NonStaticGlobalSpaceRegion : public GlobalsSpaceRegion { - virtual void anchor() override; + void anchor() override; protected: NonStaticGlobalSpaceRegion(MemRegionManager *mgr, Kind k) @@ -274,7 +287,6 @@ protected: } public: - static bool classof(const MemRegion *R) { Kind k = R->getKind(); return k >= BEGIN_NON_STATIC_GLOBAL_MEMSPACES && @@ -288,10 +300,9 @@ class GlobalSystemSpaceRegion : public NonStaticGlobalSpaceRegion { friend class MemRegionManager; GlobalSystemSpaceRegion(MemRegionManager *mgr) - : NonStaticGlobalSpaceRegion(mgr, GlobalSystemSpaceRegionKind) {} + : NonStaticGlobalSpaceRegion(mgr, GlobalSystemSpaceRegionKind) {} public: - void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -308,10 +319,9 @@ class GlobalImmutableSpaceRegion : public NonStaticGlobalSpaceRegion { friend class MemRegionManager; GlobalImmutableSpaceRegion(MemRegionManager *mgr) - : NonStaticGlobalSpaceRegion(mgr, GlobalImmutableSpaceRegionKind) {} + : NonStaticGlobalSpaceRegion(mgr, GlobalImmutableSpaceRegionKind) {} public: - void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -326,10 +336,9 @@ class GlobalInternalSpaceRegion : public NonStaticGlobalSpaceRegion { friend class MemRegionManager; GlobalInternalSpaceRegion(MemRegionManager *mgr) - : NonStaticGlobalSpaceRegion(mgr, GlobalInternalSpaceRegionKind) {} + : NonStaticGlobalSpaceRegion(mgr, GlobalInternalSpaceRegionKind) {} public: - void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -341,9 +350,9 @@ class HeapSpaceRegion : public MemSpaceRegion { friend class MemRegionManager; HeapSpaceRegion(MemRegionManager *mgr) - : MemSpaceRegion(mgr, HeapSpaceRegionKind) {} -public: + : MemSpaceRegion(mgr, HeapSpaceRegionKind) {} +public: void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -353,11 +362,11 @@ public: class UnknownSpaceRegion : public MemSpaceRegion { friend class MemRegionManager; + UnknownSpaceRegion(MemRegionManager *mgr) : MemSpaceRegion(mgr, UnknownSpaceRegionKind) {} public: - void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -372,7 +381,7 @@ class StackSpaceRegion : public MemSpaceRegion { protected: StackSpaceRegion(MemRegionManager *mgr, Kind k, const StackFrameContext *sfc) - : MemSpaceRegion(mgr, k), SFC(sfc) { + : MemSpaceRegion(mgr, k), SFC(sfc) { assert(classof(this)); assert(sfc); } @@ -390,10 +399,11 @@ public: class StackLocalsSpaceRegion : public StackSpaceRegion { friend class MemRegionManager; + StackLocalsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc) - : StackSpaceRegion(mgr, StackLocalsSpaceRegionKind, sfc) {} -public: + : StackSpaceRegion(mgr, StackLocalsSpaceRegionKind, sfc) {} +public: void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -404,10 +414,11 @@ public: class StackArgumentsSpaceRegion : public StackSpaceRegion { private: friend class MemRegionManager; + StackArgumentsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc) - : StackSpaceRegion(mgr, StackArgumentsSpaceRegionKind, sfc) {} -public: + : StackSpaceRegion(mgr, StackArgumentsSpaceRegionKind, sfc) {} +public: void dumpToStream(raw_ostream &os) const override; static bool classof(const MemRegion *R) { @@ -415,7 +426,6 @@ public: } }; - /// SubRegion - A region that subsets another larger region. Most regions /// are subclasses of SubRegion. class SubRegion : public MemRegion { @@ -423,6 +433,7 @@ class SubRegion : public MemRegion { protected: const MemRegion* superRegion; + SubRegion(const MemRegion *sReg, Kind k) : MemRegion(k), superRegion(sReg) { assert(classof(this)); assert(sReg); @@ -456,8 +467,10 @@ public: class AllocaRegion : public SubRegion { friend class MemRegionManager; - unsigned Cnt; // Block counter. Used to distinguish different pieces of - // memory allocated by alloca at the same call site. + // Block counter. Used to distinguish different pieces of memory allocated by + // alloca at the same call site. + unsigned Cnt; + const Expr *Ex; AllocaRegion(const Expr *ex, unsigned cnt, const MemSpaceRegion *superRegion) @@ -469,7 +482,6 @@ class AllocaRegion : public SubRegion { unsigned Cnt, const MemRegion *superRegion); public: - const Expr *getExpr() const { return Ex; } bool isBoundable() const override { return true; } @@ -487,7 +499,7 @@ public: /// TypedRegion - An abstract class representing regions that are typed. class TypedRegion : public SubRegion { - virtual void anchor() override; + void anchor() override; protected: TypedRegion(const MemRegion *sReg, Kind k) : SubRegion(sReg, k) { @@ -511,7 +523,7 @@ public: /// TypedValueRegion - An abstract class representing regions having a typed value. class TypedValueRegion : public TypedRegion { - virtual void anchor() override; + void anchor() override; protected: TypedValueRegion(const MemRegion* sReg, Kind k) : TypedRegion(sReg, k) { @@ -543,9 +555,8 @@ public: } }; - class CodeTextRegion : public TypedRegion { - virtual void anchor() override; + void anchor() override; protected: CodeTextRegion(const MemSpaceRegion *sreg, Kind k) : TypedRegion(sreg, k) { @@ -568,7 +579,7 @@ class FunctionCodeRegion : public CodeTextRegion { const NamedDecl *FD; FunctionCodeRegion(const NamedDecl *fd, const CodeSpaceRegion* sreg) - : CodeTextRegion(sreg, FunctionCodeRegionKind), FD(fd) { + : CodeTextRegion(sreg, FunctionCodeRegionKind), FD(fd) { assert(isa(fd) || isa(fd)); } @@ -578,7 +589,7 @@ class FunctionCodeRegion : public CodeTextRegion { public: QualType getLocationType() const override { const ASTContext &Ctx = getContext(); - if (const FunctionDecl *D = dyn_cast(FD)) { + if (const auto *D = dyn_cast(FD)) { return Ctx.getPointerType(D->getType()); } @@ -587,7 +598,7 @@ public: // TODO: We might want to return a different type here (ex: id (*ty)(...)) // depending on how it is used. - return QualType(); + return {}; } const NamedDecl *getDecl() const { @@ -603,7 +614,6 @@ public: } }; - /// BlockCodeRegion - A region that represents code texts of blocks (closures). /// Blocks are represented with two kinds of regions. BlockCodeRegions /// represent the "code", while BlockDataRegions represent instances of blocks, @@ -659,15 +669,15 @@ class BlockDataRegion : public TypedRegion { friend class MemRegionManager; const BlockCodeRegion *BC; - const LocationContext *LC; // Can be null */ + const LocationContext *LC; // Can be null unsigned BlockCount; - void *ReferencedVars; - void *OriginalVars; + void *ReferencedVars = nullptr; + void *OriginalVars = nullptr; BlockDataRegion(const BlockCodeRegion *bc, const LocationContext *lc, unsigned count, const MemSpaceRegion *sreg) : TypedRegion(sreg, BlockDataRegionKind), BC(bc), LC(lc), - BlockCount(count), ReferencedVars(nullptr), OriginalVars(nullptr) { + BlockCount(count) { assert(bc); assert(lc); assert(isa(sreg) || @@ -681,7 +691,7 @@ class BlockDataRegion : public TypedRegion { public: const BlockCodeRegion *getCodeRegion() const { return BC; } - + const BlockDecl *getDecl() const { return BC->getDecl(); } QualType getLocationType() const override { return BC->getLocationType(); } @@ -689,14 +699,16 @@ public: class referenced_vars_iterator { const MemRegion * const *R; const MemRegion * const *OriginalR; + public: explicit referenced_vars_iterator(const MemRegion * const *r, const MemRegion * const *originalR) - : R(r), OriginalR(originalR) {} + : R(r), OriginalR(originalR) {} const VarRegion *getCapturedRegion() const { return cast(*R); } + const VarRegion *getOriginalRegion() const { return cast(*OriginalR); } @@ -705,10 +717,12 @@ public: assert((R == nullptr) == (I.R == nullptr)); return I.R == R; } + bool operator!=(const referenced_vars_iterator &I) const { assert((R == nullptr) == (I.R == nullptr)); return I.R != R; } + referenced_vars_iterator &operator++() { ++R; ++OriginalR; @@ -730,6 +744,7 @@ public: static bool classof(const MemRegion* R) { return R->getKind() == BlockDataRegionKind; } + private: void LazyInitializeReferencedVars(); std::pair @@ -756,9 +771,7 @@ class SymbolicRegion : public SubRegion { } public: - SymbolRef getSymbol() const { - return sym; - } + SymbolRef getSymbol() const { return sym; } bool isBoundable() const override { return true; } @@ -781,24 +794,21 @@ public: class StringRegion : public TypedValueRegion { friend class MemRegionManager; - const StringLiteral* Str; + const StringLiteral *Str; StringRegion(const StringLiteral *str, const GlobalInternalSpaceRegion *sreg) : TypedValueRegion(sreg, StringRegionKind), Str(str) { assert(str); } - static void ProfileRegion(llvm::FoldingSetNodeID& ID, - const StringLiteral* Str, - const MemRegion* superRegion); + static void ProfileRegion(llvm::FoldingSetNodeID &ID, + const StringLiteral *Str, + const MemRegion *superRegion); public: + const StringLiteral *getStringLiteral() const { return Str; } - const StringLiteral* getStringLiteral() const { return Str; } - - QualType getValueType() const override { - return Str->getType(); - } + QualType getValueType() const override { return Str->getType(); } DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override; @@ -819,7 +829,7 @@ public: class ObjCStringRegion : public TypedValueRegion { friend class MemRegionManager; - const ObjCStringLiteral* Str; + const ObjCStringLiteral *Str; ObjCStringRegion(const ObjCStringLiteral *str, const GlobalInternalSpaceRegion *sreg) @@ -827,17 +837,14 @@ class ObjCStringRegion : public TypedValueRegion { assert(str); } - static void ProfileRegion(llvm::FoldingSetNodeID& ID, - const ObjCStringLiteral* Str, - const MemRegion* superRegion); + static void ProfileRegion(llvm::FoldingSetNodeID &ID, + const ObjCStringLiteral *Str, + const MemRegion *superRegion); public: - - const ObjCStringLiteral* getObjCStringLiteral() const { return Str; } + const ObjCStringLiteral *getObjCStringLiteral() const { return Str; } - QualType getValueType() const override { - return Str->getType(); - } + QualType getValueType() const override { return Str->getType(); } bool isBoundable() const override { return false; } @@ -871,10 +878,9 @@ class CompoundLiteralRegion : public TypedValueRegion { static void ProfileRegion(llvm::FoldingSetNodeID& ID, const CompoundLiteralExpr *CL, const MemRegion* superRegion); + public: - QualType getValueType() const override { - return CL->getType(); - } + QualType getValueType() const override { return CL->getType(); } bool isBoundable() const override { return !CL->isFileScope(); } @@ -945,13 +951,13 @@ public: void dumpToStream(raw_ostream &os) const override; - static bool classof(const MemRegion* R) { - return R->getKind() == VarRegionKind; - } - bool canPrintPrettyAsExpr() const override; void printPrettyAsExpr(raw_ostream &os) const override; + + static bool classof(const MemRegion* R) { + return R->getKind() == VarRegionKind; + } }; /// CXXThisRegion - Represents the region for the implicit 'this' parameter @@ -993,7 +999,7 @@ class FieldRegion : public DeclRegion { friend class MemRegionManager; FieldRegion(const FieldDecl *fd, const SubRegion* sReg) - : DeclRegion(fd, sReg, FieldRegionKind) {} + : DeclRegion(fd, sReg, FieldRegionKind) {} static void ProfileRegion(llvm::FoldingSetNodeID& ID, const FieldDecl *FD, const MemRegion* superRegion) { @@ -1010,16 +1016,16 @@ public: DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override; - static bool classof(const MemRegion* R) { - return R->getKind() == FieldRegionKind; - } - void dumpToStream(raw_ostream &os) const override; bool canPrintPretty() const override; void printPretty(raw_ostream &os) const override; bool canPrintPrettyAsExpr() const override; void printPrettyAsExpr(raw_ostream &os) const override; + + static bool classof(const MemRegion* R) { + return R->getKind() == FieldRegionKind; + } }; class ObjCIvarRegion : public DeclRegion { @@ -1043,12 +1049,11 @@ public: return R->getKind() == ObjCIvarRegionKind; } }; + //===----------------------------------------------------------------------===// // Auxiliary data classes for use with MemRegions. //===----------------------------------------------------------------------===// -class ElementRegion; - class RegionRawOffset { friend class ElementRegion; @@ -1056,7 +1061,7 @@ class RegionRawOffset { CharUnits Offset; RegionRawOffset(const MemRegion* reg, CharUnits offset = CharUnits::Zero()) - : Region(reg), Offset(offset) {} + : Region(reg), Offset(offset) {} public: // FIXME: Eventually support symbolic offsets. @@ -1075,8 +1080,8 @@ class ElementRegion : public TypedValueRegion { NonLoc Index; ElementRegion(QualType elementType, NonLoc Idx, const SubRegion *sReg) - : TypedValueRegion(sReg, ElementRegionKind), - ElementType(elementType), Index(Idx) { + : TypedValueRegion(sReg, ElementRegionKind), ElementType(elementType), + Index(Idx) { assert((!Idx.getAs() || Idx.castAs().getValue().isSigned()) && "The index must be signed"); @@ -1088,16 +1093,12 @@ class ElementRegion : public TypedValueRegion { SVal Idx, const MemRegion* superRegion); public: - NonLoc getIndex() const { return Index; } - QualType getValueType() const override { - return ElementType; - } + QualType getValueType() const override { return ElementType; } + + QualType getElementType() const { return ElementType; } - QualType getElementType() const { - return ElementType; - } /// Compute the offset within the array. The array might also be a subobject. RegionRawOffset getAsArrayOffset() const; @@ -1129,9 +1130,7 @@ class CXXTempObjectRegion : public TypedValueRegion { public: const Expr *getExpr() const { return Ex; } - QualType getValueType() const override { - return Ex->getType(); - } + QualType getValueType() const override { return Ex->getType(); } void dumpToStream(raw_ostream &os) const override; @@ -1168,18 +1167,18 @@ public: void Profile(llvm::FoldingSetNodeID &ID) const override; - static bool classof(const MemRegion *region) { - return region->getKind() == CXXBaseObjectRegionKind; - } - bool canPrintPrettyAsExpr() const override; void printPrettyAsExpr(raw_ostream &os) const override; + + static bool classof(const MemRegion *region) { + return region->getKind() == CXXBaseObjectRegionKind; + } }; template const RegionTy* MemRegion::getAs() const { - if (const RegionTy* RT = dyn_cast(this)) + if (const auto *RT = dyn_cast(this)) return RT; return nullptr; @@ -1194,11 +1193,10 @@ class MemRegionManager { llvm::BumpPtrAllocator& A; llvm::FoldingSet Regions; - GlobalInternalSpaceRegion *InternalGlobals; - GlobalSystemSpaceRegion *SystemGlobals; - GlobalImmutableSpaceRegion *ImmutableGlobals; + GlobalInternalSpaceRegion *InternalGlobals = nullptr; + GlobalSystemSpaceRegion *SystemGlobals = nullptr; + GlobalImmutableSpaceRegion *ImmutableGlobals = nullptr; - llvm::DenseMap StackLocalsSpaceRegions; llvm::DenseMap @@ -1206,16 +1204,12 @@ class MemRegionManager { llvm::DenseMap StaticsGlobalSpaceRegions; - HeapSpaceRegion *heap; - UnknownSpaceRegion *unknown; - CodeSpaceRegion *code; + HeapSpaceRegion *heap = nullptr; + UnknownSpaceRegion *unknown = nullptr; + CodeSpaceRegion *code = nullptr; public: - MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a) - : C(c), A(a), InternalGlobals(nullptr), SystemGlobals(nullptr), - ImmutableGlobals(nullptr), heap(nullptr), unknown(nullptr), - code(nullptr) {} - + MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a) : C(c), A(a) {} ~MemRegionManager(); ASTContext &getContext() { return C; } @@ -1269,7 +1263,7 @@ public: /// \brief Return a unique symbolic region belonging to heap memory space. const SymbolicRegion *getSymbolicHeapRegion(SymbolRef sym); - const StringRegion *getStringRegion(const StringLiteral* Str); + const StringRegion *getStringRegion(const StringLiteral *Str); const ObjCStringRegion *getObjCStringRegion(const ObjCStringLiteral *Str); @@ -1388,24 +1382,28 @@ inline ASTContext &MemRegion::getContext() const { /// Information about invalidation for a particular region/symbol. class RegionAndSymbolInvalidationTraits { - typedef unsigned char StorageTypeForKinds; + using StorageTypeForKinds = unsigned char; + llvm::DenseMap MRTraitsMap; llvm::DenseMap SymTraitsMap; - typedef llvm::DenseMap::const_iterator - const_region_iterator; - typedef llvm::DenseMap::const_iterator - const_symbol_iterator; + using const_region_iterator = + llvm::DenseMap::const_iterator; + using const_symbol_iterator = + llvm::DenseMap::const_iterator; public: /// \brief Describes different invalidation traits. enum InvalidationKinds { /// Tells that a region's contents is not changed. TK_PreserveContents = 0x1, + /// Suppress pointer-escaping of a region. TK_SuppressEscape = 0x2, + // Do not invalidate super region. TK_DoNotInvalidateSuperRegion = 0x4, + /// When applied to a MemSpaceRegion, indicates the entire memory space /// should be invalidated. TK_EntireMemSpace = 0x8 @@ -1423,8 +1421,7 @@ public: //===----------------------------------------------------------------------===// // Pretty-printing regions. //===----------------------------------------------------------------------===// -inline raw_ostream &operator<<(raw_ostream &os, - const clang::ento::MemRegion *R) { +inline raw_ostream &operator<<(raw_ostream &os, const MemRegion *R) { R->dumpToStream(os); return os; } @@ -1433,4 +1430,4 @@ inline raw_ostream &operator<<(raw_ostream &os, } // namespace clang -#endif +#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h index 7b76263..ce19b71 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h @@ -1,4 +1,4 @@ -//== TaintManager.h - Managing taint --------------------------- -*- C++ -*--=// +//===- TaintManager.h - Managing taint --------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -14,9 +14,9 @@ #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_TAINTMANAGER_H #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_TAINTMANAGER_H -#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h" #include "clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h" #include "llvm/ADT/ImmutableMap.h" @@ -29,28 +29,37 @@ namespace ento { // FIXME: This does not use the nice trait macros because it must be accessible // from multiple translation units. struct TaintMap {}; -typedef llvm::ImmutableMap TaintMapImpl; + +using TaintMapImpl = llvm::ImmutableMap; + template<> struct ProgramStateTrait : public ProgramStatePartialTrait { - static void *GDMIndex() { static int index = 0; return &index; } + static void *GDMIndex() { + static int index = 0; + return &index; + } }; /// The GDM component mapping derived symbols' parent symbols to their /// underlying regions. This is used to efficiently check whether a symbol is /// tainted when it represents a sub-region of a tainted symbol. struct DerivedSymTaint {}; -typedef llvm::ImmutableMap DerivedSymTaintImpl; + +using DerivedSymTaintImpl = llvm::ImmutableMap; + template<> struct ProgramStateTrait : public ProgramStatePartialTrait { - static void *GDMIndex() { static int index; return &index; } + static void *GDMIndex() { + static int index; + return &index; + } }; class TaintManager { - - TaintManager() {} + TaintManager() = default; }; -} -} +} // namespace ento +} // namespace clang -#endif +#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_TAINTMANAGER_H diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h index 0c56e7d..50c4b81 100644 --- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h +++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h @@ -1,4 +1,4 @@ -//== TaintTag.h - Path-sensitive "State" for tracking values -*- C++ -*--=// +//===- TaintTag.h - Path-sensitive "State" for tracking values --*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -11,6 +11,7 @@ // of taint. // //===----------------------------------------------------------------------===// + #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_TAINTTAG_H #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_TAINTTAG_H @@ -19,9 +20,11 @@ namespace ento { /// The type of taint, which helps to differentiate between different types of /// taint. -typedef unsigned TaintTagType; +using TaintTagType = unsigned; + static const TaintTagType TaintTagGeneric = 0; -}} +} // namespace ento +} // namespace clang -#endif +#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_TAINTTAG_H diff --git a/clang/lib/StaticAnalyzer/Core/CheckerRegistry.cpp b/clang/lib/StaticAnalyzer/Core/CheckerRegistry.cpp index 6437f9c..645845e 100644 --- a/clang/lib/StaticAnalyzer/Core/CheckerRegistry.cpp +++ b/clang/lib/StaticAnalyzer/Core/CheckerRegistry.cpp @@ -1,4 +1,4 @@ -//===--- CheckerRegistry.cpp - Maintains all available checkers -*- C++ -*-===// +//===- CheckerRegistry.cpp - Maintains all available checkers -------------===// // // The LLVM Compiler Infrastructure // @@ -9,18 +9,26 @@ #include "clang/StaticAnalyzer/Core/CheckerRegistry.h" #include "clang/Basic/Diagnostic.h" +#include "clang/Basic/LLVM.h" #include "clang/Frontend/FrontendDiagnostic.h" +#include "clang/StaticAnalyzer/Core/CheckerManager.h" #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h" #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" +#include +#include +#include using namespace clang; using namespace ento; static const char PackageSeparator = '.'; -typedef llvm::SetVector CheckerInfoSet; +using CheckerInfoSet = llvm::SetVector; static bool checkerNameLT(const CheckerRegistry::CheckerInfo &a, const CheckerRegistry::CheckerInfo &b) { @@ -50,8 +58,7 @@ static void collectCheckers(const CheckerRegistry::CheckerInfoList &checkers, // Use a binary search to find the possible start of the package. CheckerRegistry::CheckerInfo packageInfo(nullptr, opt.getName(), ""); auto end = checkers.cend(); - CheckerRegistry::CheckerInfoList::const_iterator i = - std::lower_bound(checkers.cbegin(), end, packageInfo, checkerNameLT); + auto i = std::lower_bound(checkers.cbegin(), end, packageInfo, checkerNameLT); // If we didn't even find a possible package, give up. if (i == end) @@ -73,12 +80,11 @@ static void collectCheckers(const CheckerRegistry::CheckerInfoList &checkers, size = packageSize->getValue(); // Step through all the checkers in the package. - for (auto checkEnd = i+size; i != checkEnd; ++i) { + for (auto checkEnd = i+size; i != checkEnd; ++i) if (opt.isEnabled()) collected.insert(&*i); else collected.remove(&*i); - } } void CheckerRegistry::addChecker(InitializationFunction fn, StringRef name, @@ -101,38 +107,34 @@ void CheckerRegistry::initializeManager(CheckerManager &checkerMgr, // Collect checkers enabled by the options. CheckerInfoSet enabledCheckers; - for (SmallVectorImpl::iterator - i = opts.begin(), e = opts.end(); i != e; ++i) { - collectCheckers(Checkers, Packages, *i, enabledCheckers); - } + for (auto &i : opts) + collectCheckers(Checkers, Packages, i, enabledCheckers); // Initialize the CheckerManager with all enabled checkers. - for (CheckerInfoSet::iterator - i = enabledCheckers.begin(), e = enabledCheckers.end(); i != e; ++i) { - checkerMgr.setCurrentCheckName(CheckName((*i)->FullName)); - (*i)->Initialize(checkerMgr); + for (const auto *i :enabledCheckers) { + checkerMgr.setCurrentCheckName(CheckName(i->FullName)); + i->Initialize(checkerMgr); } } void CheckerRegistry::validateCheckerOptions(const AnalyzerOptions &opts, DiagnosticsEngine &diags) const { - for (auto &config : opts.Config) { + for (const auto &config : opts.Config) { size_t pos = config.getKey().find(':'); if (pos == StringRef::npos) continue; bool hasChecker = false; StringRef checkerName = config.getKey().substr(0, pos); - for (auto &checker : Checkers) { + for (const auto &checker : Checkers) { if (checker.FullName.startswith(checkerName) && (checker.FullName.size() == pos || checker.FullName[pos] == '.')) { hasChecker = true; break; } } - if (!hasChecker) { + if (!hasChecker) diags.Report(diag::err_unknown_analyzer_checker) << checkerName; - } } } @@ -149,28 +151,26 @@ void CheckerRegistry::printHelp(raw_ostream &out, // Find the maximum option length. size_t optionFieldWidth = 0; - for (CheckerInfoList::const_iterator i = Checkers.begin(), e = Checkers.end(); - i != e; ++i) { + for (const auto &i : Checkers) { // Limit the amount of padding we are willing to give up for alignment. // Package.Name Description [Hidden] - size_t nameLength = i->FullName.size(); + size_t nameLength = i.FullName.size(); if (nameLength <= maxNameChars) optionFieldWidth = std::max(optionFieldWidth, nameLength); } const size_t initialPad = 2; - for (CheckerInfoList::const_iterator i = Checkers.begin(), e = Checkers.end(); - i != e; ++i) { - out.indent(initialPad) << i->FullName; + for (const auto &i : Checkers) { + out.indent(initialPad) << i.FullName; - int pad = optionFieldWidth - i->FullName.size(); + int pad = optionFieldWidth - i.FullName.size(); // Break on long option names. if (pad < 0) { out << '\n'; pad = optionFieldWidth + initialPad; } - out.indent(pad + 2) << i->Desc; + out.indent(pad + 2) << i.Desc; out << '\n'; } @@ -182,15 +182,9 @@ void CheckerRegistry::printList( // Collect checkers enabled by the options. CheckerInfoSet enabledCheckers; - for (SmallVectorImpl::iterator i = opts.begin(), - e = opts.end(); - i != e; ++i) { - collectCheckers(Checkers, Packages, *i, enabledCheckers); - } + for (auto &i : opts) + collectCheckers(Checkers, Packages, i, enabledCheckers); - for (CheckerInfoSet::const_iterator i = enabledCheckers.begin(), - e = enabledCheckers.end(); - i != e; ++i) { - out << (*i)->FullName << '\n'; - } + for (const auto *i : enabledCheckers) + out << i->FullName << '\n'; } diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp index 3249d4f16..ca8f836 100644 --- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -1,4 +1,4 @@ -//== MemRegion.cpp - Abstract memory regions for static analysis --*- C++ -*--// +//===- MemRegion.cpp - Abstract memory regions for static analysis --------===// // // The LLVM Compiler Infrastructure // @@ -14,24 +14,50 @@ //===----------------------------------------------------------------------===// #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" +#include "clang/AST/ASTContext.h" #include "clang/AST/Attr.h" #include "clang/AST/CharUnits.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" +#include "clang/AST/Expr.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/RecordLayout.h" +#include "clang/AST/Type.h" #include "clang/Analysis/AnalysisDeclContext.h" #include "clang/Analysis/Support/BumpVector.h" +#include "clang/Basic/IdentifierTable.h" +#include "clang/Basic/LLVM.h" #include "clang/Basic/SourceManager.h" #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" -#include "llvm/Support/raw_ostream.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/FoldingSet.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/PointerUnion.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/Allocator.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" - -#include - -#define DEBUG_TYPE "MemRegion" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include +#include +#include +#include +#include using namespace clang; using namespace ento; +#define DEBUG_TYPE "MemRegion" + //===----------------------------------------------------------------------===// // MemRegion Construction. //===----------------------------------------------------------------------===// @@ -42,8 +68,7 @@ RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, llvm::FoldingSetNodeID ID; RegionTy::ProfileRegion(ID, arg1, superRegion); void *InsertPos; - RegionTy* R = cast_or_null(Regions.FindNodeOrInsertPos(ID, - InsertPos)); + auto *R = cast_or_null(Regions.FindNodeOrInsertPos(ID, InsertPos)); if (!R) { R = A.Allocate(); @@ -60,8 +85,7 @@ RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2, llvm::FoldingSetNodeID ID; RegionTy::ProfileRegion(ID, arg1, arg2, superRegion); void *InsertPos; - RegionTy* R = cast_or_null(Regions.FindNodeOrInsertPos(ID, - InsertPos)); + auto *R = cast_or_null(Regions.FindNodeOrInsertPos(ID, InsertPos)); if (!R) { R = A.Allocate(); @@ -80,8 +104,7 @@ RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2, llvm::FoldingSetNodeID ID; RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion); void *InsertPos; - RegionTy* R = cast_or_null(Regions.FindNodeOrInsertPos(ID, - InsertPos)); + auto *R = cast_or_null(Regions.FindNodeOrInsertPos(ID, InsertPos)); if (!R) { R = A.Allocate(); @@ -96,12 +119,11 @@ RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2, // Object destruction. //===----------------------------------------------------------------------===// -MemRegion::~MemRegion() {} +MemRegion::~MemRegion() = default; -MemRegionManager::~MemRegionManager() { - // All regions and their data are BumpPtrAllocated. No need to call - // their destructors. -} +// All regions and their data are BumpPtrAllocated. No need to call their +// destructors. +MemRegionManager::~MemRegionManager() = default; //===----------------------------------------------------------------------===// // Basic methods. @@ -112,7 +134,7 @@ bool SubRegion::isSubRegionOf(const MemRegion* R) const { do { if (r == R) return true; - if (const SubRegion* sr = dyn_cast(r)) + if (const auto *sr = dyn_cast(r)) r = sr->getSuperRegion(); else break; @@ -124,16 +146,16 @@ MemRegionManager* SubRegion::getMemRegionManager() const { const SubRegion* r = this; do { const MemRegion *superRegion = r->getSuperRegion(); - if (const SubRegion *sr = dyn_cast(superRegion)) { + if (const auto *sr = dyn_cast(superRegion)) { r = sr; continue; } return superRegion->getMemRegionManager(); - } while (1); + } while (true); } const StackFrameContext *VarRegion::getStackFrame() const { - const StackSpaceRegion *SSR = dyn_cast(getMemorySpace()); + const auto *SSR = dyn_cast(getMemorySpace()); return SSR ? SSR->getStackFrame() : nullptr; } @@ -220,17 +242,17 @@ void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(getCodeRegion()); } -void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, - const StringLiteral* Str, - const MemRegion* superRegion) { +void StringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, + const StringLiteral *Str, + const MemRegion *superRegion) { ID.AddInteger(static_cast(StringRegionKind)); ID.AddPointer(Str); ID.AddPointer(superRegion); } -void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, - const ObjCStringLiteral* Str, - const MemRegion* superRegion) { +void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, + const ObjCStringLiteral *Str, + const MemRegion *superRegion) { ID.AddInteger(static_cast(ObjCStringRegionKind)); ID.AddPointer(Str); ID.AddPointer(superRegion); @@ -385,13 +407,19 @@ void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { // Region anchors. //===----------------------------------------------------------------------===// -void GlobalsSpaceRegion::anchor() { } -void NonStaticGlobalSpaceRegion::anchor() { } -void StackSpaceRegion::anchor() { } -void TypedRegion::anchor() { } -void TypedValueRegion::anchor() { } -void CodeTextRegion::anchor() { } -void SubRegion::anchor() { } +void GlobalsSpaceRegion::anchor() {} + +void NonStaticGlobalSpaceRegion::anchor() {} + +void StackSpaceRegion::anchor() {} + +void TypedRegion::anchor() {} + +void TypedValueRegion::anchor() {} + +void CodeTextRegion::anchor() {} + +void SubRegion::anchor() {} //===----------------------------------------------------------------------===// // Region pretty-printing. @@ -413,7 +441,7 @@ void MemRegion::dumpToStream(raw_ostream &os) const { } void AllocaRegion::dumpToStream(raw_ostream &os) const { - os << "alloca{" << static_cast(Ex) << ',' << Cnt << '}'; + os << "alloca{" << static_cast(Ex) << ',' << Cnt << '}'; } void FunctionCodeRegion::dumpToStream(raw_ostream &os) const { @@ -421,7 +449,7 @@ void FunctionCodeRegion::dumpToStream(raw_ostream &os) const { } void BlockCodeRegion::dumpToStream(raw_ostream &os) const { - os << "block_code{" << static_cast(this) << '}'; + os << "block_code{" << static_cast(this) << '}'; } void BlockDataRegion::dumpToStream(raw_ostream &os) const { @@ -437,12 +465,12 @@ void BlockDataRegion::dumpToStream(raw_ostream &os) const { void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const { // FIXME: More elaborate pretty-printing. - os << "{ " << static_cast(CL) << " }"; + os << "{ " << static_cast(CL) << " }"; } void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const { os << "temp_object{" << getValueType().getAsString() << ',' - << static_cast(Ex) << '}'; + << static_cast(Ex) << '}'; } void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const { @@ -484,11 +512,10 @@ void SymbolicRegion::dumpToStream(raw_ostream &os) const { void VarRegion::dumpToStream(raw_ostream &os) const { const auto *VD = cast(D); - if (const auto *ID = VD->getIdentifier()) { + if (const IdentifierInfo *ID = VD->getIdentifier()) os << ID->getName(); - } else { - os << "VarRegion{" << static_cast(this) << '}'; - } + else + os << "VarRegion{" << static_cast(this) << '}'; } LLVM_DUMP_METHOD void RegionRawOffset::dump() const { @@ -632,19 +659,18 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const { // Get variable name. if (R && R->canPrintPrettyAsExpr()) { R->printPrettyAsExpr(os); - if (UseQuotes) { + if (UseQuotes) return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str(); - } else { + else return (llvm::Twine(os.str()) + ArrayIndices).str(); - } } return VariableName; } SourceRange MemRegion::sourceRange() const { - const VarRegion *const VR = dyn_cast(this->getBaseRegion()); - const FieldRegion *const FR = dyn_cast(this); + const auto *const VR = dyn_cast(this->getBaseRegion()); + const auto *const FR = dyn_cast(this); // Check for more specific regions first. // FieldRegion @@ -656,9 +682,8 @@ SourceRange MemRegion::sourceRange() const { return VR->getDecl()->getSourceRange(); } // Return invalid source range (can be checked by client). - else { - return SourceRange{}; - } + else + return {}; } //===----------------------------------------------------------------------===// @@ -748,13 +773,14 @@ const CodeSpaceRegion *MemRegionManager::getCodeRegion() { //===----------------------------------------------------------------------===// // Constructing regions. //===----------------------------------------------------------------------===// -const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){ + +const StringRegion *MemRegionManager::getStringRegion(const StringLiteral *Str){ return getSubRegion( Str, cast(getGlobalsRegion())); } const ObjCStringRegion * -MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){ +MemRegionManager::getObjCStringRegion(const ObjCStringLiteral *Str){ return getSubRegion( Str, cast(getGlobalsRegion())); } @@ -767,14 +793,13 @@ getStackOrCaptureRegionForDeclContext(const LocationContext *LC, const DeclContext *DC, const VarDecl *VD) { while (LC) { - if (const StackFrameContext *SFC = dyn_cast(LC)) { + if (const auto *SFC = dyn_cast(LC)) { if (cast(SFC->getDecl()) == DC) return SFC; } - if (const BlockInvocationContext *BC = - dyn_cast(LC)) { - const BlockDataRegion *BR = - static_cast(BC->getContextData()); + if (const auto *BC = dyn_cast(LC)) { + const auto *BR = + static_cast(BC->getContextData()); // FIXME: This can be made more efficient. for (BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(), @@ -828,7 +853,7 @@ const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D, if (V.is()) return V.get(); - const StackFrameContext *STC = V.get(); + const auto *STC = V.get(); if (!STC) { // FIXME: Assign a more sensible memory space to static locals @@ -846,7 +871,7 @@ const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D, if (isa(STCD) || isa(STCD)) sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, getFunctionCodeRegion(cast(STCD))); - else if (const BlockDecl *BD = dyn_cast(STCD)) { + else if (const auto *BD = dyn_cast(STCD)) { // FIXME: The fallback type here is totally bogus -- though it should // never be queried, it will prevent uniquing with the real // BlockCodeRegion. Ideally we'd fix the AST so that we always had a @@ -942,7 +967,7 @@ MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx, void *InsertPos; MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); - ElementRegion* R = cast_or_null(data); + auto *R = cast_or_null(data); if (!R) { R = A.Allocate(); @@ -964,7 +989,6 @@ MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy, return getSubRegion(BD, locTy, AC, getCodeRegion()); } - /// getSymbolicRegion - Retrieve or create a "symbolic" memory region. const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) { return getSubRegion(sym, getUnknownRegion()); @@ -1027,10 +1051,8 @@ MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD, if (IsVirtual) { // Virtual base regions should not be layered, since the layout rules // are different. - while (const CXXBaseObjectRegion *Base = - dyn_cast(Super)) { + while (const auto *Base = dyn_cast(Super)) Super = cast(Base->getSuperRegion()); - } assert(Super && !isa(Super)); } } @@ -1041,7 +1063,7 @@ MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD, const CXXThisRegion* MemRegionManager::getCXXThisRegion(QualType thisPointerTy, const LocationContext *LC) { - const PointerType *PT = thisPointerTy->getAs(); + const auto *PT = thisPointerTy->getAs(); assert(PT); // Inside the body of the operator() of a lambda a this expr might refer to an // object in one of the parent location contexts. @@ -1070,7 +1092,7 @@ MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt, const MemSpaceRegion *MemRegion::getMemorySpace() const { const MemRegion *R = this; - const SubRegion* SR = dyn_cast(this); + const auto *SR = dyn_cast(this); while (SR) { R = SR->getSuperRegion(); @@ -1131,7 +1153,7 @@ const MemRegion *MemRegion::StripCasts(bool StripBaseCasts) const { while (true) { switch (R->getKind()) { case ElementRegionKind: { - const ElementRegion *ER = cast(R); + const auto *ER = cast(R); if (!ER->getIndex().isZeroConstant()) return R; R = ER->getSuperRegion(); @@ -1149,10 +1171,10 @@ const MemRegion *MemRegion::StripCasts(bool StripBaseCasts) const { } const SymbolicRegion *MemRegion::getSymbolicBase() const { - const SubRegion *SubR = dyn_cast(this); + const auto *SubR = dyn_cast(this); while (SubR) { - if (const SymbolicRegion *SymR = dyn_cast(SubR)) + if (const auto *SymR = dyn_cast(SubR)) return SymR; SubR = dyn_cast(SubR->getSuperRegion()); } @@ -1243,7 +1265,6 @@ RegionRawOffset ElementRegion::getAsArrayOffset() const { return RegionRawOffset(superR, offset); } - /// Returns true if \p Base is an immediate base class of \p Child static bool isImmediateBase(const CXXRecordDecl *Child, const CXXRecordDecl *Base) { @@ -1263,7 +1284,7 @@ static RegionOffset calculateOffset(const MemRegion *R) { const MemRegion *SymbolicOffsetBase = nullptr; int64_t Offset = 0; - while (1) { + while (true) { switch (R->getKind()) { case MemRegion::CodeSpaceRegionKind: case MemRegion::StackLocalsSpaceRegionKind: @@ -1307,14 +1328,14 @@ static RegionOffset calculateOffset(const MemRegion *R) { goto Finish; case MemRegion::CXXBaseObjectRegionKind: { - const CXXBaseObjectRegion *BOR = cast(R); + const auto *BOR = cast(R); R = BOR->getSuperRegion(); QualType Ty; bool RootIsSymbolic = false; - if (const TypedValueRegion *TVR = dyn_cast(R)) { + if (const auto *TVR = dyn_cast(R)) { Ty = TVR->getDesugaredValueType(R->getContext()); - } else if (const SymbolicRegion *SR = dyn_cast(R)) { + } else if (const auto *SR = dyn_cast(R)) { // If our base region is symbolic, we don't know what type it really is. // Pretend the type of the symbol is the true dynamic type. // (This will at least be self-consistent for the life of the symbol.) @@ -1358,7 +1379,7 @@ static RegionOffset calculateOffset(const MemRegion *R) { break; } case MemRegion::ElementRegionKind: { - const ElementRegion *ER = cast(R); + const auto *ER = cast(R); R = ER->getSuperRegion(); QualType EleTy = ER->getValueType(); @@ -1386,7 +1407,7 @@ static RegionOffset calculateOffset(const MemRegion *R) { break; } case MemRegion::FieldRegionKind: { - const FieldRegion *FR = cast(R); + const auto *FR = cast(R); R = FR->getSuperRegion(); const RecordDecl *RD = FR->getDecl()->getParent(); @@ -1476,13 +1497,14 @@ void BlockDataRegion::LazyInitializeReferencedVars() { llvm::BumpPtrAllocator &A = MemMgr.getAllocator(); BumpVectorContext BC(A); - typedef BumpVector VarVec; - VarVec *BV = A.Allocate(); + using VarVec = BumpVector; + + auto *BV = A.Allocate(); new (BV) VarVec(BC, NumBlockVars); - VarVec *BVOriginal = A.Allocate(); + auto *BVOriginal = A.Allocate(); new (BVOriginal) VarVec(BC, NumBlockVars); - for (const VarDecl *VD : ReferencedBlockVars) { + for (const auto *VD : ReferencedBlockVars) { const VarRegion *VR = nullptr; const VarRegion *OriginalVR = nullptr; std::tie(VR, OriginalVR) = getCaptureRegions(VD); @@ -1500,14 +1522,13 @@ BlockDataRegion::referenced_vars_iterator BlockDataRegion::referenced_vars_begin() const { const_cast(this)->LazyInitializeReferencedVars(); - BumpVector *Vec = - static_cast*>(ReferencedVars); + auto *Vec = static_cast *>(ReferencedVars); if (Vec == (void*) 0x1) return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr); - BumpVector *VecOriginal = - static_cast*>(OriginalVars); + auto *VecOriginal = + static_cast *>(OriginalVars); return BlockDataRegion::referenced_vars_iterator(Vec->begin(), VecOriginal->begin()); @@ -1517,14 +1538,13 @@ BlockDataRegion::referenced_vars_iterator BlockDataRegion::referenced_vars_end() const { const_cast(this)->LazyInitializeReferencedVars(); - BumpVector *Vec = - static_cast*>(ReferencedVars); + auto *Vec = static_cast *>(ReferencedVars); if (Vec == (void*) 0x1) return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr); - BumpVector *VecOriginal = - static_cast*>(OriginalVars); + auto *VecOriginal = + static_cast *>(OriginalVars); return BlockDataRegion::referenced_vars_iterator(Vec->end(), VecOriginal->end()); @@ -1552,7 +1572,7 @@ void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym, void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR, InvalidationKinds IK) { assert(MR); - if (const SymbolicRegion *SR = dyn_cast(MR)) + if (const auto *SR = dyn_cast(MR)) setTrait(SR->getSymbol(), IK); else MRTraitsMap[MR] |= IK; @@ -1572,7 +1592,7 @@ bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR, if (!MR) return false; - if (const SymbolicRegion *SR = dyn_cast(MR)) + if (const auto *SR = dyn_cast(MR)) return hasTrait(SR->getSymbol(), IK); const_region_iterator I = MRTraitsMap.find(MR); -- 2.7.4