2013-10-31 20:23:32 +08:00
|
|
|
//===--- PPCallbacksTracker.h - Preprocessor tracking -*- C++ -*---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Classes and definitions for preprocessor tracking.
|
|
|
|
///
|
|
|
|
/// The core definition is the PPCallbacksTracker class, derived from Clang's
|
|
|
|
/// PPCallbacks class from the Lex library, which overrides all the callbacks
|
|
|
|
/// and collects information about each callback call, saving it in a
|
|
|
|
/// data structure built up of CallbackCall and Argument objects, which
|
|
|
|
/// record the preprocessor callback name and arguments in high-level string
|
|
|
|
/// form for later inspection.
|
|
|
|
///
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef PPTRACE_PPCALLBACKSTRACKER_H
|
|
|
|
#define PPTRACE_PPCALLBACKSTRACKER_H
|
|
|
|
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
|
|
|
|
/// \brief This class represents one callback function argument by name
|
|
|
|
/// and value.
|
|
|
|
class Argument {
|
|
|
|
public:
|
|
|
|
Argument(llvm::StringRef Name, llvm::StringRef Value)
|
|
|
|
: Name(Name), Value(Value) {}
|
|
|
|
Argument() {}
|
|
|
|
|
|
|
|
std::string Name;
|
|
|
|
std::string Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief This class represents one callback call by name and an array
|
|
|
|
/// of arguments.
|
|
|
|
class CallbackCall {
|
|
|
|
public:
|
|
|
|
CallbackCall(llvm::StringRef Name) : Name(Name) {}
|
|
|
|
CallbackCall() {}
|
|
|
|
|
|
|
|
std::string Name;
|
|
|
|
std::vector<Argument> Arguments;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief This class overrides the PPCallbacks class for tracking preprocessor
|
|
|
|
/// activity by means of its callback functions.
|
|
|
|
///
|
|
|
|
/// This object is given a vector for storing the trace information, built up
|
|
|
|
/// of CallbackCall and subordinate Argument objects for representing the
|
|
|
|
/// callback calls and their arguments. It's a reference so the vector can
|
|
|
|
/// exist beyond the lifetime of this object, because it's deleted by the
|
|
|
|
/// preprocessor automatically in its destructor.
|
|
|
|
///
|
|
|
|
/// This class supports a mechanism for inhibiting trace output for
|
|
|
|
/// specific callbacks by name, for the purpose of eliminating output for
|
|
|
|
/// callbacks of no interest that might clutter the output.
|
|
|
|
///
|
|
|
|
/// Following the constructor and destructor function declarations, the
|
|
|
|
/// overidden callback functions are defined. The remaining functions are
|
|
|
|
/// helpers for recording the trace data, to reduce the coupling between it
|
|
|
|
/// and the recorded data structure.
|
|
|
|
class PPCallbacksTracker : public clang::PPCallbacks {
|
|
|
|
public:
|
|
|
|
/// \brief Note that all of the arguments are references, and owned
|
|
|
|
/// by the caller.
|
|
|
|
/// \param Ignore - Set of names of callbacks to ignore.
|
|
|
|
/// \param CallbackCalls - Trace buffer.
|
|
|
|
/// \param PP - The preprocessor. Needed for getting some argument strings.
|
|
|
|
PPCallbacksTracker(llvm::SmallSet<std::string, 4> &Ignore,
|
|
|
|
std::vector<CallbackCall> &CallbackCalls,
|
|
|
|
clang::Preprocessor &PP);
|
|
|
|
|
2015-04-11 15:59:33 +08:00
|
|
|
~PPCallbacksTracker() override;
|
2013-10-31 20:23:32 +08:00
|
|
|
|
|
|
|
// Overidden callback functions.
|
|
|
|
|
|
|
|
void FileChanged(clang::SourceLocation Loc,
|
|
|
|
clang::PPCallbacks::FileChangeReason Reason,
|
|
|
|
clang::SrcMgr::CharacteristicKind FileType,
|
2014-03-02 18:20:11 +08:00
|
|
|
clang::FileID PrevFID = clang::FileID()) override;
|
2015-05-12 19:48:21 +08:00
|
|
|
void FileSkipped(const clang::FileEntry &SkippedFile,
|
2013-10-31 20:23:32 +08:00
|
|
|
const clang::Token &FilenameTok,
|
2014-03-02 18:20:11 +08:00
|
|
|
clang::SrcMgr::CharacteristicKind FileType) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
bool FileNotFound(llvm::StringRef FileName,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::SmallVectorImpl<char> &RecoveryPath) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void InclusionDirective(clang::SourceLocation HashLoc,
|
|
|
|
const clang::Token &IncludeTok,
|
|
|
|
llvm::StringRef FileName, bool IsAngled,
|
|
|
|
clang::CharSourceRange FilenameRange,
|
|
|
|
const clang::FileEntry *File,
|
|
|
|
llvm::StringRef SearchPath,
|
|
|
|
llvm::StringRef RelativePath,
|
2014-03-02 18:20:11 +08:00
|
|
|
const clang::Module *Imported) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void moduleImport(clang::SourceLocation ImportLoc, clang::ModuleIdPath Path,
|
2014-03-02 18:20:11 +08:00
|
|
|
const clang::Module *Imported) override;
|
|
|
|
void EndOfMainFile() override;
|
2015-06-02 04:00:20 +08:00
|
|
|
void Ident(clang::SourceLocation Loc, llvm::StringRef str) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaDirective(clang::SourceLocation Loc,
|
2014-03-02 18:20:11 +08:00
|
|
|
clang::PragmaIntroducerKind Introducer) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaComment(clang::SourceLocation Loc,
|
|
|
|
const clang::IdentifierInfo *Kind,
|
2015-06-02 04:00:20 +08:00
|
|
|
llvm::StringRef Str) override;
|
|
|
|
void PragmaDetectMismatch(clang::SourceLocation Loc, llvm::StringRef Name,
|
|
|
|
llvm::StringRef Value) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaDebug(clang::SourceLocation Loc,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef DebugType) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaMessage(clang::SourceLocation Loc, llvm::StringRef Namespace,
|
|
|
|
clang::PPCallbacks::PragmaMessageKind Kind,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef Str) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaDiagnosticPush(clang::SourceLocation Loc,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef Namespace) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaDiagnosticPop(clang::SourceLocation Loc,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef Namespace) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaDiagnostic(clang::SourceLocation Loc, llvm::StringRef Namespace,
|
2014-06-10 17:58:45 +08:00
|
|
|
clang::diag::Severity mapping,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef Str) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
|
|
|
|
const clang::IdentifierInfo *Name,
|
|
|
|
clang::SourceLocation StateLoc,
|
2014-03-02 18:20:11 +08:00
|
|
|
unsigned State) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void PragmaWarning(clang::SourceLocation Loc, llvm::StringRef WarningSpec,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::ArrayRef<int> Ids) override;
|
|
|
|
void PragmaWarningPush(clang::SourceLocation Loc, int Level) override;
|
|
|
|
void PragmaWarningPop(clang::SourceLocation Loc) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void MacroExpands(const clang::Token &MacroNameTok,
|
2015-05-04 11:15:55 +08:00
|
|
|
const clang::MacroDefinition &MD, clang::SourceRange Range,
|
2014-03-02 18:20:11 +08:00
|
|
|
const clang::MacroArgs *Args) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void MacroDefined(const clang::Token &MacroNameTok,
|
2014-03-02 18:20:11 +08:00
|
|
|
const clang::MacroDirective *MD) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void MacroUndefined(const clang::Token &MacroNameTok,
|
2015-05-04 11:15:55 +08:00
|
|
|
const clang::MacroDefinition &MD) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void Defined(const clang::Token &MacroNameTok,
|
2015-05-04 11:15:55 +08:00
|
|
|
const clang::MacroDefinition &MD,
|
2014-03-02 18:20:11 +08:00
|
|
|
clang::SourceRange Range) override;
|
|
|
|
void SourceRangeSkipped(clang::SourceRange Range) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
|
2014-03-02 18:20:11 +08:00
|
|
|
ConditionValueKind ConditionValue) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
|
2014-03-02 18:20:11 +08:00
|
|
|
ConditionValueKind ConditionValue, clang::SourceLocation IfLoc) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
|
2015-05-04 11:15:55 +08:00
|
|
|
const clang::MacroDefinition &MD) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
|
2015-05-04 11:15:55 +08:00
|
|
|
const clang::MacroDefinition &MD) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void Else(clang::SourceLocation Loc,
|
2014-03-02 18:20:11 +08:00
|
|
|
clang::SourceLocation IfLoc) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
void Endif(clang::SourceLocation Loc,
|
2014-03-02 18:20:11 +08:00
|
|
|
clang::SourceLocation IfLoc) override;
|
2013-10-31 20:23:32 +08:00
|
|
|
|
|
|
|
// Helper functions.
|
|
|
|
|
|
|
|
/// \brief Start a new callback.
|
|
|
|
void beginCallback(const char *Name);
|
|
|
|
|
|
|
|
/// \brief Append a string to the top trace item.
|
|
|
|
void append(const char *Str);
|
|
|
|
|
|
|
|
/// \brief Append a bool argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, bool Value);
|
|
|
|
|
|
|
|
/// \brief Append an int argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, int Value);
|
|
|
|
|
|
|
|
/// \brief Append a string argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const char *Value);
|
|
|
|
|
|
|
|
/// \brief Append a string reference object argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, llvm::StringRef Value);
|
|
|
|
|
|
|
|
/// \brief Append a string object argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const std::string &Value);
|
|
|
|
|
|
|
|
/// \brief Append a token argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::Token &Value);
|
|
|
|
|
|
|
|
/// \brief Append an enum argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, int Value, const char *Strings[]);
|
|
|
|
|
|
|
|
/// \brief Append a FileID argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, clang::FileID Value);
|
|
|
|
|
|
|
|
/// \brief Append a FileEntry argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::FileEntry *Value);
|
|
|
|
|
|
|
|
/// \brief Append a SourceLocation argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, clang::SourceLocation Value);
|
|
|
|
|
|
|
|
/// \brief Append a SourceRange argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, clang::SourceRange Value);
|
|
|
|
|
|
|
|
/// \brief Append a CharSourceRange argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, clang::CharSourceRange Value);
|
|
|
|
|
|
|
|
/// \brief Append a ModuleIdPath argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, clang::ModuleIdPath Value);
|
|
|
|
|
|
|
|
/// \brief Append an IdentifierInfo argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::IdentifierInfo *Value);
|
|
|
|
|
|
|
|
/// \brief Append a MacroDirective argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::MacroDirective *Value);
|
|
|
|
|
2015-05-04 11:15:55 +08:00
|
|
|
/// \brief Append a MacroDefinition argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::MacroDefinition &Value);
|
|
|
|
|
2013-10-31 20:23:32 +08:00
|
|
|
/// \brief Append a MacroArgs argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::MacroArgs *Value);
|
|
|
|
|
|
|
|
/// \brief Append a Module argument to the top trace item.
|
|
|
|
void appendArgument(const char *Name, const clang::Module *Value);
|
|
|
|
|
|
|
|
/// \brief Append a double-quoted argument to the top trace item.
|
2015-07-03 17:30:33 +08:00
|
|
|
void appendQuotedArgument(const char *Name, const std::string &Value);
|
2013-10-31 20:23:32 +08:00
|
|
|
|
|
|
|
/// \brief Append a double-quoted file path argument to the top trace item.
|
|
|
|
void appendFilePathArgument(const char *Name, llvm::StringRef Value);
|
|
|
|
|
|
|
|
/// \brief Get the raw source string of the range.
|
|
|
|
llvm::StringRef getSourceString(clang::CharSourceRange Range);
|
|
|
|
|
|
|
|
/// \brief Callback trace information.
|
|
|
|
/// We use a reference so the trace will be preserved for the caller
|
|
|
|
/// after this object is destructed.
|
|
|
|
std::vector<CallbackCall> &CallbackCalls;
|
|
|
|
|
|
|
|
/// \brief Names of callbacks to ignore.
|
|
|
|
llvm::SmallSet<std::string, 4> &Ignore;
|
|
|
|
|
|
|
|
/// \brief Inhibit trace while this is set.
|
|
|
|
bool DisableTrace;
|
|
|
|
|
|
|
|
clang::Preprocessor &PP;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PPTRACE_PPCALLBACKSTRACKER_H
|