clang 20.0.0git
Public Member Functions | Static Public Member Functions | List of all members
clang::tooling::CompilationDatabase Class Referenceabstract

Interface for compilation databases. More...

#include "clang/Tooling/CompilationDatabase.h"

Inheritance diagram for clang::tooling::CompilationDatabase:
Inheritance graph
[legend]

Public Member Functions

virtual ~CompilationDatabase ()
 
virtual std::vector< CompileCommandgetCompileCommands (StringRef FilePath) const =0
 Returns all compile commands in which the specified file was compiled.
 
virtual std::vector< std::string > getAllFiles () const
 Returns the list of all files available in the compilation database.
 
virtual std::vector< CompileCommandgetAllCompileCommands () const
 Returns all compile commands for all the files in the compilation database.
 

Static Public Member Functions

static std::unique_ptr< CompilationDatabaseloadFromDirectory (StringRef BuildDirectory, std::string &ErrorMessage)
 Loads a compilation database from a build directory.
 
static std::unique_ptr< CompilationDatabaseautoDetectFromSource (StringRef SourceFile, std::string &ErrorMessage)
 Tries to detect a compilation database location and load it.
 
static std::unique_ptr< CompilationDatabaseautoDetectFromDirectory (StringRef SourceDir, std::string &ErrorMessage)
 Tries to detect a compilation database location and load it.
 

Detailed Description

Interface for compilation databases.

A compilation database allows the user to retrieve compile command lines for the files in a project.

Many implementations are enumerable, allowing all command lines to be retrieved. These can be used to run clang tools over a subset of the files in a project.

Definition at line 87 of file CompilationDatabase.h.

Constructor & Destructor Documentation

◆ ~CompilationDatabase()

CompilationDatabase::~CompilationDatabase ( )
virtualdefault

Member Function Documentation

◆ autoDetectFromDirectory()

std::unique_ptr< CompilationDatabase > CompilationDatabase::autoDetectFromDirectory ( StringRef  SourceDir,
std::string &  ErrorMessage 
)
static

Tries to detect a compilation database location and load it.

Looks for a compilation database in directory 'SourceDir' and all its parent paths by calling loadFromDirectory.

Definition at line 119 of file CompilationDatabase.cpp.

References findCompilationDatabaseFromDirectory(), and clang::tooling::getAbsolutePath().

◆ autoDetectFromSource()

std::unique_ptr< CompilationDatabase > CompilationDatabase::autoDetectFromSource ( StringRef  SourceFile,
std::string &  ErrorMessage 
)
static

Tries to detect a compilation database location and load it.

Looks for a compilation database in all parent paths of file 'SourceFile' by calling loadFromDirectory.

Definition at line 104 of file CompilationDatabase.cpp.

References findCompilationDatabaseFromDirectory(), and clang::tooling::getAbsolutePath().

◆ getAllCompileCommands()

std::vector< CompileCommand > CompilationDatabase::getAllCompileCommands ( ) const
virtual

Returns all compile commands for all the files in the compilation database.

FIXME: Add a layer in Tooling that provides an interface to run a tool over all files in a compilation database. Not all build systems have the ability to provide a feasible implementation for getAllCompileCommands.

By default, this is implemented in terms of getAllFiles() and getCompileCommands(). Subclasses may override this for efficiency.

Reimplemented in clang::tooling::ArgumentsAdjustingCompilations, and clang::tooling::JSONCompilationDatabase.

Definition at line 132 of file CompilationDatabase.cpp.

References clang::C, clang::File, getAllFiles(), getCompileCommands(), and clang::Result.

◆ getAllFiles()

virtual std::vector< std::string > clang::tooling::CompilationDatabase::getAllFiles ( ) const
inlinevirtual

Returns the list of all files available in the compilation database.

By default, returns nothing. Implementations should override this if they can enumerate their source files.

Reimplemented in clang::tooling::ArgumentsAdjustingCompilations, and clang::tooling::JSONCompilationDatabase.

Definition at line 136 of file CompilationDatabase.h.

Referenced by clang::tooling::AllTUsToolExecutor::execute(), and getAllCompileCommands().

◆ getCompileCommands()

virtual std::vector< CompileCommand > clang::tooling::CompilationDatabase::getCompileCommands ( StringRef  FilePath) const
pure virtual

Returns all compile commands in which the specified file was compiled.

This includes compile commands that span multiple source files. For example, consider a project with the following compilations: $ clang++ -o test a.cc b.cc t.cc $ clang++ -o production a.cc b.cc -DPRODUCTION A compilation database representing the project would return both command lines for a.cc and b.cc and only the first command line for t.cc.

Implemented in clang::tooling::ArgumentsAdjustingCompilations, clang::tooling::FixedCompilationDatabase, and clang::tooling::JSONCompilationDatabase.

Referenced by getAllCompileCommands(), and clang::tooling::ClangTool::run().

◆ loadFromDirectory()

std::unique_ptr< CompilationDatabase > CompilationDatabase::loadFromDirectory ( StringRef  BuildDirectory,
std::string &  ErrorMessage 
)
static

Loads a compilation database from a build directory.

Looks at the specified 'BuildDirectory' and creates a compilation database that allows to query compile commands for source files in the corresponding source tree.

Returns NULL and sets ErrorMessage if we were not able to build up a compilation database for the build directory.

FIXME: Currently only supports JSON compilation databases, which are named 'compile_commands.json' in the given directory. Extend this for other build types (like ninja build files).

Definition at line 64 of file CompilationDatabase.cpp.

Referenced by findCompilationDatabaseFromDirectory().


The documentation for this class was generated from the following files: