r367829 - [CrossTU][NFCI] Refactor loadExternalAST function

2019-08-05 Thread Endre Fulop via cfe-commits
Author: gamesh411
Date: Mon Aug  5 04:06:41 2019
New Revision: 367829

URL: http://llvm.org/viewvc/llvm-project?rev=367829&view=rev
Log:
[CrossTU][NFCI] Refactor loadExternalAST function

Summary:
Refactor loadExternalAST method of CrossTranslationUnitContext in order to
reduce maintenance burden and so that features are easier to add in the future.

Reviewers: martong

Subscribers: rnkovacs, dkrupp, Szelethus, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64753

Modified:
cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h
cfe/trunk/lib/CrossTU/CrossTranslationUnit.cpp

Modified: cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h?rev=367829&r1=367828&r2=367829&view=diff
==
--- cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h (original)
+++ cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h Mon Aug  5 04:06:41 
2019
@@ -192,11 +192,11 @@ private:
   template 
   llvm::Expected importDefinitionImpl(const T *D, ASTUnit *Unit);
 
-  llvm::StringMap> FileASTUnitMap;
-  llvm::StringMap NameASTUnitMap;
-  llvm::StringMap NameFileMap;
-  llvm::DenseMap>
-  ASTUnitImporterMap;
+  using ImporterMapTy =
+  llvm::DenseMap>;
+
+  ImporterMapTy ASTUnitImporterMap;
+
   CompilerInstance &CI;
   ASTContext &Context;
   std::shared_ptr ImporterSharedSt;
@@ -209,10 +209,105 @@ private:
   /// imported the FileID.
   ImportedFileIDMap ImportedFileIDs;
 
+  /// Functor for loading ASTUnits from AST-dump files.
+  class ASTFileLoader {
+  public:
+ASTFileLoader(const CompilerInstance &CI);
+std::unique_ptr operator()(StringRef ASTFilePath);
+
+  private:
+const CompilerInstance &CI;
+  };
+
+  /// Storage for ASTUnits, cached access, and providing searchability are the
+  /// concerns of ASTUnitStorage class.
+  class ASTUnitStorage {
+  public:
+ASTUnitStorage(const CompilerInstance &CI);
+/// Loads an ASTUnit for a function.
+///
+/// \param FuncitionName USR name of the function.
+/// \param CrossTUDir Path to the directory used to store CTU related 
files.
+/// \param IndexName Name of the file inside \p CrossTUDir which maps
+/// function USR names to file paths. These files contain the corresponding
+/// AST-dumps.
+///
+/// \return An Expected instance which contains the ASTUnit pointer or the
+/// error occured during the load.
+llvm::Expected getASTUnitForFunction(StringRef FunctionName,
+StringRef CrossTUDir,
+StringRef IndexName);
+/// Identifies the path of the file which can be used to load the ASTUnit
+/// for a given function.
+///
+/// \param FuncitionName USR name of the function.
+/// \param CrossTUDir Path to the directory used to store CTU related 
files.
+/// \param IndexName Name of the file inside \p CrossTUDir which maps
+/// function USR names to file paths. These files contain the corresponding
+/// AST-dumps.
+///
+/// \return An Expected instance containing the filepath.
+llvm::Expected getFileForFunction(StringRef FunctionName,
+   StringRef CrossTUDir,
+   StringRef IndexName);
+
+  private:
+llvm::Error ensureCTUIndexLoaded(StringRef CrossTUDir, StringRef 
IndexName);
+llvm::Expected getASTUnitForFile(StringRef FileName);
+
+template  using BaseMapTy = llvm::StringMap;
+using OwningMapTy = BaseMapTy>;
+using NonOwningMapTy = BaseMapTy;
+
+OwningMapTy FileASTUnitMap;
+NonOwningMapTy NameASTUnitMap;
+
+using IndexMapTy = BaseMapTy;
+IndexMapTy NameFileMap;
+
+ASTFileLoader FileAccessor;
+  };
+
+  ASTUnitStorage ASTStorage;
+
   /// \p CTULoadTreshold should serve as an upper limit to the number of TUs
   /// imported in order to reduce the memory footprint of CTU analysis.
   const unsigned CTULoadThreshold;
-  unsigned NumASTLoaded{0u};
+
+  /// The number successfully loaded ASTs. Used to indicate, and  - with the
+  /// appropriate threshold value - limit the  memory usage of the
+  /// CrossTranslationUnitContext.
+  unsigned NumASTLoaded;
+
+  /// RAII counter to signal 'threshold reached' condition, and to increment 
the
+  /// NumASTLoaded counter upon a successful load.
+  class LoadGuard {
+  public:
+LoadGuard(unsigned Limit, unsigned &Counter)
+: Counter(Counter), Enabled(Counter < Limit){};
+~LoadGuard() {
+  if (StoreSuccess)
+++Counter;
+}
+/// Flag the LoadGuard instance as successful, meaning that the load
+/// operation succeeded, and the memory footprint of the AST storage
+/// actually increased. In this case, \p Counter should be incremented upon
+/// destructi

r365189 - [NFC] Test commit access

2019-07-05 Thread Endre Fulop via cfe-commits
Author: gamesh411
Date: Fri Jul  5 05:00:52 2019
New Revision: 365189

URL: http://llvm.org/viewvc/llvm-project?rev=365189&view=rev
Log:
[NFC] Test commit access

Modified:
cfe/trunk/include/clang/AST/ASTImporter.h

Modified: cfe/trunk/include/clang/AST/ASTImporter.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTImporter.h?rev=365189&r1=365188&r2=365189&view=diff
==
--- cfe/trunk/include/clang/AST/ASTImporter.h (original)
+++ cfe/trunk/include/clang/AST/ASTImporter.h Fri Jul  5 05:00:52 2019
@@ -201,7 +201,7 @@ class TypeSourceInfo;
   }
 
 private:
-  // All the nodes of the path.
+  // All nodes of the path.
   VecTy Nodes;
   // Auxiliary container to be able to answer "Do we have a cycle ending
   // at last element?" as fast as possible.


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


r365314 - [analyzer] Add analyzer option to limit the number of imported TUs

2019-07-08 Thread Endre Fulop via cfe-commits
Author: gamesh411
Date: Mon Jul  8 05:37:10 2019
New Revision: 365314

URL: http://llvm.org/viewvc/llvm-project?rev=365314&view=rev
Log:
[analyzer] Add analyzer option to limit the number of imported TUs

Summary:
During CTU analysis of complex projects, the loaded AST-contents of
imported TUs can grow bigger than available system memory. This option
introduces a threshold on the number of TUs to be imported for a single
TU in order to prevent such cases.

Differential Revision: https://reviews.llvm.org/D59798

Added:
cfe/trunk/test/Analysis/ctu-import-threshold.c
Modified:
cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h
cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def
cfe/trunk/lib/CrossTU/CrossTranslationUnit.cpp
cfe/trunk/test/Analysis/analyzer-config.c
cfe/trunk/unittests/CrossTU/CrossTranslationUnitTest.cpp

Modified: cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h?rev=365314&r1=365313&r2=365314&view=diff
==
--- cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h (original)
+++ cfe/trunk/include/clang/CrossTU/CrossTranslationUnit.h Mon Jul  8 05:37:10 
2019
@@ -45,7 +45,8 @@ enum class index_error_code {
   failed_to_generate_usr,
   triple_mismatch,
   lang_mismatch,
-  lang_dialect_mismatch
+  lang_dialect_mismatch,
+  load_threshold_reached
 };
 
 class IndexError : public llvm::ErrorInfo {
@@ -134,7 +135,8 @@ public:
   /// A definition with the same declaration will be looked up in the
   /// index file which should be in the \p CrossTUDir directory, called
   /// \p IndexName. In case the declaration is found in the index the
-  /// corresponding AST file will be loaded.
+  /// corresponding AST file will be loaded. If the number of TUs imported
+  /// reaches \p CTULoadTreshold, no loading is performed.
   ///
   /// \return Returns a pointer to the ASTUnit that contains the definition of
   /// the looked up name or an Error.
@@ -182,6 +184,10 @@ private:
   CompilerInstance &CI;
   ASTContext &Context;
   std::shared_ptr ImporterSharedSt;
+  /// \p CTULoadTreshold should serve as an upper limit to the number of TUs
+  /// imported in order to reduce the memory footprint of CTU analysis.
+  const unsigned CTULoadThreshold;
+  unsigned NumASTLoaded{0u};
 };
 
 } // namespace cross_tu

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def?rev=365314&r1=365313&r2=365314&view=diff
==
--- cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def Mon Jul  8 
05:37:10 2019
@@ -300,6 +300,14 @@ ANALYZER_OPTION(bool, ShouldTrackConditi
 "Whether to place an event at each tracked condition.",
 false)
 
+ANALYZER_OPTION(unsigned, CTUImportThreshold, "ctu-import-threshold",
+"The maximal amount of translation units that is considered "
+"for import when inlining functions during CTU analysis. "
+"Lowering this threshold can alleviate the memory burder of "
+"analysis with many interdependent definitions located in "
+"various translation units.",
+100u)
+
 
//===--===//
 // Unsinged analyzer options.
 
//===--===//

Modified: cfe/trunk/lib/CrossTU/CrossTranslationUnit.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CrossTU/CrossTranslationUnit.cpp?rev=365314&r1=365313&r2=365314&view=diff
==
--- cfe/trunk/lib/CrossTU/CrossTranslationUnit.cpp (original)
+++ cfe/trunk/lib/CrossTU/CrossTranslationUnit.cpp Mon Jul  8 05:37:10 2019
@@ -47,6 +47,8 @@ STATISTIC(NumNameConflicts, "The # of im
 STATISTIC(NumTripleMismatch, "The # of triple mismatches");
 STATISTIC(NumLangMismatch, "The # of language mismatches");
 STATISTIC(NumLangDialectMismatch, "The # of language dialect mismatches");
+STATISTIC(NumASTLoadThresholdReached,
+  "The # of ASTs not loaded because of threshold");
 
 // Same as Triple's equality operator, but we check a field only if that is
 // known in both instances.
@@ -106,6 +108,8 @@ public:
   return "Language mismatch";
 case index_error_code::lang_dialect_mismatch:
   return "Language dialect mismatch";
+case index_error_code::load_threshold_reached:
+  return "Load threshold reached";
 }
 llvm_unreachable("Unrecognized index_error_code.");
   }
@@ -184,7 +188,8 @@ template  static bool hasBod
 }
 
 CrossT

[clang] 141cb8a - [analyzer] Model iterator random incrementation symmetrically

2020-08-04 Thread Endre Fulop via cfe-commits

Author: Endre Fülöp
Date: 2020-08-04T11:04:12+02:00
New Revision: 141cb8a1eecc0c843cdd4e788a28d2b6715e4dc5

URL: 
https://github.com/llvm/llvm-project/commit/141cb8a1eecc0c843cdd4e788a28d2b6715e4dc5
DIFF: 
https://github.com/llvm/llvm-project/commit/141cb8a1eecc0c843cdd4e788a28d2b6715e4dc5.diff

LOG: [analyzer] Model iterator random incrementation symmetrically

Summary:
In case a pointer iterator is incremented in a binary plus expression
(operator+), where the iterator is on the RHS, IteratorModeling should
now detect, and track the resulting value.

Reviewers: Szelethus, baloghadamsoftware

Reviewed By: baloghadamsoftware

Subscribers: rnkovacs, whisperity, xazax.hun, baloghadamsoftware, szepet, 
a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, Charusso, steakhal, 
martong, ASDenysPetrov, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D83190

Added: 


Modified: 
clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp
clang/test/Analysis/iterator-modeling.cpp

Removed: 




diff  --git a/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp 
b/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp
index 632de9e5dc83..ab5e6a1c9991 100644
--- a/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp
@@ -109,7 +109,7 @@ class IteratorModeling
bool Postfix) const;
   void handleRandomIncrOrDecr(CheckerContext &C, const Expr *CE,
   OverloadedOperatorKind Op, const SVal &RetVal,
-  const SVal &LHS, const SVal &RHS) const;
+  const SVal &Iterator, const SVal &Amount) const;
   void handlePtrIncrOrDecr(CheckerContext &C, const Expr *Iterator,
OverloadedOperatorKind OK, SVal Offset) const;
   void handleAdvance(CheckerContext &C, const Expr *CE, SVal RetVal, SVal Iter,
@@ -262,20 +262,30 @@ void IteratorModeling::checkPostStmt(const UnaryOperator 
*UO,
 
 void IteratorModeling::checkPostStmt(const BinaryOperator *BO,
  CheckerContext &C) const {
-  ProgramStateRef State = C.getState();
-  BinaryOperatorKind OK = BO->getOpcode();
-  SVal RVal = State->getSVal(BO->getRHS(), C.getLocationContext());
+  const ProgramStateRef State = C.getState();
+  const BinaryOperatorKind OK = BO->getOpcode();
+  const Expr *const LHS = BO->getLHS();
+  const Expr *const RHS = BO->getRHS();
+  const SVal LVal = State->getSVal(LHS, C.getLocationContext());
+  const SVal RVal = State->getSVal(RHS, C.getLocationContext());
 
   if (isSimpleComparisonOperator(BO->getOpcode())) {
-SVal LVal = State->getSVal(BO->getLHS(), C.getLocationContext());
 SVal Result = State->getSVal(BO, C.getLocationContext());
 handleComparison(C, BO, Result, LVal, RVal,
  BinaryOperator::getOverloadedOperator(OK));
   } else if (isRandomIncrOrDecrOperator(OK)) {
-if (!BO->getRHS()->getType()->isIntegralOrEnumerationType())
+// In case of operator+ the iterator can be either on the LHS (eg.: it + 
1),
+// or on the RHS (eg.: 1 + it). Both cases are modeled.
+const bool IsIterOnLHS = BO->getLHS()->getType()->isPointerType();
+const Expr *const &IterExpr = IsIterOnLHS ? LHS : RHS;
+const Expr *const &AmountExpr = IsIterOnLHS ? RHS : LHS;
+
+// The non-iterator side must have an integral or enumeration type.
+if (!AmountExpr->getType()->isIntegralOrEnumerationType())
   return;
-handlePtrIncrOrDecr(C, BO->getLHS(),
-BinaryOperator::getOverloadedOperator(OK), RVal);
+const SVal &AmountVal = IsIterOnLHS ? RVal : LVal;
+handlePtrIncrOrDecr(C, IterExpr, BinaryOperator::getOverloadedOperator(OK),
+AmountVal);
   }
 }
 
@@ -368,11 +378,24 @@ IteratorModeling::handleOverloadedOperator(CheckerContext 
&C,
  InstCall->getCXXThisVal(), 
Call.getArgSVal(0));
   return;
 }
-  } else {
-if (Call.getNumArgs() >= 2 &&
-  Call.getArgExpr(1)->getType()->isIntegralOrEnumerationType()) {
+  } else if (Call.getNumArgs() >= 2) {
+const Expr *FirstArg = Call.getArgExpr(0);
+const Expr *SecondArg = Call.getArgExpr(1);
+const QualType FirstType = FirstArg->getType();
+const QualType SecondType = SecondArg->getType();
+
+if (FirstType->isIntegralOrEnumerationType() ||
+SecondType->isIntegralOrEnumerationType()) {
+  // In case of operator+ the iterator can be either on the LHS (eg.:
+  // it + 1), or on the RHS (eg.: 1 + it). Both cases are modeled.
+  const bool IsIterFirst = FirstType->isStructureOrClassType();
+  const SVal FirstArg = Call.getArgSVal(0);
+  const SVal SecondArg = Call.getArgSVal(1);
+  const SVal &Iterator =