clang API Documentation

Classes | Typedefs | Functions | Variables
clang::tooling Namespace Reference

Classes

class  ArgumentsAdjuster
 Abstract interface for a command line adjusters. More...
class  ClangSyntaxOnlyAdjuster
 Syntax check only command line adjuster. More...
class  ClangStripOutputAdjuster
 An argument adjuster which removes output-related command line arguments. More...
class  InsertArgumentAdjuster
class  CommonOptionsParser
 A parser for options common to all command-line Clang tools. More...
struct  CompileCommand
 Specifies the working directory and command of a compilation. More...
class  CompilationDatabase
 Interface for compilation databases. More...
class  CompilationDatabasePlugin
 Interface for compilation database plugins. More...
class  FixedCompilationDatabase
 A compilation database that returns a single compile command line. More...
class  Range
 A source range independent of the SourceManager. More...
class  Replacement
 A text replacement. More...
struct  TranslationUnitReplacements
 Collection of Replacements generated from a single translation unit. More...
struct  PathComparator
class  FileMatchTrie
 A trie to efficiently match against the entries of the compilation database in order of matching suffix length. More...
class  JSONCompilationDatabase
 A JSON based compilation database. More...
class  RefactoringTool
 A tool to run refactorings. More...
class  RefactoringCallback
 Base class for RefactoringCallbacks. More...
class  ReplaceStmtWithText
 Replace the text of the statement bound to FromId with the text in ToText. More...
class  ReplaceStmtWithStmt
 Replace the text of the statement bound to FromId with the text of the statement bound to ToId. More...
class  ReplaceIfStmtWithItsBody
 Replace an if-statement bound to Id with the outdented text of its body, choosing the consequent or the alternative based on whether PickTrueBranch is true. More...
class  ToolAction
 Interface to process a clang::CompilerInvocation. More...
class  FrontendActionFactory
 Interface to generate clang::FrontendActions. More...
class  SourceFileCallbacks
 Callbacks called before and after each source file processed by a FrontendAction created by the FrontedActionFactory returned by newFrontendActionFactory. More...
class  ToolInvocation
 Utility to run a FrontendAction in a single clang invocation. More...
class  ClangTool
 Utility to run a FrontendAction over a set of files. More...
struct  CompileJobAnalyzer
class  UnusedInputDiagConsumer
struct  MatchesAny
struct  DefaultPathComparator
 Default PathComparator using llvm::sys::fs::equivalent(). More...
class  FileMatchTrieNode
 A node of the FileMatchTrie. More...

Typedefs

typedef std::vector< std::string > CommandLineArguments
 A sequence of command line arguments.
typedef llvm::Registry
< CompilationDatabasePlugin
CompilationDatabasePluginRegistry
typedef std::set< ReplacementReplacements
 A set of Replacements. FIXME: Change to a vector and deduplicate in the RefactoringTool.

Functions

bool operator< (const Replacement &LHS, const Replacement &RHS)
 Less-than operator between two Replacements.
bool operator== (const Replacement &LHS, const Replacement &RHS)
 Equal-to operator between two Replacements.
bool applyAllReplacements (const Replacements &Replaces, Rewriter &Rewrite)
 Apply all replacements in Replaces to the Rewriter Rewrite.
bool applyAllReplacements (const std::vector< Replacement > &Replaces, Rewriter &Rewrite)
 Apply all replacements in Replaces to the Rewriter Rewrite.
std::string applyAllReplacements (StringRef Code, const Replacements &Replaces)
 Applies all replacements in Replaces to Code.
unsigned shiftedCodePosition (const Replacements &Replaces, unsigned Position)
 Calculates how a code Position is shifted when Replaces are applied.
unsigned shiftedCodePosition (const std::vector< Replacement > &Replaces, unsigned Position)
 Calculates how a code Position is shifted when Replaces are applied.
void deduplicate (std::vector< Replacement > &Replaces, std::vector< Range > &Conflicts)
 Removes duplicate Replacements and reports if Replacements conflict with one another. All Replacements are assumed to be in the same file.
template<typename T >
std::unique_ptr
< FrontendActionFactory
newFrontendActionFactory ()
 Returns a new FrontendActionFactory for a given type.
template<typename FactoryT >
std::unique_ptr
< FrontendActionFactory
newFrontendActionFactory (FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks=nullptr)
 Returns a new FrontendActionFactory for any type that provides an implementation of newASTConsumer().
bool runToolOnCode (clang::FrontendAction *ToolAction, const Twine &Code, const Twine &FileName="input.cc")
 Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
bool runToolOnCodeWithArgs (clang::FrontendAction *ToolAction, const Twine &Code, const std::vector< std::string > &Args, const Twine &FileName="input.cc")
 Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and with additional other flags.
std::unique_ptr< ASTUnitbuildASTFromCode (const Twine &Code, const Twine &FileName="input.cc")
 Builds an AST for 'Code'.
std::unique_ptr< ASTUnitbuildASTFromCodeWithArgs (const Twine &Code, const std::vector< std::string > &Args, const Twine &FileName="input.cc")
 Builds an AST for 'Code' with additional flags.
std::string getAbsolutePath (StringRef File)
 Returns the absolute path of File, by prepending it with the current directory if File is not absolute.
static std::unique_ptr
< CompilationDatabase
findCompilationDatabaseFromDirectory (StringRef Directory, std::string &ErrorMessage)
static bool stripPositionalArgs (std::vector< const char * > Args, std::vector< std::string > &Result)
 Strips any positional args and possible argv[0] from a command-line provided by the user to construct a FixedCompilationDatabase.
static int getRangeSize (const SourceManager &Sources, const CharSourceRange &Range)
static
CompilationDatabasePluginRegistry::Add
< JSONCompilationDatabasePlugin > 
X ("json-compilation-database","Reads JSON formatted compilation databases")
static Replacement replaceStmtWithText (SourceManager &Sources, const Stmt &From, StringRef Text)
static Replacement replaceStmtWithStmt (SourceManager &Sources, const Stmt &From, const Stmt &To)
static clang::driver::DrivernewDriver (clang::DiagnosticsEngine *Diagnostics, const char *BinaryName)
 Builds a clang driver initialized for running clang tools.
static const
llvm::opt::ArgStringList * 
getCC1Arguments (clang::DiagnosticsEngine *Diagnostics, clang::driver::Compilation *Compilation)
 Retrieves the clang CC1 specific flags out of the compilation's jobs.
static clang::CompilerInvocationnewInvocation (clang::DiagnosticsEngine *Diagnostics, const llvm::opt::ArgStringList &CC1Args)
 Returns a clang build invocation initialized from the CC1 flags.
static std::vector< std::string > getSyntaxOnlyToolArgs (const std::vector< std::string > &ExtraArgs, StringRef FileName)

Variables

volatile int JSONAnchorSource = 0
static int JSONAnchorDest = JSONAnchorSource
static const char *const InvalidLocation = ""

Typedef Documentation

typedef std::vector<std::string> clang::tooling::CommandLineArguments

A sequence of command line arguments.

Definition at line 27 of file ArgumentsAdjusters.h.

Definition at line 19 of file CompilationDatabasePluginRegistry.h.

A set of Replacements. FIXME: Change to a vector and deduplicate in the RefactoringTool.

Definition at line 136 of file Replacement.h.


Function Documentation

bool clang::tooling::applyAllReplacements ( const Replacements &  Replaces,
Rewriter &  Rewrite 
)

Apply all replacements in Replaces to the Rewriter Rewrite.

Replacement applications happen independently of the success of other applications.

Returns:
true if all replacements apply. false otherwise.

Definition at line 228 of file Replacement.cpp.

bool clang::tooling::applyAllReplacements ( const std::vector< Replacement > &  Replaces,
Rewriter &  Rewrite 
)

Apply all replacements in Replaces to the Rewriter Rewrite.

Replacement applications happen independently of the success of other applications.

Returns:
true if all replacements apply. false otherwise.

Definition at line 244 of file Replacement.cpp.

std::string clang::tooling::applyAllReplacements ( StringRef  Code,
const Replacements &  Replaces 
)

Applies all replacements in Replaces to Code.

This completely ignores the path stored in each replacement. If one or more replacements cannot be applied, this returns an empty string.

Definition at line 259 of file Replacement.cpp.

References clang::SrcMgr::C_User, clang::SourceManager::createFileID(), clang::Rewriter::getEditBuffer(), clang::FileManager::getVirtualFile(), clang::SourceManager::overrideFileContents(), SourceMgr, and clang::RewriteBuffer::write().

std::unique_ptr< ASTUnit > clang::tooling::buildASTFromCode ( const Twine &  Code,
const Twine &  FileName = "input.cc" 
)

Builds an AST for 'Code'.

Parameters:
CodeC++ code.
FileNameThe file name which 'Code' will be mapped as.
Returns:
The resulting AST or null if an error occurred.

Definition at line 398 of file Tooling.cpp.

References buildASTFromCodeWithArgs().

std::unique_ptr< ASTUnit > clang::tooling::buildASTFromCodeWithArgs ( const Twine &  Code,
const std::vector< std::string > &  Args,
const Twine &  FileName = "input.cc" 
)

Builds an AST for 'Code' with additional flags.

Parameters:
CodeC++ code.
ArgsAdditional flags to pass on.
FileNameThe file name which 'Code' will be mapped as.
Returns:
The resulting AST or null if an error occurred.

Definition at line 404 of file Tooling.cpp.

References Action, ASTs, getSyntaxOnlyToolArgs(), clang::tooling::ToolInvocation::mapVirtualFile(), and clang::tooling::ToolInvocation::run().

Referenced by buildASTFromCode().

void clang::tooling::deduplicate ( std::vector< Replacement > &  Replaces,
std::vector< Range > &  Conflicts 
)

Removes duplicate Replacements and reports if Replacements conflict with one another. All Replacements are assumed to be in the same file.

Postcondition:
Replaces[i].getOffset() <= Replaces[i+1].getOffset().

This function sorts Replaces so that conflicts can be reported simply by offset into Replaces and number of elements in the conflict.

Definition at line 176 of file Replacement.cpp.

References Current, clang::tooling::Range::getLength(), clang::tooling::Replacement::getLength(), clang::tooling::Range::getOffset(), clang::tooling::Replacement::getOffset(), and clang::tooling::Replacement::getReplacementText().

static std::unique_ptr<CompilationDatabase> clang::tooling::findCompilationDatabaseFromDirectory ( StringRef  Directory,
std::string &  ErrorMessage 
) [static]
std::string clang::tooling::getAbsolutePath ( StringRef  File)

Returns the absolute path of File, by prepending it with the current directory if File is not absolute.

Otherwise returns File. If 'File' starts with "./", the returned path will not contain the "./". Otherwise, the returned path will contain the literal path-concatenation of the current directory and File.

The difference to llvm::sys::fs::make_absolute is the canonicalization this does by removing "./" and computing native paths.

Parameters:
FileEither an absolute or relative path.

Definition at line 140 of file Tooling.cpp.

Referenced by clang::tooling::CompilationDatabase::autoDetectFromDirectory(), clang::tooling::CompilationDatabase::autoDetectFromSource(), and clang::tooling::ClangTool::run().

static const llvm::opt::ArgStringList* clang::tooling::getCC1Arguments ( clang::DiagnosticsEngine Diagnostics,
clang::driver::Compilation Compilation 
) [static]
static int clang::tooling::getRangeSize ( const SourceManager &  Sources,
const CharSourceRange &  Range 
) [static]
static std::vector<std::string> clang::tooling::getSyntaxOnlyToolArgs ( const std::vector< std::string > &  ExtraArgs,
StringRef  FileName 
) [static]

Definition at line 114 of file Tooling.cpp.

Referenced by buildASTFromCodeWithArgs(), and runToolOnCodeWithArgs().

static clang::driver::Driver* clang::tooling::newDriver ( clang::DiagnosticsEngine Diagnostics,
const char *  BinaryName 
) [static]

Builds a clang driver initialized for running clang tools.

Definition at line 55 of file Tooling.cpp.

References clang::driver::Driver::setTitle().

Referenced by clang::tooling::ToolInvocation::run().

template<typename T >
std::unique_ptr< FrontendActionFactory > clang::tooling::newFrontendActionFactory ( )

Returns a new FrontendActionFactory for a given type.

T must derive from clang::FrontendAction.

Example: FrontendActionFactory *Factory = newFrontendActionFactory<clang::SyntaxOnlyAction>();

Definition at line 305 of file Tooling.h.

References clang::serialized_diags::create().

template<typename FactoryT >
std::unique_ptr< FrontendActionFactory > clang::tooling::newFrontendActionFactory ( FactoryT *  ConsumerFactory,
SourceFileCallbacks *  Callbacks = nullptr 
) [inline]

Returns a new FrontendActionFactory for any type that provides an implementation of newASTConsumer().

FactoryT must implement: ASTConsumer *newASTConsumer().

Example: struct ProvidesASTConsumers { clang::ASTConsumer *newASTConsumer(); } Factory; std::unique_ptr<FrontendActionFactory> FactoryAdapter( newFrontendActionFactory(&Factory));

Definition at line 316 of file Tooling.h.

References clang::FrontendAction::BeginSourceFileAction(), clang::serialized_diags::create(), clang::FrontendAction::EndSourceFileAction(), clang::tooling::SourceFileCallbacks::handleBeginSource(), and clang::tooling::SourceFileCallbacks::handleEndSource().

static clang::CompilerInvocation* clang::tooling::newInvocation ( clang::DiagnosticsEngine Diagnostics,
const llvm::opt::ArgStringList &  CC1Args 
) [static]

Returns a clang build invocation initialized from the CC1 flags.

Definition at line 93 of file Tooling.cpp.

References clang::CompilerInvocation::CreateFromArgs().

Referenced by clang::tooling::ToolInvocation::run().

bool clang::tooling::operator< ( const Replacement &  LHS,
const Replacement &  RHS 
)
bool clang::tooling::operator== ( const Replacement &  LHS,
const Replacement &  RHS 
)
static Replacement clang::tooling::replaceStmtWithStmt ( SourceManager &  Sources,
const Stmt &  From,
const Stmt &  To 
) [static]
static Replacement clang::tooling::replaceStmtWithText ( SourceManager &  Sources,
const Stmt &  From,
StringRef  Text 
) [static]
bool clang::tooling::runToolOnCode ( clang::FrontendAction ToolAction,
const Twine &  Code,
const Twine &  FileName = "input.cc" 
)

Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.

Parameters:
ToolActionThe action to run over the code.
CodeC++ code.
FileNameThe file name which 'Code' will be mapped as.
Returns:
- True if 'ToolAction' was successfully executed.

Definition at line 107 of file Tooling.cpp.

References runToolOnCodeWithArgs().

bool clang::tooling::runToolOnCodeWithArgs ( clang::FrontendAction ToolAction,
const Twine &  Code,
const std::vector< std::string > &  Args,
const Twine &  FileName = "input.cc" 
)

Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and with additional other flags.

Parameters:
ToolActionThe action to run over the code.
CodeC++ code.
ArgsAdditional flags to pass on.
FileNameThe file name which 'Code' will be mapped as.
Returns:
- True if 'ToolAction' was successfully executed.

Definition at line 124 of file Tooling.cpp.

References getSyntaxOnlyToolArgs(), and clang::tooling::ToolInvocation::mapVirtualFile().

Referenced by runToolOnCode().

unsigned clang::tooling::shiftedCodePosition ( const Replacements &  Replaces,
unsigned  Position 
)

Calculates how a code Position is shifted when Replaces are applied.

Definition at line 146 of file Replacement.cpp.

References Position.

unsigned clang::tooling::shiftedCodePosition ( const std::vector< Replacement > &  Replaces,
unsigned  Position 
)

Calculates how a code Position is shifted when Replaces are applied.

Precondition:
Replaces[i].getOffset() <= Replaces[i+1].getOffset().

Definition at line 161 of file Replacement.cpp.

References Position.

static bool clang::tooling::stripPositionalArgs ( std::vector< const char * >  Args,
std::vector< std::string > &  Result 
) [static]

Strips any positional args and possible argv[0] from a command-line provided by the user to construct a FixedCompilationDatabase.

FixedCompilationDatabase requires a command line to be in this format as it constructs the command line for each file by appending the name of the file to be compiled. FixedCompilationDatabase also adds its own argv[0] to the start of the command line although its value is not important as it's just ignored by the Driver invoked by the ClangTool using the FixedCompilationDatabase.

FIXME: This functionality should probably be made available by clang::driver::Driver although what the interface should look like is not clear.

Parameters:
[in]ArgsArgs as provided by the user.
Returns:
Resulting stripped command line.
  • true if successful.
  • false if Args cannot be used for compilation jobs (e.g. contains an option like -E or -version).

Definition at line 206 of file CompilationDatabase.cpp.

References clang::driver::Action::AssembleJobClass, clang::driver::Job::CommandClass, clang::driver::JobList::getJobs(), clang::driver::Action::getKind(), clang::driver::Command::getSource(), clang::tooling::CompileJobAnalyzer::Inputs, clang::tooling::CompileJobAnalyzer::run(), and clang::tooling::UnusedInputDiagConsumer::UnusedInputs.

Referenced by clang::tooling::FixedCompilationDatabase::loadFromCommandLine().

static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin> clang::tooling::X ( "json-compilation-database"  ,
"Reads JSON formatted compilation databases"   
) [static]

Variable Documentation

const char* const clang::tooling::InvalidLocation = "" [static]

Definition at line 29 of file Replacement.cpp.

Referenced by clang::tooling::Replacement::isApplicable().

Definition at line 330 of file CompilationDatabase.cpp.

Definition at line 142 of file JSONCompilationDatabase.cpp.