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

Add On-the-fly analysis support #694

Open
wants to merge 1 commit into
base: ctu-clang801
Choose a base branch
from
Open
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
32 changes: 24 additions & 8 deletions include/clang/CrossTU/CrossTranslationUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class FunctionDecl;
class NamedDecl;
class TranslationUnitDecl;

namespace tooling {
class JSONCompilationDatabase;
}

namespace cross_tu {

enum class index_error_code {
Expand All @@ -41,12 +45,14 @@ enum class index_error_code {
multiple_definitions,
missing_definition,
failed_import,
failed_to_load_compilation_database,
failed_to_get_external_ast,
failed_to_generate_usr,
triple_mismatch,
lang_mismatch,
lang_dialect_mismatch,
load_threshold_reached
load_threshold_reached,
ambiguous_compile_commands_database
};

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

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

Expand Down Expand Up @@ -125,7 +132,8 @@ class CrossTranslationUnitContext {
llvm::Expected<const FunctionDecl *>
getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir,
StringRef IndexName, bool DisplayCTUProgress,
unsigned CTULoadThreshold);
unsigned CTULoadThreshold,
llvm::Optional<StringRef> OnDemandParsingDatabase);

/// This function loads a function definition from an external AST
/// file.
Expand All @@ -141,11 +149,11 @@ class CrossTranslationUnitContext {
/// The returned pointer is never a nullptr.
///
/// Note that the AST files should also be in the \p CrossTUDir.
llvm::Expected<ASTUnit *> loadExternalAST(StringRef LookupName,
StringRef CrossTUDir,
StringRef IndexName,
bool DisplayCTUProgress,
unsigned CTULoadThreshold);
llvm::Expected<ASTUnit *>
loadExternalAST(StringRef LookupName, StringRef CrossTUDir,
StringRef IndexName, bool DisplayCTUProgress,
unsigned CTULoadThreshold,
llvm::Optional<StringRef> OnDemandParsingDatabase);

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

private:
llvm::Expected<std::unique_ptr<ASTUnit>>
loadASTFromDump(StringRef ASTSourcePath) const;
llvm::Expected<std::unique_ptr<ASTUnit>>
loadASTOnDemand(StringRef ASTSourcePath) const;
llvm::Error lazyInitCompileCommands(StringRef CompileCommandsFile);
void lazyInitImporterSharedSt(TranslationUnitDecl *ToTU);
ASTImporter &getOrCreateASTImporter(ASTUnit *Unit);
const FunctionDecl *findFunctionInDeclContext(const DeclContext *DC,
Expand All @@ -190,6 +203,9 @@ class CrossTranslationUnitContext {
ASTContext &Context;
std::shared_ptr<ASTImporterSharedState> ImporterSharedSt;
unsigned NumASTLoaded{0u};
/// In case of on-demand parsing, the compilation database is parsed and
/// stored.
std::unique_ptr<tooling::JSONCompilationDatabase> CompileCommands;
};

} // namespace cross_tu
Expand Down
15 changes: 15 additions & 0 deletions include/clang/StaticAnalyzer/Core/AnalyzerOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ ANALYZER_OPTION(StringRef, CTUIndexName, "ctu-index-name",
"the name of the file containing the CTU index of definitions.",
"externalDefMap.txt")

ANALYZER_OPTION(bool, CTUOnDemandParsing, "ctu-on-demand-parsing",
"Whether to parse function definitions from external TUs in "
"an on-demand manner during analysis. When using on-demand "
"parsing there is no need for pre-dumping ASTs. External "
"definition mapping is still needed, and a valid compilation "
"database with compile commands for the external TUs is also "
"necessary. Disabled by default.",
false)

ANALYZER_OPTION(StringRef, CTUOnDemandParsingDatabase,
"ctu-on-demand-parsing-database",
"The path to the compilation database used for on-demand "
"parsing of ASTs during CTU analysis.",
"compile_commands.json")

ANALYZER_OPTION(
StringRef, ModelPath, "model-path",
"The analyzer can inline an alternative implementation written in C at the "
Expand Down
1 change: 1 addition & 0 deletions lib/CrossTU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ add_clang_library(clangCrossTU
clangBasic
clangFrontend
clangIndex
clangTooling
)
Loading