1 //===--- tools/pp-trace/PPTrace.cpp - Clang preprocessor tracer -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements pp-trace, a tool for displaying a textual trace
11 // of the Clang preprocessor activity.  It's based on a derivation of the
12 // PPCallbacks class, that once registerd with Clang, receives callback calls
13 // to its virtual members, and outputs the information passed to the callbacks
14 // in a high-level YAML format.
15 //
16 // The pp-trace tool also serves as the basis for a test of the PPCallbacks
17 // mechanism.
18 //
19 // The pp-trace tool supports the following general command line format:
20 //
21 //    pp-trace [pp-trace options] (source file) [compiler options]
22 //
23 // Basically you put the pp-trace options first, then the source file or files,
24 // and then any options you want to pass to the compiler.
25 //
26 // These are the pp-trace options:
27 //
28 //    -ignore (callback list)     Don't display output for a comma-separated
29 //                                list of callbacks, i.e.:
30 //                                  -ignore "FileChanged,InclusionDirective"
31 //
32 //    -output (file)              Output trace to the given file in a YAML
33 //                                format, e.g.:
34 //
35 //                                  ---
36 //                                  - Callback: Name
37 //                                    Argument1: Value1
38 //                                    Argument2: Value2
39 //                                  (etc.)
40 //                                  ...
41 //
42 // Future Directions:
43 //
44 // 1. Add option opposite to "-ignore" that specifys a comma-separated option
45 // list of callbacs.  Perhaps "-only" or "-exclusive".
46 //
47 //===----------------------------------------------------------------------===//
48 
49 #include "PPCallbacksTracker.h"
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"
69 #include <algorithm>
70 #include <fstream>
71 #include <iterator>
72 #include <string>
73 #include <vector>
74 
75 using namespace clang;
76 using namespace clang::driver;
77 using namespace clang::driver::options;
78 using namespace clang::tooling;
79 using namespace llvm;
80 using namespace llvm::opt;
81 
82 // Options:
83 
84 // Collect the source files.
85 cl::list<std::string> SourcePaths(cl::Positional,
86                                   cl::desc("<source0> [... <sourceN>]"),
87                                   cl::OneOrMore);
88 
89 // Option to specify a list or one or more callback names to ignore.
90 cl::opt<std::string> IgnoreCallbacks(
91     "ignore", cl::init(""),
92     cl::desc("Ignore callbacks, i.e. \"Callback1, Callback2...\"."));
93 
94 // Option to specify the trace output file name.
95 cl::opt<std::string> OutputFileName(
96     "output", cl::init(""),
97     cl::desc("Output trace to the given file name or '-' for stdout."));
98 
99 // Collect all other arguments, which will be passed to the front end.
100 cl::list<std::string>
101 CC1Arguments(cl::ConsumeAfter,
102              cl::desc("<arguments to be passed to front end>..."));
103 
104 // Frontend action stuff:
105 
106 // Consumer is responsible for setting up the callbacks.
107 class PPTraceConsumer : public ASTConsumer {
108 public:
109   PPTraceConsumer(SmallSet<std::string, 4> &Ignore,
110                   std::vector<CallbackCall> &CallbackCalls, Preprocessor &PP) {
111     // PP takes ownership.
112     PP.addPPCallbacks(llvm::make_unique<PPCallbacksTracker>(Ignore,
113                                                             CallbackCalls, PP));
114   }
115 };
116 
117 class PPTraceAction : public SyntaxOnlyAction {
118 public:
119   PPTraceAction(SmallSet<std::string, 4> &Ignore,
120                 std::vector<CallbackCall> &CallbackCalls)
121       : Ignore(Ignore), CallbackCalls(CallbackCalls) {}
122 
123 protected:
124   std::unique_ptr<clang::ASTConsumer>
125   CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
126     return llvm::make_unique<PPTraceConsumer>(Ignore, CallbackCalls,
127                                               CI.getPreprocessor());
128   }
129 
130 private:
131   SmallSet<std::string, 4> &Ignore;
132   std::vector<CallbackCall> &CallbackCalls;
133 };
134 
135 class PPTraceFrontendActionFactory : public FrontendActionFactory {
136 public:
137   PPTraceFrontendActionFactory(SmallSet<std::string, 4> &Ignore,
138                                std::vector<CallbackCall> &CallbackCalls)
139       : Ignore(Ignore), CallbackCalls(CallbackCalls) {}
140 
141   virtual PPTraceAction *create() {
142     return new PPTraceAction(Ignore, CallbackCalls);
143   }
144 
145 private:
146   SmallSet<std::string, 4> &Ignore;
147   std::vector<CallbackCall> &CallbackCalls;
148 };
149 
150 // Output the trace given its data structure and a stream.
151 int outputPPTrace(std::vector<CallbackCall> &CallbackCalls,
152                   llvm::raw_ostream &OS) {
153   // Mark start of document.
154   OS << "---\n";
155 
156   for (std::vector<CallbackCall>::const_iterator I = CallbackCalls.begin(),
157                                                  E = CallbackCalls.end();
158        I != E; ++I) {
159     const CallbackCall &Callback = *I;
160     OS << "- Callback: " << Callback.Name << "\n";
161 
162     for (std::vector<Argument>::const_iterator AI = Callback.Arguments.begin(),
163                                                AE = Callback.Arguments.end();
164          AI != AE; ++AI) {
165       const Argument &Arg = *AI;
166       OS << "  " << Arg.Name << ": " << Arg.Value << "\n";
167     }
168   }
169 
170   // Mark end of document.
171   OS << "...\n";
172 
173   return 0;
174 }
175 
176 // Program entry point.
177 int main(int Argc, const char **Argv) {
178 
179   // Parse command line.
180   cl::ParseCommandLineOptions(Argc, Argv, "pp-trace.\n");
181 
182   // Parse the IgnoreCallbacks list into strings.
183   SmallVector<StringRef, 32> IgnoreCallbacksStrings;
184   StringRef(IgnoreCallbacks).split(IgnoreCallbacksStrings, ",",
185                                    /*MaxSplit=*/ -1, /*KeepEmpty=*/false);
186   SmallSet<std::string, 4> Ignore;
187   for (SmallVector<StringRef, 32>::iterator I = IgnoreCallbacksStrings.begin(),
188                                             E = IgnoreCallbacksStrings.end();
189        I != E; ++I)
190     Ignore.insert(*I);
191 
192   // Create the compilation database.
193   SmallString<256> PathBuf;
194   sys::fs::current_path(PathBuf);
195   std::unique_ptr<CompilationDatabase> Compilations;
196   Compilations.reset(
197       new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
198 
199   // Store the callback trace information here.
200   std::vector<CallbackCall> CallbackCalls;
201 
202   // Create the tool and run the compilation.
203   ClangTool Tool(*Compilations, SourcePaths);
204   PPTraceFrontendActionFactory Factory(Ignore, CallbackCalls);
205   int HadErrors = Tool.run(&Factory);
206 
207   // If we had errors, exit early.
208   if (HadErrors)
209     return HadErrors;
210 
211   // Do the output.
212   if (!OutputFileName.size()) {
213     HadErrors = outputPPTrace(CallbackCalls, llvm::outs());
214   } else {
215     // Set up output file.
216     std::error_code EC;
217     llvm::tool_output_file Out(OutputFileName, EC, llvm::sys::fs::F_Text);
218     if (EC) {
219       llvm::errs() << "pp-trace: error creating " << OutputFileName << ":"
220                    << EC.message() << "\n";
221       return 1;
222     }
223 
224     HadErrors = outputPPTrace(CallbackCalls, Out.os());
225 
226     // Tell tool_output_file that we want to keep the file.
227     if (HadErrors == 0)
228       Out.keep();
229   }
230 
231   return HadErrors;
232 }
233