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

Classes

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...
 
class  FileMatchTrieNode
 A node of the FileMatchTrie. More...
 

Typedefs

typedef std::vector< std::string > CommandLineArguments
 A sequence of command line arguments. More...
 
typedef std::function
< CommandLineArguments(const
CommandLineArguments &)> 
ArgumentsAdjuster
 A prototype of a command line adjuster. More...
 
typedef llvm::Registry
< CompilationDatabasePlugin
CompilationDatabasePluginRegistry
 
typedef std::set< ReplacementReplacements
 A set of Replacements. FIXME: Change to a vector and deduplicate in the RefactoringTool. More...
 
typedef std::vector< std::pair
< std::string, std::string > > 
FileContentMappings
 

Enumerations

enum  ArgumentInsertPosition { ArgumentInsertPosition::BEGIN, ArgumentInsertPosition::END }
 

Functions

ArgumentsAdjuster getClangSyntaxOnlyAdjuster ()
 Gets an argument adjuster that converts input command line arguments to the "syntax check only" variant. More...
 
ArgumentsAdjuster getClangStripOutputAdjuster ()
 Gets an argument adjuster which removes output-related command line arguments. More...
 
ArgumentsAdjuster getInsertArgumentAdjuster (const CommandLineArguments &Extra, ArgumentInsertPosition Pos)
 Gets an argument adjuster which inserts Extra arguments in the specified position. More...
 
ArgumentsAdjuster getInsertArgumentAdjuster (const char *Extra, ArgumentInsertPosition Pos=ArgumentInsertPosition::END)
 Gets an argument adjuster which inserts an Extra argument in the specified position. More...
 
ArgumentsAdjuster combineAdjusters (ArgumentsAdjuster First, ArgumentsAdjuster Second)
 Gets an argument adjuster which adjusts the arguments in sequence with the First adjuster and then with the Second one. More...
 
bool operator< (const Replacement &LHS, const Replacement &RHS)
 Less-than operator between two Replacements. More...
 
bool operator== (const Replacement &LHS, const Replacement &RHS)
 Equal-to operator between two Replacements. More...
 
bool applyAllReplacements (const Replacements &Replaces, Rewriter &Rewrite)
 Apply all replacements in Replaces to the Rewriter Rewrite. More...
 
bool applyAllReplacements (const std::vector< Replacement > &Replaces, Rewriter &Rewrite)
 Apply all replacements in Replaces to the Rewriter Rewrite. More...
 
std::string applyAllReplacements (StringRef Code, const Replacements &Replaces)
 Applies all replacements in Replaces to Code. More...
 
unsigned shiftedCodePosition (const Replacements &Replaces, unsigned Position)
 Calculates how a code Position is shifted when Replaces are applied. More...
 
unsigned shiftedCodePosition (const std::vector< Replacement > &Replaces, unsigned Position)
 Calculates how a code Position is shifted when Replaces are applied. More...
 
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. More...
 
template<typename T >
std::unique_ptr
< FrontendActionFactory
newFrontendActionFactory ()
 Returns a new FrontendActionFactory for a given type. More...
 
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(). More...
 
bool runToolOnCode (clang::FrontendAction *ToolAction, const Twine &Code, const Twine &FileName="input.cc", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
 Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag. More...
 
bool runToolOnCodeWithArgs (clang::FrontendAction *ToolAction, const Twine &Code, const std::vector< std::string > &Args, const Twine &FileName="input.cc", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >(), const FileContentMappings &VirtualMappedFiles=FileContentMappings())
 Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and with additional other flags. More...
 
std::unique_ptr< ASTUnitbuildASTFromCode (const Twine &Code, const Twine &FileName="input.cc", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
 Builds an AST for 'Code'. More...
 
std::unique_ptr< ASTUnitbuildASTFromCodeWithArgs (const Twine &Code, const std::vector< std::string > &Args, const Twine &FileName="input.cc", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
 Builds an AST for 'Code' with additional flags. More...
 
std::string getAbsolutePath (StringRef File)
 Returns the absolute path of File, by prepending it with the current directory if File is not absolute. More...
 
clang::CompilerInvocationnewInvocation (clang::DiagnosticsEngine *Diagnostics, const llvm::opt::ArgStringList &CC1Args)
 Creates a CompilerInvocation. More...
 
static int getRangeSize (const SourceManager &Sources, const CharSourceRange &Range, const LangOptions &LangOpts)
 
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. More...
 
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. More...
 
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

A prototype of a command line adjuster.

Command line argument adjuster is responsible for command line arguments modification before the arguments are used to run a frontend action.

Definition at line 35 of file ArgumentsAdjusters.h.

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

A sequence of command line arguments.

Definition at line 28 of file ArgumentsAdjusters.h.

Definition at line 19 of file CompilationDatabasePluginRegistry.h.

typedef std::vector<std::pair<std::string, std::string> > clang::tooling::FileContentMappings

The first part of the pair is the filename, the second part the file-content.

Definition at line 157 of file Tooling.h.

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

Definition at line 141 of file Replacement.h.

Enumeration Type Documentation

Enumerator
BEGIN 
END 

Definition at line 45 of file ArgumentsAdjusters.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 236 of file Replacement.cpp.

References clang::Result.

Referenced by clang::tooling::RefactoringTool::applyAllReplacements().

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 252 of file Replacement.cpp.

References clang::Result.

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 267 of file Replacement.cpp.

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

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

Builds an AST for 'Code'.

Parameters
CodeC++ code.
FileNameThe file name which 'Code' will be mapped as.
PCHContainerOpsThe PCHContainerOperations for loading and creating clang modules.
Returns
The resulting AST or null if an error occurred.

Definition at line 417 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",
std::shared_ptr< PCHContainerOperations >  PCHContainerOps = std::make_shared<PCHContainerOperations>() 
)

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.
PCHContainerOpsThe PCHContainerOperations for loading and creating clang modules.
Returns
The resulting AST or null if an error occurred.

Definition at line 423 of file Tooling.cpp.

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

Referenced by buildASTFromCode().

ArgumentsAdjuster clang::tooling::combineAdjusters ( ArgumentsAdjuster  First,
ArgumentsAdjuster  Second 
)

Gets an argument adjuster which adjusts the arguments in sequence with the First adjuster and then with the Second one.

Definition at line 79 of file ArgumentsAdjusters.cpp.

Referenced by clang::tooling::ClangTool::appendArgumentsAdjuster().

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 184 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().

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 150 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
ArgumentsAdjuster clang::tooling::getClangStripOutputAdjuster ( )

Gets an argument adjuster which removes output-related command line arguments.

Definition at line 38 of file ArgumentsAdjusters.cpp.

Referenced by clang::tooling::ClangTool::ClangTool().

ArgumentsAdjuster clang::tooling::getClangSyntaxOnlyAdjuster ( )

Gets an argument adjuster that converts input command line arguments to the "syntax check only" variant.

Add -fsyntax-only option to the commnand line arguments.

Definition at line 23 of file ArgumentsAdjusters.cpp.

Referenced by clang::tooling::ClangTool::ClangTool().

ArgumentsAdjuster clang::tooling::getInsertArgumentAdjuster ( const CommandLineArguments &  Extra,
ArgumentInsertPosition  Pos 
)

Gets an argument adjuster which inserts Extra arguments in the specified position.

Definition at line 56 of file ArgumentsAdjusters.cpp.

References END.

Referenced by clang::tooling::CommonOptionsParser::CommonOptionsParser(), and getInsertArgumentAdjuster().

ArgumentsAdjuster clang::tooling::getInsertArgumentAdjuster ( const char *  Extra,
ArgumentInsertPosition  Pos = ArgumentInsertPosition::END 
)

Gets an argument adjuster which inserts an Extra argument in the specified position.

Definition at line 74 of file ArgumentsAdjusters.cpp.

References getInsertArgumentAdjuster().

static int clang::tooling::getRangeSize ( const SourceManager &  Sources,
const CharSourceRange &  Range,
const LangOptions &  LangOpts 
)
static
static std::vector<std::string> clang::tooling::getSyntaxOnlyToolArgs ( const std::vector< std::string > &  ExtraArgs,
StringRef  FileName 
)
static

Definition at line 115 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 344 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 355 of file Tooling.h.

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

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

Creates a CompilerInvocation.

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",
std::shared_ptr< PCHContainerOperations >  PCHContainerOps = std::make_shared<PCHContainerOperations>() 
)

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.
PCHContainerOpsThe PCHContainerOperations for loading and creating clang modules.
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",
std::shared_ptr< PCHContainerOperations >  PCHContainerOps = std::make_shared<PCHContainerOperations>(),
const FileContentMappings &  VirtualMappedFiles = FileContentMappings() 
)

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.
PCHContainerOpsThe PCHContainerOperations for loading and creating clang modules.
Returns
- True if 'ToolAction' was successfully executed.

Definition at line 125 of file Tooling.cpp.

References getSyntaxOnlyToolArgs().

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 154 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 169 of file Replacement.cpp.

References Position.

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().

int clang::tooling::JSONAnchorDest = JSONAnchorSource
static

Definition at line 328 of file CompilationDatabase.cpp.

volatile int clang::tooling::JSONAnchorSource = 0

Definition at line 142 of file JSONCompilationDatabase.cpp.