50 #include "clang/AST/ASTConsumer.h" 51 #include "clang/AST/ASTContext.h" 52 #include "clang/AST/RecursiveASTVisitor.h" 53 #include "clang/Basic/SourceManager.h" 54 #include "clang/Driver/Options.h" 55 #include "clang/Frontend/CompilerInstance.h" 56 #include "clang/Frontend/FrontendActions.h" 57 #include "clang/Lex/Preprocessor.h" 58 #include "clang/Tooling/CompilationDatabase.h" 59 #include "clang/Tooling/Tooling.h" 60 #include "llvm/Option/Arg.h" 61 #include "llvm/Option/ArgList.h" 62 #include "llvm/Option/OptTable.h" 63 #include "llvm/Option/Option.h" 64 #include "llvm/Support/CommandLine.h" 65 #include "llvm/Support/FileSystem.h" 66 #include "llvm/Support/MemoryBuffer.h" 67 #include "llvm/Support/Path.h" 68 #include "llvm/Support/ToolOutputFile.h" 75 using namespace clang;
82 static cl::list<std::string>
SourcePaths(cl::Positional,
83 cl::desc(
"<source0> [... <sourceN>]"),
88 "ignore", cl::init(
""),
89 cl::desc(
"Ignore callbacks, i.e. \"Callback1, Callback2...\"."));
93 "output", cl::init(
""),
94 cl::desc(
"Output trace to the given file name or '-' for stdout."));
97 static cl::list<std::string>
99 cl::desc(
"<arguments to be passed to front end>..."));
107 PPTraceConsumer(SmallSet<std::string, 4> &Ignore,
108 std::vector<CallbackCall> &CallbackCalls, Preprocessor &PP) {
110 PP.addPPCallbacks(llvm::make_unique<PPCallbacksTracker>(Ignore,
117 PPTraceAction(SmallSet<std::string, 4> &Ignore,
118 std::vector<CallbackCall> &CallbackCalls)
119 : Ignore(Ignore), CallbackCalls(CallbackCalls) {}
122 std::unique_ptr<clang::ASTConsumer>
123 CreateASTConsumer(CompilerInstance &CI, StringRef InFile)
override {
124 return llvm::make_unique<PPTraceConsumer>(Ignore, CallbackCalls,
125 CI.getPreprocessor());
129 SmallSet<std::string, 4> &Ignore;
130 std::vector<CallbackCall> &CallbackCalls;
133 class PPTraceFrontendActionFactory :
public FrontendActionFactory {
135 PPTraceFrontendActionFactory(SmallSet<std::string, 4> &Ignore,
136 std::vector<CallbackCall> &CallbackCalls)
137 : Ignore(Ignore), CallbackCalls(CallbackCalls) {}
139 PPTraceAction *create()
override {
140 return new PPTraceAction(Ignore, CallbackCalls);
144 SmallSet<std::string, 4> &Ignore;
145 std::vector<CallbackCall> &CallbackCalls;
151 llvm::raw_ostream &OS) {
155 for (std::vector<CallbackCall>::const_iterator I = CallbackCalls.begin(),
156 E = CallbackCalls.end();
159 OS <<
"- Callback: " << Callback.
Name <<
"\n";
164 OS <<
" " << Arg.
Name <<
": " << Arg.
Value <<
"\n";
175 int main(
int Argc,
const char **Argv) {
178 cl::ParseCommandLineOptions(Argc, Argv,
"pp-trace.\n");
181 SmallVector<StringRef, 32> IgnoreCallbacksStrings;
184 SmallSet<std::string, 4> Ignore;
185 for (SmallVector<StringRef, 32>::iterator I = IgnoreCallbacksStrings.begin(),
186 E = IgnoreCallbacksStrings.end();
191 SmallString<256> PathBuf;
192 sys::fs::current_path(PathBuf);
193 std::unique_ptr<CompilationDatabase> Compilations;
195 new FixedCompilationDatabase(Twine(PathBuf),
CC1Arguments));
198 std::vector<CallbackCall> CallbackCalls;
202 PPTraceFrontendActionFactory Factory(Ignore, CallbackCalls);
203 int HadErrors = Tool.run(&Factory);
215 llvm::ToolOutputFile Out(
OutputFileName, EC, llvm::sys::fs::F_Text);
217 llvm::errs() <<
"pp-trace: error creating " <<
OutputFileName <<
":" 218 << EC.message() <<
"\n";
This class represents one callback function argument by name and value.
Some operations such as code completion produce a set of candidates.
std::vector< Argument > Arguments
static cl::list< std::string > SourcePaths(cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore)
static cl::list< std::string > CC1Arguments(cl::ConsumeAfter, cl::desc("<arguments to be passed to front end>..."))
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
static cl::opt< std::string > OutputFileName("output", cl::init(""), cl::desc("Output trace to the given file name or '-' for stdout."))
static cl::opt< std::string > IgnoreCallbacks("ignore", cl::init(""), cl::desc("Ignore callbacks, i.e. \allback1, Callback2...\"))
Classes and definitions for preprocessor tracking.
This class represents one callback call by name and an array of arguments.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static int outputPPTrace(std::vector< CallbackCall > &CallbackCalls, llvm::raw_ostream &OS)
int main(int Argc, const char **Argv)