class ClangTool

Declaration

class ClangTool { /* full declaration omitted */ };

Description

Utility to run a FrontendAction over a set of files. This class is written to be usable for command line utilities. By default the class uses ClangSyntaxOnlyAdjuster to modify command line arguments before the arguments are used to run a frontend action. One could install an additional command line arguments adjuster by calling the appendArgumentsAdjuster() method.

Declared at: clang/include/clang/Tooling/Tooling.h:308

Member Variables

private const clang::tooling::CompilationDatabase& Compilations
private std::vector<std::string> SourcePaths
private std::shared_ptr<PCHContainerOperations> PCHContainerOps
private llvm::IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem> OverlayFileSystem
private llvm::IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem> InMemoryFileSystem
private llvm::IntrusiveRefCntPtr<FileManager> Files
private std::vector<std::pair<StringRef, StringRef>> MappedFileContents
private llvm::StringSet<> SeenWorkingDirectories
private clang::tooling::ArgumentsAdjuster ArgsAdjuster
private clang::DiagnosticConsumer* DiagConsumer = nullptr
private bool RestoreCWD = true
private bool PrintErrorMessage = true

Method Overview

  • public ClangTool(const clang::tooling::CompilationDatabase & Compilations, ArrayRef<std::string> SourcePaths, std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<clang::PCHContainerOperations>(), IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS = llvm::vfs::getRealFileSystem(), IntrusiveRefCntPtr<clang::FileManager> Files = nullptr)
  • public void appendArgumentsAdjuster(clang::tooling::ArgumentsAdjuster Adjuster)
  • public int buildASTs(std::vector<std::unique_ptr<ASTUnit>> & ASTs)
  • public void clearArgumentsAdjusters()
  • public clang::FileManager & getFiles()
  • public llvm::ArrayRef<std::string> getSourcePaths() const
  • public void mapVirtualFile(llvm::StringRef FilePath, llvm::StringRef Content)
  • public int run(clang::tooling::ToolAction * Action)
  • public void setDiagnosticConsumer(clang::DiagnosticConsumer * DiagConsumer)
  • public void setPrintErrorMessage(bool PrintErrorMessage)
  • public void setRestoreWorkingDir(bool RestoreCWD)
  • public ~ClangTool()

Methods

ClangTool(
    const clang::tooling::CompilationDatabase&
        Compilations,
    ArrayRef<std::string> SourcePaths,
    std::shared_ptr<PCHContainerOperations>
        PCHContainerOps = std::make_shared<
            clang::PCHContainerOperations>(),
    IntrusiveRefCntPtr<llvm::vfs::FileSystem>
        BaseFS = llvm::vfs::getRealFileSystem(),
    IntrusiveRefCntPtr<clang::FileManager> Files =
        nullptr)

Description

Constructs a clang tool to run over a list of files.

Declared at: clang/include/clang/Tooling/Tooling.h:322

Parameters

const clang::tooling::CompilationDatabase& Compilations
The CompilationDatabase which contains the compile command lines for the given source paths.
ArrayRef<std::string> SourcePaths
The source files to run over. If a source files is not found in Compilations, it is skipped.
std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<clang::PCHContainerOperations>()
The PCHContainerOperations for loading and creating clang modules.
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS = llvm::vfs::getRealFileSystem()
VFS used for all underlying file accesses when running the tool.
IntrusiveRefCntPtr<clang::FileManager> Files = nullptr
The file manager to use for underlying file operations when running the tool.

void appendArgumentsAdjuster(
    clang::tooling::ArgumentsAdjuster Adjuster)

Description

Append a command line arguments adjuster to the adjuster chain.

Declared at: clang/include/clang/Tooling/Tooling.h:347

Parameters

clang::tooling::ArgumentsAdjuster Adjuster
An argument adjuster, which will be run on the output of previous argument adjusters.

int buildASTs(
    std::vector<std::unique_ptr<ASTUnit>>& ASTs)

Description

Create an AST for each file specified in the command line and append them to ASTs.

Declared at: clang/include/clang/Tooling/Tooling.h:362

Parameters

std::vector<std::unique_ptr<ASTUnit>>& ASTs

void clearArgumentsAdjusters()

Description

Clear the command line arguments adjuster chain.

Declared at: clang/include/clang/Tooling/Tooling.h:350

clang::FileManager& getFiles()

Description

Returns the file manager used in the tool. The file manager is shared between all translation units.

Declared at: clang/include/clang/Tooling/Tooling.h:376

llvm::ArrayRef<std::string> getSourcePaths() const

Declared at: clang/include/clang/Tooling/Tooling.h:378

void mapVirtualFile(llvm::StringRef FilePath,
                    llvm::StringRef Content)

Description

Map a virtual file to be used while running the tool.

Declared at: clang/include/clang/Tooling/Tooling.h:341

Parameters

llvm::StringRef FilePath
The path at which the content will be mapped.
llvm::StringRef Content
A null terminated buffer of the file's content.

int run(clang::tooling::ToolAction* Action)

Description

Runs an action over all files specified in the command line.

Declared at: clang/include/clang/Tooling/Tooling.h:358

Parameters

clang::tooling::ToolAction* Action
Tool action.

Returns

0 on success; 1 if any error occurred; 2 if there is no error but some files are skipped due to missing compile commands.

void setDiagnosticConsumer(
    clang::DiagnosticConsumer* DiagConsumer)

Description

Set a \c DiagnosticConsumer to use during parsing.

Declared at: clang/include/clang/Tooling/Tooling.h:333

Parameters

clang::DiagnosticConsumer* DiagConsumer

void setPrintErrorMessage(bool PrintErrorMessage)

Description

Sets whether an error message should be printed out if an action fails. By default, if an action fails, a message is printed out to stderr.

Declared at: clang/include/clang/Tooling/Tooling.h:371

Parameters

bool PrintErrorMessage

void setRestoreWorkingDir(bool RestoreCWD)

Description

Sets whether working directory should be restored after calling run(). By default, working directory is restored. However, it could be useful to turn this off when running on multiple threads to avoid the raciness.

Declared at: clang/include/clang/Tooling/Tooling.h:367

Parameters

bool RestoreCWD

~ClangTool()

Declared at: clang/include/clang/Tooling/Tooling.h:330