clang  14.0.0git
Public Member Functions | List of all members
clang::tooling::ClangTool Class Reference

Utility to run a FrontendAction over a set of files. More...

#include "clang/Tooling/Tooling.h"

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

Public Member Functions

 ClangTool (const CompilationDatabase &Compilations, ArrayRef< std::string > SourcePaths, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS=llvm::vfs::getRealFileSystem(), IntrusiveRefCntPtr< FileManager > Files=nullptr)
 Constructs a clang tool to run over a list of files. More...
 
 ~ClangTool ()
 
void setDiagnosticConsumer (DiagnosticConsumer *DiagConsumer)
 Set a DiagnosticConsumer to use during parsing. More...
 
void mapVirtualFile (StringRef FilePath, StringRef Content)
 Map a virtual file to be used while running the tool. More...
 
void appendArgumentsAdjuster (ArgumentsAdjuster Adjuster)
 Append a command line arguments adjuster to the adjuster chain. More...
 
void clearArgumentsAdjusters ()
 Clear the command line arguments adjuster chain. More...
 
int run (ToolAction *Action)
 Runs an action over all files specified in the command line. More...
 
int buildASTs (std::vector< std::unique_ptr< ASTUnit >> &ASTs)
 Create an AST for each file specified in the command line and append them to ASTs. More...
 
void setRestoreWorkingDir (bool RestoreCWD)
 Sets whether working directory should be restored after calling run(). More...
 
void setPrintErrorMessage (bool PrintErrorMessage)
 Sets whether an error message should be printed out if an action fails. More...
 
FileManagergetFiles ()
 Returns the file manager used in the tool. More...
 
llvm::ArrayRef< std::stringgetSourcePaths () const
 

Detailed 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.

Definition at line 309 of file Tooling.h.

Constructor & Destructor Documentation

◆ ClangTool()

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

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

Parameters
CompilationsThe CompilationDatabase which contains the compile command lines for the given source paths.
SourcePathsThe source files to run over. If a source files is not found in Compilations, it is skipped.
PCHContainerOpsThe PCHContainerOperations for loading and creating clang modules.
BaseFSVFS used for all underlying file accesses when running the tool.
FilesThe file manager to use for underlying file operations when running the tool.

Definition at line 429 of file Tooling.cpp.

References appendArgumentsAdjuster(), clang::tooling::getClangStripDependencyFileAdjuster(), clang::tooling::getClangStripOutputAdjuster(), and clang::tooling::getClangSyntaxOnlyAdjuster().

◆ ~ClangTool()

ClangTool::~ClangTool ( )
default

Member Function Documentation

◆ appendArgumentsAdjuster()

void ClangTool::appendArgumentsAdjuster ( ArgumentsAdjuster  Adjuster)

Append a command line arguments adjuster to the adjuster chain.

Parameters
AdjusterAn argument adjuster, which will be run on the output of previous argument adjusters.

Definition at line 454 of file Tooling.cpp.

References clang::tooling::combineAdjusters().

Referenced by ClangTool(), and clang::tooling::StandaloneToolExecutor::execute().

◆ buildASTs()

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

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

Definition at line 623 of file Tooling.cpp.

References run().

◆ clearArgumentsAdjusters()

void ClangTool::clearArgumentsAdjusters ( )

Clear the command line arguments adjuster chain.

Definition at line 458 of file Tooling.cpp.

◆ getFiles()

FileManager& clang::tooling::ClangTool::getFiles ( )
inline

Returns the file manager used in the tool.

The file manager is shared between all translation units.

Definition at line 377 of file Tooling.h.

Referenced by clang::tooling::StandaloneToolExecutor::getFiles(), and clang::tooling::RefactoringTool::runAndSave().

◆ getSourcePaths()

llvm::ArrayRef<std::string> clang::tooling::ClangTool::getSourcePaths ( ) const
inline

Definition at line 379 of file Tooling.h.

Referenced by clang::tooling::StandaloneToolExecutor::getSourcePaths().

◆ mapVirtualFile()

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

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

Parameters
FilePathThe path at which the content will be mapped.
ContentA null terminated buffer of the file's content.

Definition at line 450 of file Tooling.cpp.

Referenced by clang::tooling::StandaloneToolExecutor::mapVirtualFile().

◆ run()

int ClangTool::run ( ToolAction Action)

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

Parameters
ActionTool 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.

Definition at line 475 of file Tooling.cpp.

References clang::tooling::CompileCommand::CommandLine, clang::tooling::CompileCommand::Directory, clang::File, clang::tooling::CompileCommand::Filename, clang::tooling::getAbsolutePath(), clang::tooling::CompilationDatabase::getCompileCommands(), injectResourceDir(), string(), and clang::threadSafety::sx::toString().

Referenced by buildASTs(), clang::tooling::StandaloneToolExecutor::execute(), and clang::tooling::RefactoringTool::runAndSave().

◆ setDiagnosticConsumer()

void clang::tooling::ClangTool::setDiagnosticConsumer ( DiagnosticConsumer DiagConsumer)
inline

Set a DiagnosticConsumer to use during parsing.

Definition at line 334 of file Tooling.h.

Referenced by clang::tooling::StandaloneToolExecutor::setDiagnosticConsumer().

◆ setPrintErrorMessage()

void ClangTool::setPrintErrorMessage ( bool  PrintErrorMessage)

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.

Definition at line 632 of file Tooling.cpp.

◆ setRestoreWorkingDir()

void ClangTool::setRestoreWorkingDir ( bool  RestoreCWD)

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.

Definition at line 628 of file Tooling.cpp.


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