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/FrontendAction.h"
34#include "clang/Frontend/FrontendActions.h"
35#include "clang/Lex/Preprocessor.h"
36#include "clang/Tooling/Execution.h"
37#include "clang/Tooling/Tooling.h"
38#include "llvm/Option/Arg.h"
39#include "llvm/Option/ArgList.h"
40#include "llvm/Option/OptTable.h"
41#include "llvm/Option/Option.h"
42#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/FileSystem.h"
44#include "llvm/Support/GlobPattern.h"
45#include "llvm/Support/InitLLVM.h"
46#include "llvm/Support/Path.h"
47#include "llvm/Support/ToolOutputFile.h"
48#include "llvm/Support/WithColor.h"
57static cl::OptionCategory
Cat(
"pp-trace options");
59static cl::opt<std::string> Callbacks(
60 "callbacks", cl::init(
"*"),
61 cl::desc(
"Comma-separated list of globs describing the list of callbacks "
62 "to output. Globs are processed in order of appearance. Globs "
63 "with the '-' prefix remove callbacks from the set. e.g. "
68 "output", cl::init(
"-"),
69 cl::desc(
"Output trace to the given file name or '-' for stdout."),
72[[noreturn]]
static void error(Twine Message) {
73 WithColor::error() << Message <<
'\n';
79class PPTraceAction :
public ASTFrontendAction {
81 PPTraceAction(
const FilterType &Filters, raw_ostream &OS)
82 : Filters(Filters),
OS(
OS) {}
85 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &
CI,
86 StringRef InFile)
override {
87 Preprocessor &PP =
CI.getPreprocessor();
89 std::make_unique<PPCallbacksTracker>(Filters, CallbackCalls, PP));
90 return std::make_unique<ASTConsumer>();
93 void EndSourceFileAction()
override {
95 for (
const CallbackCall &Callback : CallbackCalls) {
97 for (
const Argument &Arg :
Callback.Arguments)
98 OS <<
" " << Arg.Name <<
": " << Arg.Value <<
"\n";
102 CallbackCalls.clear();
108 std::vector<CallbackCall> CallbackCalls;
111class PPTraceFrontendActionFactory :
public tooling::FrontendActionFactory {
113 PPTraceFrontendActionFactory(
const FilterType &Filters, raw_ostream &OS)
114 : Filters(Filters),
OS(
OS) {}
116 std::unique_ptr<FrontendAction> create()
override {
117 return std::make_unique<PPTraceAction>(Filters, OS);
128int main(
int argc,
const char **argv) {
130 InitLLVM
X(argc, argv);
131 auto OptionsParser = clang::tooling::CommonOptionsParser::create(
132 argc, argv, Cat, llvm::cl::ZeroOrMore);
134 error(toString(OptionsParser.takeError()));
136 SmallVector<StringRef, 32> Patterns;
138 StringRef(Callbacks).split(Patterns,
",",
140 for (StringRef Pattern : Patterns) {
141 Pattern = Pattern.trim();
142 bool Enabled = !Pattern.consume_front(
"-");
143 Expected<GlobPattern> Pat = GlobPattern::create(Pattern);
145 Filters.emplace_back(std::move(*Pat),
Enabled);
147 error(toString(Pat.takeError()));
151 clang::tooling::ClangTool Tool(OptionsParser->getCompilations(),
152 OptionsParser->getSourcePathList());
155 llvm::ToolOutputFile
Out(OutputFileName, EC, llvm::sys::fs::OF_TextWithCRLF);
158 PPTraceFrontendActionFactory Factory(Filters,
Out.os());
159 int HadErrors = Tool.run(&Factory);
CompiledFragmentImpl & Out
Classes and definitions for preprocessor tracking.
int main(int argc, const char **argv)
const bool Enabled
Whether the heuristic is to be enabled by default.
std::unique_ptr< CompilerInvocation > CI
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
std::vector< std::pair< llvm::GlobPattern, bool > > FilterType
static cl::opt< std::string > OutputFileName("output", cl::init("-"), cl::desc("Output trace to the given file name or '-' for stdout."), cl::cat(Cat))
static void error(Twine Message)
static cl::OptionCategory Cat("pp-trace options")
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.