2013-10-31 20:23:32 +08:00
|
|
|
//===--- tools/pp-trace/PPTrace.cpp - Clang preprocessor tracer -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements pp-trace, a tool for displaying a textual trace
|
|
|
|
// of the Clang preprocessor activity. It's based on a derivation of the
|
|
|
|
// PPCallbacks class, that once registerd with Clang, receives callback calls
|
|
|
|
// to its virtual members, and outputs the information passed to the callbacks
|
|
|
|
// in a high-level YAML format.
|
|
|
|
//
|
|
|
|
// The pp-trace tool also serves as the basis for a test of the PPCallbacks
|
|
|
|
// mechanism.
|
|
|
|
//
|
|
|
|
// The pp-trace tool supports the following general command line format:
|
|
|
|
//
|
2013-10-31 20:53:42 +08:00
|
|
|
// pp-trace [pp-trace options] (source file) [compiler options]
|
2013-10-31 20:23:32 +08:00
|
|
|
//
|
|
|
|
// Basically you put the pp-trace options first, then the source file or files,
|
|
|
|
// and then any options you want to pass to the compiler.
|
|
|
|
//
|
|
|
|
// These are the pp-trace options:
|
|
|
|
//
|
|
|
|
// -ignore (callback list) Don't display output for a comma-separated
|
|
|
|
// list of callbacks, i.e.:
|
|
|
|
// -ignore "FileChanged,InclusionDirective"
|
|
|
|
//
|
|
|
|
// -output (file) Output trace to the given file in a YAML
|
|
|
|
// format, e.g.:
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// - Callback: Name
|
|
|
|
// Argument1: Value1
|
|
|
|
// Argument2: Value2
|
|
|
|
// (etc.)
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// Future Directions:
|
|
|
|
//
|
|
|
|
// 1. Add option opposite to "-ignore" that specifys a comma-separated option
|
|
|
|
// list of callbacs. Perhaps "-only" or "-exclusive".
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PPCallbacksTracker.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace clang::driver::options;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
2015-02-25 10:46:37 +08:00
|
|
|
// Options:
|
|
|
|
|
|
|
|
// Collect the source files.
|
2015-03-23 20:49:15 +08:00
|
|
|
static cl::list<std::string> SourcePaths(cl::Positional,
|
|
|
|
cl::desc("<source0> [... <sourceN>]"),
|
|
|
|
cl::OneOrMore);
|
2015-02-25 10:46:37 +08:00
|
|
|
|
|
|
|
// Option to specify a list or one or more callback names to ignore.
|
2015-03-23 20:49:15 +08:00
|
|
|
static cl::opt<std::string> IgnoreCallbacks(
|
2015-02-25 10:46:37 +08:00
|
|
|
"ignore", cl::init(""),
|
|
|
|
cl::desc("Ignore callbacks, i.e. \"Callback1, Callback2...\"."));
|
|
|
|
|
|
|
|
// Option to specify the trace output file name.
|
2015-03-23 20:49:15 +08:00
|
|
|
static cl::opt<std::string> OutputFileName(
|
2015-02-25 10:46:37 +08:00
|
|
|
"output", cl::init(""),
|
|
|
|
cl::desc("Output trace to the given file name or '-' for stdout."));
|
|
|
|
|
|
|
|
// Collect all other arguments, which will be passed to the front end.
|
2015-03-23 20:49:15 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
CC1Arguments(cl::ConsumeAfter,
|
|
|
|
cl::desc("<arguments to be passed to front end>..."));
|
2015-02-25 10:46:37 +08:00
|
|
|
|
2013-10-31 20:23:32 +08:00
|
|
|
// Frontend action stuff:
|
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
namespace {
|
2013-10-31 20:23:32 +08:00
|
|
|
// Consumer is responsible for setting up the callbacks.
|
|
|
|
class PPTraceConsumer : public ASTConsumer {
|
|
|
|
public:
|
|
|
|
PPTraceConsumer(SmallSet<std::string, 4> &Ignore,
|
|
|
|
std::vector<CallbackCall> &CallbackCalls, Preprocessor &PP) {
|
|
|
|
// PP takes ownership.
|
2014-09-10 13:07:57 +08:00
|
|
|
PP.addPPCallbacks(llvm::make_unique<PPCallbacksTracker>(Ignore,
|
|
|
|
CallbackCalls, PP));
|
2013-10-31 20:23:32 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PPTraceAction : public SyntaxOnlyAction {
|
|
|
|
public:
|
|
|
|
PPTraceAction(SmallSet<std::string, 4> &Ignore,
|
|
|
|
std::vector<CallbackCall> &CallbackCalls)
|
|
|
|
: Ignore(Ignore), CallbackCalls(CallbackCalls) {}
|
|
|
|
|
|
|
|
protected:
|
2014-08-11 03:56:59 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
|
|
|
CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
|
|
|
|
return llvm::make_unique<PPTraceConsumer>(Ignore, CallbackCalls,
|
|
|
|
CI.getPreprocessor());
|
2013-10-31 20:23:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmallSet<std::string, 4> &Ignore;
|
|
|
|
std::vector<CallbackCall> &CallbackCalls;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PPTraceFrontendActionFactory : public FrontendActionFactory {
|
|
|
|
public:
|
|
|
|
PPTraceFrontendActionFactory(SmallSet<std::string, 4> &Ignore,
|
|
|
|
std::vector<CallbackCall> &CallbackCalls)
|
|
|
|
: Ignore(Ignore), CallbackCalls(CallbackCalls) {}
|
|
|
|
|
2015-04-11 15:59:33 +08:00
|
|
|
PPTraceAction *create() override {
|
2013-10-31 20:23:32 +08:00
|
|
|
return new PPTraceAction(Ignore, CallbackCalls);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmallSet<std::string, 4> &Ignore;
|
|
|
|
std::vector<CallbackCall> &CallbackCalls;
|
|
|
|
};
|
2015-03-23 20:49:15 +08:00
|
|
|
} // namespace
|
2013-10-31 20:23:32 +08:00
|
|
|
|
|
|
|
// Output the trace given its data structure and a stream.
|
2015-03-23 20:49:15 +08:00
|
|
|
static int outputPPTrace(std::vector<CallbackCall> &CallbackCalls,
|
|
|
|
llvm::raw_ostream &OS) {
|
2013-10-31 20:23:32 +08:00
|
|
|
// Mark start of document.
|
|
|
|
OS << "---\n";
|
|
|
|
|
|
|
|
for (std::vector<CallbackCall>::const_iterator I = CallbackCalls.begin(),
|
|
|
|
E = CallbackCalls.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const CallbackCall &Callback = *I;
|
|
|
|
OS << "- Callback: " << Callback.Name << "\n";
|
|
|
|
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto AI = Callback.Arguments.begin(), AE = Callback.Arguments.end();
|
2013-10-31 20:23:32 +08:00
|
|
|
AI != AE; ++AI) {
|
|
|
|
const Argument &Arg = *AI;
|
|
|
|
OS << " " << Arg.Name << ": " << Arg.Value << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark end of document.
|
|
|
|
OS << "...\n";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Program entry point.
|
|
|
|
int main(int Argc, const char **Argv) {
|
|
|
|
|
|
|
|
// Parse command line.
|
|
|
|
cl::ParseCommandLineOptions(Argc, Argv, "pp-trace.\n");
|
|
|
|
|
|
|
|
// Parse the IgnoreCallbacks list into strings.
|
|
|
|
SmallVector<StringRef, 32> IgnoreCallbacksStrings;
|
|
|
|
StringRef(IgnoreCallbacks).split(IgnoreCallbacksStrings, ",",
|
|
|
|
/*MaxSplit=*/ -1, /*KeepEmpty=*/false);
|
|
|
|
SmallSet<std::string, 4> Ignore;
|
|
|
|
for (SmallVector<StringRef, 32>::iterator I = IgnoreCallbacksStrings.begin(),
|
|
|
|
E = IgnoreCallbacksStrings.end();
|
|
|
|
I != E; ++I)
|
|
|
|
Ignore.insert(*I);
|
|
|
|
|
|
|
|
// Create the compilation database.
|
|
|
|
SmallString<256> PathBuf;
|
|
|
|
sys::fs::current_path(PathBuf);
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<CompilationDatabase> Compilations;
|
2013-10-31 20:23:32 +08:00
|
|
|
Compilations.reset(
|
|
|
|
new FixedCompilationDatabase(Twine(PathBuf), CC1Arguments));
|
|
|
|
|
|
|
|
// Store the callback trace information here.
|
|
|
|
std::vector<CallbackCall> CallbackCalls;
|
|
|
|
|
|
|
|
// Create the tool and run the compilation.
|
|
|
|
ClangTool Tool(*Compilations, SourcePaths);
|
2014-07-24 18:23:33 +08:00
|
|
|
PPTraceFrontendActionFactory Factory(Ignore, CallbackCalls);
|
|
|
|
int HadErrors = Tool.run(&Factory);
|
2013-10-31 20:23:32 +08:00
|
|
|
|
|
|
|
// If we had errors, exit early.
|
|
|
|
if (HadErrors)
|
|
|
|
return HadErrors;
|
|
|
|
|
|
|
|
// Do the output.
|
|
|
|
if (!OutputFileName.size()) {
|
|
|
|
HadErrors = outputPPTrace(CallbackCalls, llvm::outs());
|
|
|
|
} else {
|
|
|
|
// Set up output file.
|
2014-08-26 02:17:00 +08:00
|
|
|
std::error_code EC;
|
2017-09-23 09:03:17 +08:00
|
|
|
llvm::ToolOutputFile Out(OutputFileName, EC, llvm::sys::fs::F_Text);
|
2014-08-26 02:17:00 +08:00
|
|
|
if (EC) {
|
2013-10-31 20:23:32 +08:00
|
|
|
llvm::errs() << "pp-trace: error creating " << OutputFileName << ":"
|
2014-08-26 02:17:00 +08:00
|
|
|
<< EC.message() << "\n";
|
2013-10-31 20:23:32 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
HadErrors = outputPPTrace(CallbackCalls, Out.os());
|
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
// Tell ToolOutputFile that we want to keep the file.
|
2013-10-31 20:23:32 +08:00
|
|
|
if (HadErrors == 0)
|
|
|
|
Out.keep();
|
|
|
|
}
|
|
|
|
|
|
|
|
return HadErrors;
|
|
|
|
}
|