28 #include "clang/AST/ASTConsumer.h" 29 #include "clang/AST/ASTContext.h" 30 #include "clang/Basic/SourceManager.h" 31 #include "clang/Driver/Options.h" 32 #include "clang/Frontend/CompilerInstance.h" 33 #include "clang/Frontend/FrontendActions.h" 34 #include "clang/Lex/Preprocessor.h" 35 #include "clang/Tooling/Execution.h" 36 #include "clang/Tooling/Tooling.h" 37 #include "llvm/Option/Arg.h" 38 #include "llvm/Option/ArgList.h" 39 #include "llvm/Option/OptTable.h" 40 #include "llvm/Option/Option.h" 41 #include "llvm/Support/CommandLine.h" 42 #include "llvm/Support/FileSystem.h" 43 #include "llvm/Support/GlobPattern.h" 44 #include "llvm/Support/InitLLVM.h" 45 #include "llvm/Support/Path.h" 46 #include "llvm/Support/ToolOutputFile.h" 47 #include "llvm/Support/WithColor.h" 56 static cl::OptionCategory
Cat(
"pp-trace options");
59 "callbacks", cl::init(
"*"),
60 cl::desc(
"Comma-separated list of globs describing the list of callbacks " 61 "to output. Globs are processed in order of appearance. Globs " 62 "with the '-' prefix remove callbacks from the set. e.g. " 67 "output", cl::init(
"-"),
68 cl::desc(
"Output trace to the given file name or '-' for stdout."),
78 class PPTraceAction :
public ASTFrontendAction {
80 PPTraceAction(
const FilterType &Filters, raw_ostream &OS)
81 : Filters(Filters), OS(OS) {}
84 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
85 StringRef InFile)
override {
86 Preprocessor &PP = CI.getPreprocessor();
88 llvm::make_unique<PPCallbacksTracker>(Filters, CallbackCalls, PP));
89 return llvm::make_unique<ASTConsumer>();
92 void EndSourceFileAction()
override {
95 OS <<
"- Callback: " <<
Callback.Name <<
"\n";
97 OS <<
" " << Arg.
Name <<
": " << Arg.
Value <<
"\n";
101 CallbackCalls.clear();
107 std::vector<CallbackCall> CallbackCalls;
110 class PPTraceFrontendActionFactory :
public tooling::FrontendActionFactory {
112 PPTraceFrontendActionFactory(
const FilterType &Filters, raw_ostream &OS)
113 : Filters(Filters), OS(OS) {}
115 PPTraceAction *create()
override {
return new PPTraceAction(Filters, OS); }
125 int main(
int argc,
const char **argv) {
127 InitLLVM
X(argc, argv);
128 auto OptionsParser = clang::tooling::CommonOptionsParser::create(
129 argc, argv,
Cat, llvm::cl::ZeroOrMore);
133 SmallVector<StringRef, 32> Patterns;
135 StringRef(
Callbacks).split(Patterns,
",",
137 for (StringRef Pattern : Patterns) {
138 Pattern = Pattern.trim();
139 bool Enabled = !Pattern.consume_front(
"-");
140 Expected<GlobPattern> Pat = GlobPattern::create(Pattern);
142 Filters.emplace_back(std::move(*Pat), Enabled);
148 clang::tooling::ClangTool Tool(OptionsParser->getCompilations(),
149 OptionsParser->getSourcePathList());
152 llvm::ToolOutputFile Out(
OutputFileName, EC, llvm::sys::fs::F_Text);
155 PPTraceFrontendActionFactory Factory(Filters, Out.os());
156 int HadErrors = Tool.run(&Factory);
Some operations such as code completion produce a set of candidates.
static cl::opt< std::string > Callbacks("callbacks", cl::init("*"), cl::desc("Comma-separated list of globs describing the list of callbacks " "to output. Globs are processed in order of appearance. Globs " "with the '-' prefix remove callbacks from the set. e.g. " "'*,-Macro*'."), cl::cat(Cat))
constexpr llvm::StringLiteral Message
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
static ClangTidyModuleRegistry::Add< AbseilModule > X("abseil-module", "Add Abseil checks.")
static LLVM_ATTRIBUTE_NORETURN void error(Twine Message)
This class represents one callback call by name and an array of arguments.
int main(int argc, const char **argv)
Classes and definitions for preprocessor tracking.
static cl::opt< std::string > OutputFileName("output", cl::init("-"), cl::desc("Output trace to the given file name or '-' for stdout."), cl::cat(Cat))
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::pair< llvm::GlobPattern, bool > > FilterType
static cl::OptionCategory Cat("pp-trace options")