Skip to content
This repository was archived by the owner on Mar 30, 2021. It is now read-only.

Commit 672c1c4

Browse files
gamesh411gamesh411
authored andcommitted
Add CTU On-Demand analysis support
Add an option to enable on-demand parsing of needed ASTs during CTU analysis, and another option to specify the compilation database used. The option CTUOnDemandParsing is a boolean flag, which enables the new AST-loading mode. Option CTUOnDemandParsingDatabase is a string, which should be the path of the compilation database used for on-demand parsing. The compilation database is needed for on-demand mode, because it has all the necessary information to generate the ASTs. Please enter the commit message for your changes. Lines starting
1 parent e2504ca commit 672c1c4

18 files changed

+455
-56
lines changed

include/clang/CrossTU/CrossTranslationUnit.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class FunctionDecl;
3232
class NamedDecl;
3333
class TranslationUnitDecl;
3434

35+
namespace tooling {
36+
class JSONCompilationDatabase;
37+
}
38+
3539
namespace cross_tu {
3640

3741
enum class index_error_code {
@@ -41,12 +45,14 @@ enum class index_error_code {
4145
multiple_definitions,
4246
missing_definition,
4347
failed_import,
48+
failed_to_load_compilation_database,
4449
failed_to_get_external_ast,
4550
failed_to_generate_usr,
4651
triple_mismatch,
4752
lang_mismatch,
4853
lang_dialect_mismatch,
49-
load_threshold_reached
54+
load_threshold_reached,
55+
ambiguous_compile_commands_database
5056
};
5157

5258
class IndexError : public llvm::ErrorInfo<IndexError> {
@@ -85,7 +91,8 @@ class IndexError : public llvm::ErrorInfo<IndexError> {
8591
/// \return Returns a map where the USR is the key and the filepath is the value
8692
/// or an error.
8793
llvm::Expected<llvm::StringMap<std::string>>
88-
parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir);
94+
parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir,
95+
llvm::Optional<StringRef> OnDemandParsingDatabase);
8996

9097
std::string createCrossTUIndexString(const llvm::StringMap<std::string> &Index);
9198

@@ -125,7 +132,8 @@ class CrossTranslationUnitContext {
125132
llvm::Expected<const FunctionDecl *>
126133
getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
127134
StringRef IndexName, bool DisplayCTUProgress,
128-
unsigned CTULoadThreshold);
135+
unsigned CTULoadThreshold,
136+
llvm::Optional<StringRef> OnDemandParsingDatabase);
129137

130138
/// This function loads a function definition from an external AST
131139
/// file.
@@ -141,11 +149,11 @@ class CrossTranslationUnitContext {
141149
/// The returned pointer is never a nullptr.
142150
///
143151
/// Note that the AST files should also be in the \p CrossTUDir.
144-
llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
145-
StringRef CrossTUDir,
146-
StringRef IndexName,
147-
bool DisplayCTUProgress,
148-
unsigned CTULoadThreshold);
152+
llvm::Expected<ASTUnit *>
153+
loadExternalAST(StringRef LookupName, StringRef CrossTUDir,
154+
StringRef IndexName, bool DisplayCTUProgress,
155+
unsigned CTULoadThreshold,
156+
llvm::Optional<StringRef> OnDemandParsingDatabase);
149157

150158
/// This function merges a definition from a separate AST Unit into
151159
/// the current one which was created by the compiler instance that
@@ -172,6 +180,11 @@ class CrossTranslationUnitContext {
172180
GetImportedFromSourceLocation(const clang::SourceLocation &ToLoc) const;
173181

174182
private:
183+
llvm::Expected<std::unique_ptr<ASTUnit>>
184+
loadASTFromDump(StringRef ASTSourcePath) const;
185+
llvm::Expected<std::unique_ptr<ASTUnit>>
186+
loadASTOnDemand(StringRef ASTSourcePath) const;
187+
llvm::Error lazyInitCompileCommands(StringRef CompileCommandsFile);
175188
void lazyInitImporterSharedSt(TranslationUnitDecl *ToTU);
176189
ASTImporter &getOrCreateASTImporter(ASTUnit *Unit);
177190
const FunctionDecl *findFunctionInDeclContext(const DeclContext *DC,
@@ -190,6 +203,9 @@ class CrossTranslationUnitContext {
190203
ASTContext &Context;
191204
std::shared_ptr<ASTImporterSharedState> ImporterSharedSt;
192205
unsigned NumASTLoaded{0u};
206+
/// In case of on-demand parsing, the compilation database is parsed and
207+
/// stored.
208+
std::unique_ptr<tooling::JSONCompilationDatabase> CompileCommands;
193209
};
194210

195211
} // namespace cross_tu

include/clang/StaticAnalyzer/Core/AnalyzerOptions.def

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ ANALYZER_OPTION(StringRef, CTUIndexName, "ctu-index-name",
355355
"the name of the file containing the CTU index of definitions.",
356356
"externalDefMap.txt")
357357

358+
ANALYZER_OPTION(bool, CTUOnDemandParsing, "ctu-on-demand-parsing",
359+
"Whether to parse function definitions from external TUs in "
360+
"an on-demand manner during analysis. When using on-demand "
361+
"parsing there is no need for pre-dumping ASTs. External "
362+
"definition mapping is still needed, and a valid compilation "
363+
"database with compile commands for the external TUs is also "
364+
"necessary. Disabled by default.",
365+
false)
366+
367+
ANALYZER_OPTION(StringRef, CTUOnDemandParsingDatabase,
368+
"ctu-on-demand-parsing-database",
369+
"The path to the compilation database used for on-demand "
370+
"parsing of ASTs during CTU analysis.",
371+
"compile_commands.json")
372+
358373
ANALYZER_OPTION(
359374
StringRef, ModelPath, "model-path",
360375
"The analyzer can inline an alternative implementation written in C at the "

lib/CrossTU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ add_clang_library(clangCrossTU
1010
clangBasic
1111
clangFrontend
1212
clangIndex
13+
clangTooling
1314
)

0 commit comments

Comments
 (0)