2013-07-30 03:07:00 +08:00
|
|
|
//===--- PreprocessorTracker.cpp - Preprocessor tracking -*- C++ -*------===//
|
2013-07-27 07:56:42 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2013-07-30 03:07:00 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// The Basic Idea (Macro and Conditional Checking)
|
2013-07-27 07:56:42 +08:00
|
|
|
//
|
|
|
|
// Basically we install a PPCallbacks-derived object to track preprocessor
|
|
|
|
// activity, namely when a header file is entered/exited, when a macro
|
2013-07-30 03:07:00 +08:00
|
|
|
// is expanded, when "defined" is used, and when #if, #elif, #ifdef,
|
|
|
|
// and #ifndef are used. We save the state of macro and "defined"
|
2013-07-27 07:56:42 +08:00
|
|
|
// expressions in a map, keyed on a name/file/line/column quadruple.
|
2013-07-30 03:07:00 +08:00
|
|
|
// The map entries store the different states (values) that a macro expansion,
|
|
|
|
// "defined" expression, or condition expression has in the course of
|
2013-07-27 07:56:42 +08:00
|
|
|
// processing for the one location in the one header containing it,
|
|
|
|
// plus a list of the nested include stacks for the states. When a macro
|
2013-07-30 03:07:00 +08:00
|
|
|
// or "defined" expression evaluates to the same value, which is the
|
2013-07-27 07:56:42 +08:00
|
|
|
// desired case, only one state is stored. Similarly, for conditional
|
|
|
|
// directives, we save the condition expression states in a separate map.
|
|
|
|
//
|
|
|
|
// This information is collected as modularize compiles all the headers
|
|
|
|
// given to it to process. After all the compilations are performed,
|
2013-07-30 03:07:00 +08:00
|
|
|
// a check is performed for any entries in the maps that contain more
|
|
|
|
// than one different state, and for these an output message is generated.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// (...)/SubHeader.h:11:5:
|
|
|
|
// #if SYMBOL == 1
|
|
|
|
// ^
|
|
|
|
// error: Macro instance 'SYMBOL' has different values in this header,
|
|
|
|
// depending on how it was included.
|
|
|
|
// 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
|
|
|
|
// (...)/Header1.h
|
|
|
|
// (...)/SubHeader.h
|
|
|
|
// (...)/SubHeader.h:3:9:
|
|
|
|
// #define SYMBOL 1
|
|
|
|
// ^
|
|
|
|
// Macro defined here.
|
|
|
|
// 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
|
|
|
|
// (...)/Header2.h
|
|
|
|
// (...)/SubHeader.h
|
|
|
|
// (...)/SubHeader.h:7:9:
|
|
|
|
// #define SYMBOL 2
|
|
|
|
// ^
|
|
|
|
// Macro defined here.
|
2013-07-27 07:56:42 +08:00
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// The Basic Idea ('Extern "C/C++" {}' Or 'namespace {}') With Nested
|
|
|
|
// '#include' Checking)
|
|
|
|
//
|
|
|
|
// To check for '#include' directives nested inside 'Extern "C/C++" {}'
|
|
|
|
// or 'namespace {}' blocks, we keep track of the '#include' directives
|
|
|
|
// while running the preprocessor, and later during a walk of the AST
|
|
|
|
// we call a function to check for any '#include' directies inside
|
|
|
|
// an 'Extern "C/C++" {}' or 'namespace {}' block, given its source
|
|
|
|
// range.
|
|
|
|
//
|
|
|
|
// Design and Implementation Details (Macro and Conditional Checking)
|
2013-07-27 07:56:42 +08:00
|
|
|
//
|
|
|
|
// A PreprocessorTrackerImpl class implements the PreprocessorTracker
|
|
|
|
// interface. It uses a PreprocessorCallbacks class derived from PPCallbacks
|
|
|
|
// to track preprocessor activity, namely entering/exiting a header, macro
|
2013-07-30 03:07:00 +08:00
|
|
|
// expansions, use of "defined" expressions, and #if, #elif, #ifdef, and
|
2013-07-27 07:56:42 +08:00
|
|
|
// #ifndef conditional directives. PreprocessorTrackerImpl stores a map
|
|
|
|
// of MacroExpansionTracker objects keyed on a name/file/line/column
|
|
|
|
// value represented by a light-weight PPItemKey value object. This
|
|
|
|
// is the key top-level data structure tracking the values of macro
|
|
|
|
// expansion instances. Similarly, it stores a map of ConditionalTracker
|
|
|
|
// objects with the same kind of key, for tracking preprocessor conditional
|
|
|
|
// directives.
|
|
|
|
//
|
|
|
|
// The MacroExpansionTracker object represents one macro reference or use
|
2013-07-30 03:07:00 +08:00
|
|
|
// of a "defined" expression in a header file. It stores a handle to a
|
2013-07-27 07:56:42 +08:00
|
|
|
// string representing the unexpanded macro instance, a handle to a string
|
|
|
|
// representing the unpreprocessed source line containing the unexpanded
|
|
|
|
// macro instance, and a vector of one or more MacroExpansionInstance
|
|
|
|
// objects.
|
|
|
|
//
|
|
|
|
// The MacroExpansionInstance object represents one or more expansions
|
|
|
|
// of a macro reference, for the case where the macro expands to the same
|
|
|
|
// value. MacroExpansionInstance stores a handle to a string representing
|
|
|
|
// the expanded macro value, a PPItemKey representing the file/line/column
|
|
|
|
// where the macro was defined, a handle to a string representing the source
|
|
|
|
// line containing the macro definition, and a vector of InclusionPathHandle
|
2013-09-04 02:44:11 +08:00
|
|
|
// values that represents the hierarchies of include files for each case
|
2013-07-27 07:56:42 +08:00
|
|
|
// where the particular header containing the macro reference was referenced
|
|
|
|
// or included.
|
|
|
|
|
|
|
|
// In the normal case where a macro instance always expands to the same
|
|
|
|
// value, the MacroExpansionTracker object will only contain one
|
|
|
|
// MacroExpansionInstance representing all the macro expansion instances.
|
|
|
|
// If a case was encountered where a macro instance expands to a value
|
|
|
|
// that is different from that seen before, or the macro was defined in
|
|
|
|
// a different place, a new MacroExpansionInstance object representing
|
|
|
|
// that case will be added to the vector in MacroExpansionTracker. If a
|
|
|
|
// macro instance expands to a value already seen before, the
|
2013-07-30 03:07:00 +08:00
|
|
|
// InclusionPathHandle representing that case's include file hierarchy
|
2013-07-27 07:56:42 +08:00
|
|
|
// will be added to the existing MacroExpansionInstance object.
|
|
|
|
|
|
|
|
// For checking conditional directives, the ConditionalTracker class
|
|
|
|
// functions similarly to MacroExpansionTracker, but tracks an #if,
|
|
|
|
// #elif, #ifdef, or #ifndef directive in a header file. It stores
|
|
|
|
// a vector of one or two ConditionalExpansionInstance objects,
|
|
|
|
// representing the cases where the conditional expression evaluates
|
|
|
|
// to true or false. This latter object stores the evaluated value
|
|
|
|
// of the condition expression (a bool) and a vector of
|
|
|
|
// InclusionPathHandles.
|
|
|
|
//
|
|
|
|
// To reduce the instances of string and object copying, the
|
|
|
|
// PreprocessorTrackerImpl class uses a StringPool to save all stored
|
|
|
|
// strings, and defines a StringHandle type to abstract the references
|
|
|
|
// to the strings.
|
|
|
|
//
|
|
|
|
// PreprocessorTrackerImpl also maintains a list representing the unique
|
2013-07-30 03:07:00 +08:00
|
|
|
// headers, which is just a vector of StringHandle's for the header file
|
2013-07-27 07:56:42 +08:00
|
|
|
// paths. A HeaderHandle abstracts a reference to a header, and is simply
|
|
|
|
// the index of the stored header file path.
|
|
|
|
//
|
2013-07-30 03:07:00 +08:00
|
|
|
// A HeaderInclusionPath class abstracts a unique hierarchy of header file
|
2013-07-27 07:56:42 +08:00
|
|
|
// inclusions. It simply stores a vector of HeaderHandles ordered from the
|
|
|
|
// top-most header (the one from the header list passed to modularize) down
|
|
|
|
// to the header containing the macro reference. PreprocessorTrackerImpl
|
|
|
|
// stores a vector of these objects. An InclusionPathHandle typedef
|
|
|
|
// abstracts a reference to one of the HeaderInclusionPath objects, and is
|
|
|
|
// simply the index of the stored HeaderInclusionPath object. The
|
2013-07-30 03:07:00 +08:00
|
|
|
// MacroExpansionInstance object stores a vector of these handles so that
|
2013-07-27 07:56:42 +08:00
|
|
|
// the reporting function can display the include hierarchies for the macro
|
|
|
|
// expansion instances represented by that object, to help the user
|
|
|
|
// understand how the header was included. (A future enhancement might
|
|
|
|
// be to associate a line number for the #include directives, but I
|
|
|
|
// think not doing so is good enough for the present.)
|
|
|
|
//
|
|
|
|
// A key reason for using these opaque handles was to try to keep all the
|
|
|
|
// internal objects light-weight value objects, in order to reduce string
|
|
|
|
// and object copying overhead, and to abstract this implementation detail.
|
|
|
|
//
|
|
|
|
// The key data structures are built up while modularize runs the headers
|
|
|
|
// through the compilation. A PreprocessorTracker instance is created and
|
|
|
|
// passed down to the AST action and consumer objects in modularize. For
|
|
|
|
// each new compilation instance, the consumer calls the
|
2013-07-30 03:07:00 +08:00
|
|
|
// PreprocessorTracker's handleNewPreprocessorEntry function, which sets
|
2013-07-27 07:56:42 +08:00
|
|
|
// up a PreprocessorCallbacks object for the preprocessor. At the end of
|
2013-07-30 03:07:00 +08:00
|
|
|
// the compilation instance, the PreprocessorTracker's
|
2013-07-27 07:56:42 +08:00
|
|
|
// handleNewPreprocessorExit function handles cleaning up with respect
|
|
|
|
// to the preprocessing instance.
|
|
|
|
//
|
|
|
|
// The PreprocessorCallbacks object uses an overidden FileChanged callback
|
|
|
|
// to determine when a header is entered and exited (including exiting the
|
2013-07-30 03:07:00 +08:00
|
|
|
// header during #include directives). It calls PreprocessorTracker's
|
2013-07-27 07:56:42 +08:00
|
|
|
// handleHeaderEntry and handleHeaderExit functions upon entering and
|
|
|
|
// exiting a header. These functions manage a stack of header handles
|
|
|
|
// representing by a vector, pushing and popping header handles as headers
|
|
|
|
// are entered and exited. When a HeaderInclusionPath object is created,
|
|
|
|
// it simply copies this stack.
|
|
|
|
//
|
|
|
|
// The PreprocessorCallbacks object uses an overridden MacroExpands callback
|
|
|
|
// to track when a macro expansion is performed. It calls a couple of helper
|
|
|
|
// functions to get the unexpanded and expanded macro values as strings, but
|
2013-07-30 03:07:00 +08:00
|
|
|
// then calls PreprocessorTrackerImpl's addMacroExpansionInstance function to
|
2013-07-27 07:56:42 +08:00
|
|
|
// do the rest of the work. The getMacroExpandedString function uses the
|
2013-07-30 03:07:00 +08:00
|
|
|
// preprocessor's getSpelling to convert tokens to strings using the
|
2013-07-27 07:56:42 +08:00
|
|
|
// information passed to the MacroExpands callback, and simply concatenates
|
|
|
|
// them. It makes recursive calls to itself to handle nested macro
|
|
|
|
// definitions, and also handles function-style macros.
|
|
|
|
//
|
2013-07-30 03:07:00 +08:00
|
|
|
// PreprocessorTrackerImpl's addMacroExpansionInstance function looks for
|
2013-07-27 07:56:42 +08:00
|
|
|
// an existing MacroExpansionTracker entry in its map of MacroExampleTracker
|
|
|
|
// objects. If none exists, it adds one with one MacroExpansionInstance and
|
|
|
|
// returns. If a MacroExpansionTracker object already exists, it looks for
|
|
|
|
// an existing MacroExpansionInstance object stored in the
|
|
|
|
// MacroExpansionTracker object, one that matches the macro expanded value
|
|
|
|
// and the macro definition location. If a matching MacroExpansionInstance
|
|
|
|
// object is found, it just adds the current HeaderInclusionPath object to
|
|
|
|
// it. If not found, it creates and stores a new MacroExpantionInstance
|
|
|
|
// object. The addMacroExpansionInstance function calls a couple of helper
|
|
|
|
// functions to get the pre-formatted location and source line strings for
|
|
|
|
// the macro reference and the macro definition stored as string handles.
|
|
|
|
// These helper functions use the current source manager from the
|
|
|
|
// preprocessor. This is done in advance at this point in time because the
|
2013-07-30 03:07:00 +08:00
|
|
|
// source manager doesn't exist at the time of the reporting.
|
2013-07-27 07:56:42 +08:00
|
|
|
//
|
|
|
|
// For conditional check, the PreprocessorCallbacks class overrides the
|
|
|
|
// PPCallbacks handlers for #if, #elif, #ifdef, and #ifndef. These handlers
|
|
|
|
// call the addConditionalExpansionInstance method of
|
|
|
|
// PreprocessorTrackerImpl. The process is similar to that of macros, but
|
|
|
|
// with some different data and error messages. A lookup is performed for
|
2013-07-30 03:07:00 +08:00
|
|
|
// the conditional, and if a ConditionalTracker object doesn't yet exist for
|
2013-07-27 07:56:42 +08:00
|
|
|
// the conditional, a new one is added, including adding a
|
|
|
|
// ConditionalExpansionInstance object to it to represent the condition
|
|
|
|
// expression state. If a ConditionalTracker for the conditional does
|
|
|
|
// exist, a lookup is made for a ConditionalExpansionInstance object
|
|
|
|
// matching the condition expression state. If one exists, a
|
|
|
|
// HeaderInclusionPath is added to it. Otherwise a new
|
|
|
|
// ConditionalExpansionInstance entry is made. If a ConditionalTracker
|
|
|
|
// has two ConditionalExpansionInstance objects, it means there was a
|
|
|
|
// conflict, meaning the conditional expression evaluated differently in
|
|
|
|
// one or more cases.
|
2013-09-04 02:44:11 +08:00
|
|
|
//
|
2013-07-27 07:56:42 +08:00
|
|
|
// After modularize has performed all the compilations, it enters a phase
|
|
|
|
// of error reporting. This new feature adds to this reporting phase calls
|
2013-07-30 03:07:00 +08:00
|
|
|
// to the PreprocessorTracker's reportInconsistentMacros and
|
2013-07-27 07:56:42 +08:00
|
|
|
// reportInconsistentConditionals functions. These functions walk the maps
|
2013-07-30 03:07:00 +08:00
|
|
|
// of MacroExpansionTracker's and ConditionalTracker's respectively. If
|
2013-07-27 07:56:42 +08:00
|
|
|
// any of these objects have more than one MacroExpansionInstance or
|
|
|
|
// ConditionalExpansionInstance objects, it formats and outputs an error
|
|
|
|
// message like the example shown previously, using the stored data.
|
|
|
|
//
|
|
|
|
// A potential issue is that there is some overlap between the #if/#elif
|
|
|
|
// conditional and macro reporting. I could disable the #if and #elif,
|
2013-07-30 03:07:00 +08:00
|
|
|
// leaving just the #ifdef and #ifndef, since these don't overlap. Or,
|
2013-07-27 07:56:42 +08:00
|
|
|
// to make clearer the separate reporting phases, I could add an output
|
|
|
|
// message marking the phases.
|
|
|
|
//
|
2013-09-19 02:19:43 +08:00
|
|
|
// Design and Implementation Details ('Extern "C/C++" {}' Or
|
|
|
|
// 'namespace {}') With Nested '#include' Checking)
|
|
|
|
//
|
|
|
|
// We override the InclusionDirective in PPCallbacks to record information
|
|
|
|
// about each '#include' directive encountered during preprocessing.
|
|
|
|
// We co-opt the PPItemKey class to store the information about each
|
|
|
|
// '#include' directive, including the source file name containing the
|
|
|
|
// directive, the name of the file being included, and the source line
|
|
|
|
// and column of the directive. We store these object in a vector,
|
|
|
|
// after first check to see if an entry already exists.
|
|
|
|
//
|
|
|
|
// Later, while the AST is being walked for other checks, we provide
|
|
|
|
// visit handlers for 'extern "C/C++" {}' and 'namespace (name) {}'
|
|
|
|
// blocks, checking to see if any '#include' directives occurred
|
|
|
|
// within the blocks, reporting errors if any found.
|
|
|
|
//
|
2013-07-27 07:56:42 +08:00
|
|
|
// Future Directions
|
|
|
|
//
|
|
|
|
// We probably should add options to disable any of the checks, in case
|
|
|
|
// there is some problem with them, or the messages get too verbose.
|
|
|
|
//
|
|
|
|
// With the map of all the macro and conditional expansion instances,
|
|
|
|
// it might be possible to add to the existing modularize error messages
|
|
|
|
// (the second part referring to definitions being different), attempting
|
|
|
|
// to tie them to the last macro conflict encountered with respect to the
|
|
|
|
// order of the code encountered.
|
|
|
|
//
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "PreprocessorTracker.h"
|
2013-07-27 07:56:42 +08:00
|
|
|
#include "clang/Lex/MacroArgs.h"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2013-08-08 02:49:47 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "llvm/Support/StringPool.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-02-27 03:31:10 +08:00
|
|
|
#include "ModularizeUtilities.h"
|
2013-07-27 07:56:42 +08:00
|
|
|
|
|
|
|
namespace Modularize {
|
|
|
|
|
|
|
|
// Some handle types
|
|
|
|
typedef llvm::PooledStringPtr StringHandle;
|
|
|
|
|
|
|
|
typedef int HeaderHandle;
|
|
|
|
const HeaderHandle HeaderHandleInvalid = -1;
|
|
|
|
|
|
|
|
typedef int InclusionPathHandle;
|
|
|
|
const InclusionPathHandle InclusionPathHandleInvalid = -1;
|
|
|
|
|
|
|
|
// Some utility functions.
|
|
|
|
|
|
|
|
// Get a "file:line:column" source location string.
|
|
|
|
static std::string getSourceLocationString(clang::Preprocessor &PP,
|
|
|
|
clang::SourceLocation Loc) {
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return std::string("(none)");
|
|
|
|
else
|
|
|
|
return Loc.printToString(PP.getSourceManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get just the file name from a source location.
|
|
|
|
static std::string getSourceLocationFile(clang::Preprocessor &PP,
|
|
|
|
clang::SourceLocation Loc) {
|
|
|
|
std::string Source(getSourceLocationString(PP, Loc));
|
|
|
|
size_t Offset = Source.find(':', 2);
|
|
|
|
if (Offset == std::string::npos)
|
|
|
|
return Source;
|
|
|
|
return Source.substr(0, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get just the line and column from a source location.
|
|
|
|
static void getSourceLocationLineAndColumn(clang::Preprocessor &PP,
|
|
|
|
clang::SourceLocation Loc, int &Line,
|
|
|
|
int &Column) {
|
|
|
|
clang::PresumedLoc PLoc = PP.getSourceManager().getPresumedLoc(Loc);
|
|
|
|
if (PLoc.isInvalid()) {
|
|
|
|
Line = 0;
|
|
|
|
Column = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Line = PLoc.getLine();
|
|
|
|
Column = PLoc.getColumn();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve source snippet from file image.
|
2015-03-23 20:49:15 +08:00
|
|
|
static std::string getSourceString(clang::Preprocessor &PP,
|
|
|
|
clang::SourceRange Range) {
|
2013-07-27 07:56:42 +08:00
|
|
|
clang::SourceLocation BeginLoc = Range.getBegin();
|
|
|
|
clang::SourceLocation EndLoc = Range.getEnd();
|
|
|
|
const char *BeginPtr = PP.getSourceManager().getCharacterData(BeginLoc);
|
|
|
|
const char *EndPtr = PP.getSourceManager().getCharacterData(EndLoc);
|
|
|
|
size_t Length = EndPtr - BeginPtr;
|
|
|
|
return llvm::StringRef(BeginPtr, Length).trim().str();
|
|
|
|
}
|
|
|
|
|
2013-09-19 02:19:43 +08:00
|
|
|
// Retrieve source line from file image given a location.
|
2015-03-23 20:49:15 +08:00
|
|
|
static std::string getSourceLine(clang::Preprocessor &PP,
|
|
|
|
clang::SourceLocation Loc) {
|
2013-07-27 07:56:42 +08:00
|
|
|
const llvm::MemoryBuffer *MemBuffer =
|
|
|
|
PP.getSourceManager().getBuffer(PP.getSourceManager().getFileID(Loc));
|
|
|
|
const char *Buffer = MemBuffer->getBufferStart();
|
|
|
|
const char *BufferEnd = MemBuffer->getBufferEnd();
|
|
|
|
const char *BeginPtr = PP.getSourceManager().getCharacterData(Loc);
|
|
|
|
const char *EndPtr = BeginPtr;
|
|
|
|
while (BeginPtr > Buffer) {
|
|
|
|
if (*BeginPtr == '\n') {
|
|
|
|
BeginPtr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BeginPtr--;
|
|
|
|
}
|
|
|
|
while (EndPtr < BufferEnd) {
|
|
|
|
if (*EndPtr == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EndPtr++;
|
|
|
|
}
|
|
|
|
size_t Length = EndPtr - BeginPtr;
|
|
|
|
return llvm::StringRef(BeginPtr, Length).str();
|
|
|
|
}
|
|
|
|
|
2013-09-19 02:19:43 +08:00
|
|
|
// Retrieve source line from file image given a file ID and line number.
|
2015-03-23 20:49:15 +08:00
|
|
|
static std::string getSourceLine(clang::Preprocessor &PP, clang::FileID FileID,
|
|
|
|
int Line) {
|
2013-09-19 02:19:43 +08:00
|
|
|
const llvm::MemoryBuffer *MemBuffer = PP.getSourceManager().getBuffer(FileID);
|
|
|
|
const char *Buffer = MemBuffer->getBufferStart();
|
|
|
|
const char *BufferEnd = MemBuffer->getBufferEnd();
|
|
|
|
const char *BeginPtr = Buffer;
|
|
|
|
const char *EndPtr = BufferEnd;
|
|
|
|
int LineCounter = 1;
|
|
|
|
if (Line == 1)
|
|
|
|
BeginPtr = Buffer;
|
|
|
|
else {
|
|
|
|
while (Buffer < BufferEnd) {
|
|
|
|
if (*Buffer == '\n') {
|
|
|
|
if (++LineCounter == Line) {
|
|
|
|
BeginPtr = Buffer++ + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Buffer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (Buffer < BufferEnd) {
|
|
|
|
if (*Buffer == '\n') {
|
|
|
|
EndPtr = Buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Buffer++;
|
|
|
|
}
|
|
|
|
size_t Length = EndPtr - BeginPtr;
|
|
|
|
return llvm::StringRef(BeginPtr, Length).str();
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Get the string for the Unexpanded macro instance.
|
|
|
|
// The soureRange is expected to end at the last token
|
|
|
|
// for the macro instance, which in the case of a function-style
|
|
|
|
// macro will be a ')', but for an object-style macro, it
|
|
|
|
// will be the macro name itself.
|
2015-03-23 20:49:15 +08:00
|
|
|
static std::string getMacroUnexpandedString(clang::SourceRange Range,
|
|
|
|
clang::Preprocessor &PP,
|
|
|
|
llvm::StringRef MacroName,
|
|
|
|
const clang::MacroInfo *MI) {
|
2013-07-27 07:56:42 +08:00
|
|
|
clang::SourceLocation BeginLoc(Range.getBegin());
|
|
|
|
const char *BeginPtr = PP.getSourceManager().getCharacterData(BeginLoc);
|
|
|
|
size_t Length;
|
|
|
|
std::string Unexpanded;
|
|
|
|
if (MI->isFunctionLike()) {
|
|
|
|
clang::SourceLocation EndLoc(Range.getEnd());
|
|
|
|
const char *EndPtr = PP.getSourceManager().getCharacterData(EndLoc) + 1;
|
|
|
|
Length = (EndPtr - BeginPtr) + 1; // +1 is ')' width.
|
|
|
|
} else
|
|
|
|
Length = MacroName.size();
|
|
|
|
return llvm::StringRef(BeginPtr, Length).trim().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the expansion for a macro instance, given the information
|
|
|
|
// provided by PPCallbacks.
|
2013-08-09 08:22:20 +08:00
|
|
|
// FIXME: This doesn't support function-style macro instances
|
|
|
|
// passed as arguments to another function-style macro. However,
|
|
|
|
// since it still expands the inner arguments, it still
|
|
|
|
// allows modularize to effectively work with respect to macro
|
|
|
|
// consistency checking, although it displays the incorrect
|
|
|
|
// expansion in error messages.
|
2015-03-23 20:49:15 +08:00
|
|
|
static std::string getMacroExpandedString(clang::Preprocessor &PP,
|
|
|
|
llvm::StringRef MacroName,
|
|
|
|
const clang::MacroInfo *MI,
|
|
|
|
const clang::MacroArgs *Args) {
|
2013-07-27 07:56:42 +08:00
|
|
|
std::string Expanded;
|
|
|
|
// Walk over the macro Tokens.
|
|
|
|
typedef clang::MacroInfo::tokens_iterator Iter;
|
|
|
|
for (Iter I = MI->tokens_begin(), E = MI->tokens_end(); I != E; ++I) {
|
|
|
|
clang::IdentifierInfo *II = I->getIdentifierInfo();
|
|
|
|
int ArgNo = (II && Args ? MI->getArgumentNum(II) : -1);
|
|
|
|
if (ArgNo == -1) {
|
|
|
|
// This isn't an argument, just add it.
|
2014-06-09 10:03:06 +08:00
|
|
|
if (II == nullptr)
|
2013-07-27 07:56:42 +08:00
|
|
|
Expanded += PP.getSpelling((*I)); // Not an identifier.
|
|
|
|
else {
|
|
|
|
// Token is for an identifier.
|
|
|
|
std::string Name = II->getName().str();
|
|
|
|
// Check for nexted macro references.
|
|
|
|
clang::MacroInfo *MacroInfo = PP.getMacroInfo(II);
|
2014-06-09 10:03:06 +08:00
|
|
|
if (MacroInfo)
|
|
|
|
Expanded += getMacroExpandedString(PP, Name, MacroInfo, nullptr);
|
2013-07-27 07:56:42 +08:00
|
|
|
else
|
|
|
|
Expanded += Name;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We get here if it's a function-style macro with arguments.
|
|
|
|
const clang::Token *ResultArgToks;
|
|
|
|
const clang::Token *ArgTok = Args->getUnexpArgument(ArgNo);
|
|
|
|
if (Args->ArgNeedsPreexpansion(ArgTok, PP))
|
|
|
|
ResultArgToks = &(const_cast<clang::MacroArgs *>(Args))
|
|
|
|
->getPreExpArgument(ArgNo, MI, PP)[0];
|
|
|
|
else
|
|
|
|
ResultArgToks = ArgTok; // Use non-preexpanded Tokens.
|
|
|
|
// If the arg token didn't expand into anything, ignore it.
|
|
|
|
if (ResultArgToks->is(clang::tok::eof))
|
|
|
|
continue;
|
|
|
|
unsigned NumToks = clang::MacroArgs::getArgLength(ResultArgToks);
|
|
|
|
// Append the resulting argument expansions.
|
|
|
|
for (unsigned ArgumentIndex = 0; ArgumentIndex < NumToks; ++ArgumentIndex) {
|
|
|
|
const clang::Token &AT = ResultArgToks[ArgumentIndex];
|
|
|
|
clang::IdentifierInfo *II = AT.getIdentifierInfo();
|
2014-06-09 10:03:06 +08:00
|
|
|
if (II == nullptr)
|
2013-07-27 07:56:42 +08:00
|
|
|
Expanded += PP.getSpelling(AT); // Not an identifier.
|
|
|
|
else {
|
|
|
|
// It's an identifier. Check for further expansion.
|
|
|
|
std::string Name = II->getName().str();
|
|
|
|
clang::MacroInfo *MacroInfo = PP.getMacroInfo(II);
|
2014-06-09 10:03:06 +08:00
|
|
|
if (MacroInfo)
|
|
|
|
Expanded += getMacroExpandedString(PP, Name, MacroInfo, nullptr);
|
2013-07-27 07:56:42 +08:00
|
|
|
else
|
|
|
|
Expanded += Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Expanded;
|
|
|
|
}
|
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
namespace {
|
2013-07-27 07:56:42 +08:00
|
|
|
|
2013-12-07 16:41:15 +08:00
|
|
|
// ConditionValueKind strings.
|
|
|
|
const char *
|
|
|
|
ConditionValueKindStrings[] = {
|
|
|
|
"(not evaluated)", "false", "true"
|
|
|
|
};
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
bool operator<(const StringHandle &H1, const StringHandle &H2) {
|
|
|
|
const char *S1 = (H1 ? *H1 : "");
|
|
|
|
const char *S2 = (H2 ? *H2 : "");
|
|
|
|
int Diff = strcmp(S1, S2);
|
|
|
|
return Diff < 0;
|
|
|
|
}
|
|
|
|
bool operator>(const StringHandle &H1, const StringHandle &H2) {
|
|
|
|
const char *S1 = (H1 ? *H1 : "");
|
|
|
|
const char *S2 = (H2 ? *H2 : "");
|
|
|
|
int Diff = strcmp(S1, S2);
|
|
|
|
return Diff > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preprocessor item key.
|
|
|
|
//
|
|
|
|
// This class represents a location in a source file, for use
|
|
|
|
// as a key representing a unique name/file/line/column quadruplet,
|
|
|
|
// which in this case is used to identify a macro expansion instance,
|
|
|
|
// but could be used for other things as well.
|
|
|
|
// The file is a header file handle, the line is a line number,
|
|
|
|
// and the column is a column number.
|
|
|
|
class PPItemKey {
|
|
|
|
public:
|
|
|
|
PPItemKey(clang::Preprocessor &PP, StringHandle Name, HeaderHandle File,
|
|
|
|
clang::SourceLocation Loc)
|
|
|
|
: Name(Name), File(File) {
|
|
|
|
getSourceLocationLineAndColumn(PP, Loc, Line, Column);
|
|
|
|
}
|
|
|
|
PPItemKey(StringHandle Name, HeaderHandle File, int Line, int Column)
|
|
|
|
: Name(Name), File(File), Line(Line), Column(Column) {}
|
|
|
|
PPItemKey(const PPItemKey &Other)
|
|
|
|
: Name(Other.Name), File(Other.File), Line(Other.Line),
|
|
|
|
Column(Other.Column) {}
|
|
|
|
PPItemKey() : File(HeaderHandleInvalid), Line(0), Column(0) {}
|
|
|
|
bool operator==(const PPItemKey &Other) const {
|
|
|
|
if (Name != Other.Name)
|
|
|
|
return false;
|
|
|
|
if (File != Other.File)
|
|
|
|
return false;
|
|
|
|
if (Line != Other.Line)
|
|
|
|
return false;
|
|
|
|
return Column == Other.Column;
|
|
|
|
}
|
|
|
|
bool operator<(const PPItemKey &Other) const {
|
|
|
|
if (Name < Other.Name)
|
|
|
|
return true;
|
|
|
|
else if (Name > Other.Name)
|
|
|
|
return false;
|
|
|
|
if (File < Other.File)
|
|
|
|
return true;
|
|
|
|
else if (File > Other.File)
|
|
|
|
return false;
|
|
|
|
if (Line < Other.Line)
|
|
|
|
return true;
|
|
|
|
else if (Line > Other.Line)
|
|
|
|
return false;
|
|
|
|
return Column < Other.Column;
|
|
|
|
}
|
|
|
|
StringHandle Name;
|
|
|
|
HeaderHandle File;
|
|
|
|
int Line;
|
|
|
|
int Column;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Header inclusion path.
|
|
|
|
class HeaderInclusionPath {
|
|
|
|
public:
|
|
|
|
HeaderInclusionPath(std::vector<HeaderHandle> HeaderInclusionPath)
|
|
|
|
: Path(HeaderInclusionPath) {}
|
|
|
|
HeaderInclusionPath(const HeaderInclusionPath &Other) : Path(Other.Path) {}
|
|
|
|
HeaderInclusionPath() {}
|
|
|
|
std::vector<HeaderHandle> Path;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Macro expansion instance.
|
|
|
|
//
|
|
|
|
// This class represents an instance of a macro expansion with a
|
|
|
|
// unique value. It also stores the unique header inclusion paths
|
2013-07-30 03:07:00 +08:00
|
|
|
// for use in telling the user the nested include path to the header.
|
2013-07-27 07:56:42 +08:00
|
|
|
class MacroExpansionInstance {
|
|
|
|
public:
|
|
|
|
MacroExpansionInstance(StringHandle MacroExpanded,
|
|
|
|
PPItemKey &DefinitionLocation,
|
|
|
|
StringHandle DefinitionSourceLine,
|
|
|
|
InclusionPathHandle H)
|
|
|
|
: MacroExpanded(MacroExpanded), DefinitionLocation(DefinitionLocation),
|
|
|
|
DefinitionSourceLine(DefinitionSourceLine) {
|
|
|
|
InclusionPathHandles.push_back(H);
|
|
|
|
}
|
|
|
|
MacroExpansionInstance() {}
|
|
|
|
|
|
|
|
// Check for the presence of a header inclusion path handle entry.
|
|
|
|
// Return false if not found.
|
|
|
|
bool haveInclusionPathHandle(InclusionPathHandle H) {
|
|
|
|
for (std::vector<InclusionPathHandle>::iterator
|
|
|
|
I = InclusionPathHandles.begin(),
|
|
|
|
E = InclusionPathHandles.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (*I == H)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return InclusionPathHandleInvalid;
|
|
|
|
}
|
|
|
|
// Add a new header inclusion path entry, if not already present.
|
|
|
|
void addInclusionPathHandle(InclusionPathHandle H) {
|
|
|
|
if (!haveInclusionPathHandle(H))
|
|
|
|
InclusionPathHandles.push_back(H);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A string representing the macro instance after preprocessing.
|
|
|
|
StringHandle MacroExpanded;
|
|
|
|
// A file/line/column triplet representing the macro definition location.
|
|
|
|
PPItemKey DefinitionLocation;
|
|
|
|
// A place to save the macro definition line string.
|
|
|
|
StringHandle DefinitionSourceLine;
|
|
|
|
// The header inclusion path handles for all the instances.
|
|
|
|
std::vector<InclusionPathHandle> InclusionPathHandles;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Macro expansion instance tracker.
|
|
|
|
//
|
|
|
|
// This class represents one macro expansion, keyed by a PPItemKey.
|
|
|
|
// It stores a string representing the macro reference in the source,
|
|
|
|
// and a list of ConditionalExpansionInstances objects representing
|
2013-07-30 03:07:00 +08:00
|
|
|
// the unique values the condition expands to in instances of the header.
|
2013-07-27 07:56:42 +08:00
|
|
|
class MacroExpansionTracker {
|
|
|
|
public:
|
|
|
|
MacroExpansionTracker(StringHandle MacroUnexpanded,
|
|
|
|
StringHandle MacroExpanded,
|
|
|
|
StringHandle InstanceSourceLine,
|
|
|
|
PPItemKey &DefinitionLocation,
|
|
|
|
StringHandle DefinitionSourceLine,
|
|
|
|
InclusionPathHandle InclusionPathHandle)
|
|
|
|
: MacroUnexpanded(MacroUnexpanded),
|
|
|
|
InstanceSourceLine(InstanceSourceLine) {
|
|
|
|
addMacroExpansionInstance(MacroExpanded, DefinitionLocation,
|
|
|
|
DefinitionSourceLine, InclusionPathHandle);
|
|
|
|
}
|
|
|
|
MacroExpansionTracker() {}
|
|
|
|
|
|
|
|
// Find a matching macro expansion instance.
|
|
|
|
MacroExpansionInstance *
|
|
|
|
findMacroExpansionInstance(StringHandle MacroExpanded,
|
|
|
|
PPItemKey &DefinitionLocation) {
|
|
|
|
for (std::vector<MacroExpansionInstance>::iterator
|
|
|
|
I = MacroExpansionInstances.begin(),
|
|
|
|
E = MacroExpansionInstances.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if ((I->MacroExpanded == MacroExpanded) &&
|
|
|
|
(I->DefinitionLocation == DefinitionLocation)) {
|
|
|
|
return &*I; // Found.
|
|
|
|
}
|
|
|
|
}
|
2014-06-09 10:03:06 +08:00
|
|
|
return nullptr; // Not found.
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a macro expansion instance.
|
|
|
|
void addMacroExpansionInstance(StringHandle MacroExpanded,
|
|
|
|
PPItemKey &DefinitionLocation,
|
|
|
|
StringHandle DefinitionSourceLine,
|
|
|
|
InclusionPathHandle InclusionPathHandle) {
|
|
|
|
MacroExpansionInstances.push_back(
|
|
|
|
MacroExpansionInstance(MacroExpanded, DefinitionLocation,
|
|
|
|
DefinitionSourceLine, InclusionPathHandle));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if there is a mismatch.
|
|
|
|
bool hasMismatch() { return MacroExpansionInstances.size() > 1; }
|
|
|
|
|
|
|
|
// A string representing the macro instance without expansion.
|
|
|
|
StringHandle MacroUnexpanded;
|
|
|
|
// A place to save the macro instance source line string.
|
|
|
|
StringHandle InstanceSourceLine;
|
|
|
|
// The macro expansion instances.
|
|
|
|
// If all instances of the macro expansion expand to the same value,
|
|
|
|
// This vector will only have one instance.
|
|
|
|
std::vector<MacroExpansionInstance> MacroExpansionInstances;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Conditional expansion instance.
|
|
|
|
//
|
2013-07-30 03:07:00 +08:00
|
|
|
// This class represents an instance of a condition exoression result
|
|
|
|
// with a unique value. It also stores the unique header inclusion paths
|
|
|
|
// for use in telling the user the nested include path to the header.
|
2013-07-27 07:56:42 +08:00
|
|
|
class ConditionalExpansionInstance {
|
|
|
|
public:
|
2013-12-07 16:41:15 +08:00
|
|
|
ConditionalExpansionInstance(clang::PPCallbacks::ConditionValueKind ConditionValue, InclusionPathHandle H)
|
2013-07-27 07:56:42 +08:00
|
|
|
: ConditionValue(ConditionValue) {
|
|
|
|
InclusionPathHandles.push_back(H);
|
|
|
|
}
|
|
|
|
ConditionalExpansionInstance() {}
|
|
|
|
|
|
|
|
// Check for the presence of a header inclusion path handle entry.
|
|
|
|
// Return false if not found.
|
|
|
|
bool haveInclusionPathHandle(InclusionPathHandle H) {
|
|
|
|
for (std::vector<InclusionPathHandle>::iterator
|
|
|
|
I = InclusionPathHandles.begin(),
|
|
|
|
E = InclusionPathHandles.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (*I == H)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return InclusionPathHandleInvalid;
|
|
|
|
}
|
|
|
|
// Add a new header inclusion path entry, if not already present.
|
|
|
|
void addInclusionPathHandle(InclusionPathHandle H) {
|
|
|
|
if (!haveInclusionPathHandle(H))
|
|
|
|
InclusionPathHandles.push_back(H);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A flag representing the evaluated condition value.
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionValue;
|
2013-07-27 07:56:42 +08:00
|
|
|
// The header inclusion path handles for all the instances.
|
|
|
|
std::vector<InclusionPathHandle> InclusionPathHandles;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Conditional directive instance tracker.
|
|
|
|
//
|
|
|
|
// This class represents one conditional directive, keyed by a PPItemKey.
|
|
|
|
// It stores a string representing the macro reference in the source,
|
2013-07-30 03:07:00 +08:00
|
|
|
// and a list of ConditionExpansionInstance objects representing
|
|
|
|
// the unique value the condition expression expands to in instances of
|
|
|
|
// the header.
|
2013-07-27 07:56:42 +08:00
|
|
|
class ConditionalTracker {
|
|
|
|
public:
|
|
|
|
ConditionalTracker(clang::tok::PPKeywordKind DirectiveKind,
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionValue,
|
|
|
|
StringHandle ConditionUnexpanded,
|
2013-07-27 07:56:42 +08:00
|
|
|
InclusionPathHandle InclusionPathHandle)
|
|
|
|
: DirectiveKind(DirectiveKind), ConditionUnexpanded(ConditionUnexpanded) {
|
|
|
|
addConditionalExpansionInstance(ConditionValue, InclusionPathHandle);
|
|
|
|
}
|
|
|
|
ConditionalTracker() {}
|
|
|
|
|
|
|
|
// Find a matching condition expansion instance.
|
|
|
|
ConditionalExpansionInstance *
|
2013-12-07 16:41:15 +08:00
|
|
|
findConditionalExpansionInstance(clang::PPCallbacks::ConditionValueKind ConditionValue) {
|
2013-07-27 07:56:42 +08:00
|
|
|
for (std::vector<ConditionalExpansionInstance>::iterator
|
|
|
|
I = ConditionalExpansionInstances.begin(),
|
|
|
|
E = ConditionalExpansionInstances.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->ConditionValue == ConditionValue) {
|
|
|
|
return &*I; // Found.
|
|
|
|
}
|
|
|
|
}
|
2014-06-09 10:03:06 +08:00
|
|
|
return nullptr; // Not found.
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a conditional expansion instance.
|
|
|
|
void
|
2013-12-07 16:41:15 +08:00
|
|
|
addConditionalExpansionInstance(clang::PPCallbacks::ConditionValueKind ConditionValue,
|
2013-07-27 07:56:42 +08:00
|
|
|
InclusionPathHandle InclusionPathHandle) {
|
|
|
|
ConditionalExpansionInstances.push_back(
|
|
|
|
ConditionalExpansionInstance(ConditionValue, InclusionPathHandle));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if there is a mismatch.
|
|
|
|
bool hasMismatch() { return ConditionalExpansionInstances.size() > 1; }
|
|
|
|
|
|
|
|
// The kind of directive.
|
|
|
|
clang::tok::PPKeywordKind DirectiveKind;
|
|
|
|
// A string representing the macro instance without expansion.
|
|
|
|
StringHandle ConditionUnexpanded;
|
|
|
|
// The condition expansion instances.
|
|
|
|
// If all instances of the conditional expression expand to the same value,
|
|
|
|
// This vector will only have one instance.
|
|
|
|
std::vector<ConditionalExpansionInstance> ConditionalExpansionInstances;
|
|
|
|
};
|
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
class PreprocessorTrackerImpl;
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Preprocessor callbacks for modularize.
|
|
|
|
//
|
|
|
|
// This class derives from the Clang PPCallbacks class to track preprocessor
|
|
|
|
// actions, such as changing files and handling preprocessor directives and
|
|
|
|
// macro expansions. It has to figure out when a new header file is entered
|
|
|
|
// and left, as the provided handler is not particularly clear about it.
|
|
|
|
class PreprocessorCallbacks : public clang::PPCallbacks {
|
|
|
|
public:
|
|
|
|
PreprocessorCallbacks(PreprocessorTrackerImpl &ppTracker,
|
|
|
|
clang::Preprocessor &PP, llvm::StringRef rootHeaderFile)
|
|
|
|
: PPTracker(ppTracker), PP(PP), RootHeaderFile(rootHeaderFile) {}
|
2015-04-11 15:59:33 +08:00
|
|
|
~PreprocessorCallbacks() override {}
|
2013-07-27 07:56:42 +08:00
|
|
|
|
|
|
|
// Overridden handlers.
|
2013-09-19 02:19:43 +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,
|
2015-04-11 15:59:33 +08:00
|
|
|
const clang::Module *Imported) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void FileChanged(clang::SourceLocation Loc,
|
|
|
|
clang::PPCallbacks::FileChangeReason Reason,
|
|
|
|
clang::SrcMgr::CharacteristicKind FileType,
|
2015-04-11 15:59:33 +08:00
|
|
|
clang::FileID PrevFID = clang::FileID()) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void MacroExpands(const clang::Token &MacroNameTok,
|
|
|
|
const clang::MacroDirective *MD, clang::SourceRange Range,
|
2015-04-11 15:59:33 +08:00
|
|
|
const clang::MacroArgs *Args) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void Defined(const clang::Token &MacroNameTok,
|
2015-04-11 15:59:33 +08:00
|
|
|
const clang::MacroDirective *MD,
|
|
|
|
clang::SourceRange Range) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
|
2015-04-11 15:59:33 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionResult) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange,
|
2015-04-11 15:59:33 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionResult,
|
|
|
|
clang::SourceLocation IfLoc) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
|
2015-04-11 15:59:33 +08:00
|
|
|
const clang::MacroDirective *MD) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok,
|
2015-04-11 15:59:33 +08:00
|
|
|
const clang::MacroDirective *MD) override;
|
2013-07-27 07:56:42 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
PreprocessorTrackerImpl &PPTracker;
|
|
|
|
clang::Preprocessor &PP;
|
|
|
|
std::string RootHeaderFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Preprocessor macro expansion item map types.
|
|
|
|
typedef std::map<PPItemKey, MacroExpansionTracker> MacroExpansionMap;
|
|
|
|
typedef std::map<PPItemKey, MacroExpansionTracker>::iterator
|
|
|
|
MacroExpansionMapIter;
|
|
|
|
|
|
|
|
// Preprocessor conditional expansion item map types.
|
|
|
|
typedef std::map<PPItemKey, ConditionalTracker> ConditionalExpansionMap;
|
|
|
|
typedef std::map<PPItemKey, ConditionalTracker>::iterator
|
|
|
|
ConditionalExpansionMapIter;
|
|
|
|
|
|
|
|
// Preprocessor tracker for modularize.
|
|
|
|
//
|
|
|
|
// This class stores information about all the headers processed in the
|
|
|
|
// course of running modularize.
|
|
|
|
class PreprocessorTrackerImpl : public PreprocessorTracker {
|
|
|
|
public:
|
2015-02-12 00:58:36 +08:00
|
|
|
PreprocessorTrackerImpl(llvm::SmallVector<std::string, 32> &Headers,
|
|
|
|
bool DoBlockCheckHeaderListOnly)
|
|
|
|
: BlockCheckHeaderListOnly(DoBlockCheckHeaderListOnly),
|
|
|
|
CurrentInclusionPathHandle(InclusionPathHandleInvalid),
|
|
|
|
InNestedHeader(false) {
|
|
|
|
// Use canonical header path representation.
|
|
|
|
for (llvm::ArrayRef<std::string>::iterator I = Headers.begin(),
|
|
|
|
E = Headers.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
HeaderList.push_back(getCanonicalPath(*I));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 15:59:33 +08:00
|
|
|
~PreprocessorTrackerImpl() override {}
|
2013-07-27 07:56:42 +08:00
|
|
|
|
|
|
|
// Handle entering a preprocessing session.
|
|
|
|
void handlePreprocessorEntry(clang::Preprocessor &PP,
|
2015-04-11 15:59:33 +08:00
|
|
|
llvm::StringRef rootHeaderFile) override {
|
2013-08-08 02:49:47 +08:00
|
|
|
HeadersInThisCompile.clear();
|
2013-07-27 07:56:42 +08:00
|
|
|
assert((HeaderStack.size() == 0) && "Header stack should be empty.");
|
|
|
|
pushHeaderHandle(addHeader(rootHeaderFile));
|
2014-09-10 13:07:57 +08:00
|
|
|
PP.addPPCallbacks(llvm::make_unique<PreprocessorCallbacks>(*this, PP,
|
|
|
|
rootHeaderFile));
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
// Handle exiting a preprocessing session.
|
2015-04-11 15:59:33 +08:00
|
|
|
void handlePreprocessorExit() override { HeaderStack.clear(); }
|
2013-07-27 07:56:42 +08:00
|
|
|
|
2013-09-19 02:19:43 +08:00
|
|
|
// Handle include directive.
|
|
|
|
// This function is called every time an include directive is seen by the
|
|
|
|
// preprocessor, for the purpose of later checking for 'extern "" {}' or
|
|
|
|
// "namespace {}" blocks containing #include directives.
|
|
|
|
void handleIncludeDirective(llvm::StringRef DirectivePath, int DirectiveLine,
|
2015-04-11 15:59:33 +08:00
|
|
|
int DirectiveColumn,
|
|
|
|
llvm::StringRef TargetPath) override {
|
2015-02-12 00:58:36 +08:00
|
|
|
// If it's not a header in the header list, ignore it with respect to
|
|
|
|
// the check.
|
2015-02-14 07:32:08 +08:00
|
|
|
if (BlockCheckHeaderListOnly && !isHeaderListHeader(TargetPath))
|
2015-02-12 00:58:36 +08:00
|
|
|
return;
|
2013-09-19 02:19:43 +08:00
|
|
|
HeaderHandle CurrentHeaderHandle = findHeaderHandle(DirectivePath);
|
|
|
|
StringHandle IncludeHeaderHandle = addString(TargetPath);
|
|
|
|
for (std::vector<PPItemKey>::const_iterator I = IncludeDirectives.begin(),
|
|
|
|
E = IncludeDirectives.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// If we already have an entry for this directive, return now.
|
|
|
|
if ((I->File == CurrentHeaderHandle) && (I->Line == DirectiveLine))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PPItemKey IncludeDirectiveItem(IncludeHeaderHandle, CurrentHeaderHandle,
|
|
|
|
DirectiveLine, DirectiveColumn);
|
|
|
|
IncludeDirectives.push_back(IncludeDirectiveItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for include directives within the given source line range.
|
|
|
|
// Report errors if any found. Returns true if no include directives
|
|
|
|
// found in block.
|
|
|
|
bool checkForIncludesInBlock(clang::Preprocessor &PP,
|
|
|
|
clang::SourceRange BlockSourceRange,
|
|
|
|
const char *BlockIdentifierMessage,
|
2015-04-11 15:59:33 +08:00
|
|
|
llvm::raw_ostream &OS) override {
|
2013-09-19 02:19:43 +08:00
|
|
|
clang::SourceLocation BlockStartLoc = BlockSourceRange.getBegin();
|
|
|
|
clang::SourceLocation BlockEndLoc = BlockSourceRange.getEnd();
|
|
|
|
// Use block location to get FileID of both the include directive
|
|
|
|
// and block statement.
|
|
|
|
clang::FileID FileID = PP.getSourceManager().getFileID(BlockStartLoc);
|
|
|
|
std::string SourcePath = getSourceLocationFile(PP, BlockStartLoc);
|
2015-02-27 03:31:10 +08:00
|
|
|
SourcePath = ModularizeUtilities::getCanonicalPath(SourcePath);
|
2013-09-19 02:19:43 +08:00
|
|
|
HeaderHandle SourceHandle = findHeaderHandle(SourcePath);
|
2015-02-19 00:12:26 +08:00
|
|
|
if (SourceHandle == -1)
|
|
|
|
return true;
|
2013-09-19 02:19:43 +08:00
|
|
|
int BlockStartLine, BlockStartColumn, BlockEndLine, BlockEndColumn;
|
|
|
|
bool returnValue = true;
|
|
|
|
getSourceLocationLineAndColumn(PP, BlockStartLoc, BlockStartLine,
|
|
|
|
BlockStartColumn);
|
|
|
|
getSourceLocationLineAndColumn(PP, BlockEndLoc, BlockEndLine,
|
|
|
|
BlockEndColumn);
|
|
|
|
for (std::vector<PPItemKey>::const_iterator I = IncludeDirectives.begin(),
|
|
|
|
E = IncludeDirectives.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// If we find an entry within the block, report an error.
|
|
|
|
if ((I->File == SourceHandle) && (I->Line >= BlockStartLine) &&
|
|
|
|
(I->Line < BlockEndLine)) {
|
|
|
|
returnValue = false;
|
2013-09-20 22:44:20 +08:00
|
|
|
OS << SourcePath << ":" << I->Line << ":" << I->Column << ":\n";
|
2013-09-19 02:19:43 +08:00
|
|
|
OS << getSourceLine(PP, FileID, I->Line) << "\n";
|
|
|
|
if (I->Column > 0)
|
|
|
|
OS << std::string(I->Column - 1, ' ') << "^\n";
|
|
|
|
OS << "error: Include directive within " << BlockIdentifierMessage
|
|
|
|
<< ".\n";
|
|
|
|
OS << SourcePath << ":" << BlockStartLine << ":" << BlockStartColumn
|
2013-09-20 22:44:20 +08:00
|
|
|
<< ":\n";
|
2013-09-19 02:19:43 +08:00
|
|
|
OS << getSourceLine(PP, BlockStartLoc) << "\n";
|
|
|
|
if (BlockStartColumn > 0)
|
|
|
|
OS << std::string(BlockStartColumn - 1, ' ') << "^\n";
|
|
|
|
OS << "The \"" << BlockIdentifierMessage << "\" block is here.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Handle entering a header source file.
|
|
|
|
void handleHeaderEntry(clang::Preprocessor &PP, llvm::StringRef HeaderPath) {
|
|
|
|
// Ignore <built-in> and <command-line> to reduce message clutter.
|
|
|
|
if (HeaderPath.startswith("<"))
|
|
|
|
return;
|
|
|
|
HeaderHandle H = addHeader(HeaderPath);
|
2013-08-08 02:49:47 +08:00
|
|
|
if (H != getCurrentHeaderHandle())
|
2013-07-27 07:56:42 +08:00
|
|
|
pushHeaderHandle(H);
|
2013-08-08 02:49:47 +08:00
|
|
|
// Check for nested header.
|
|
|
|
if (!InNestedHeader)
|
2014-11-19 15:49:54 +08:00
|
|
|
InNestedHeader = !HeadersInThisCompile.insert(H).second;
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
2015-02-12 00:58:36 +08:00
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Handle exiting a header source file.
|
|
|
|
void handleHeaderExit(llvm::StringRef HeaderPath) {
|
|
|
|
// Ignore <built-in> and <command-line> to reduce message clutter.
|
|
|
|
if (HeaderPath.startswith("<"))
|
|
|
|
return;
|
|
|
|
HeaderHandle H = findHeaderHandle(HeaderPath);
|
2015-02-12 23:26:17 +08:00
|
|
|
HeaderHandle TH;
|
2013-07-27 07:56:42 +08:00
|
|
|
if (isHeaderHandleInStack(H)) {
|
2015-02-12 23:26:17 +08:00
|
|
|
do {
|
|
|
|
TH = getCurrentHeaderHandle();
|
2013-07-27 07:56:42 +08:00
|
|
|
popHeaderHandle();
|
2015-02-12 23:26:17 +08:00
|
|
|
} while ((TH != H) && (HeaderStack.size() != 0));
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
2013-08-06 07:55:14 +08:00
|
|
|
InNestedHeader = false;
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup/add string.
|
|
|
|
StringHandle addString(llvm::StringRef Str) { return Strings.intern(Str); }
|
|
|
|
|
2015-02-12 00:45:50 +08:00
|
|
|
// Convert to a canonical path.
|
|
|
|
std::string getCanonicalPath(llvm::StringRef path) const {
|
|
|
|
std::string CanonicalPath(path);
|
|
|
|
std::replace(CanonicalPath.begin(), CanonicalPath.end(), '\\', '/');
|
|
|
|
return CanonicalPath;
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:58:36 +08:00
|
|
|
// Return true if the given header is in the header list.
|
|
|
|
bool isHeaderListHeader(llvm::StringRef HeaderPath) const {
|
|
|
|
std::string CanonicalPath = getCanonicalPath(HeaderPath);
|
|
|
|
for (llvm::ArrayRef<std::string>::iterator I = HeaderList.begin(),
|
|
|
|
E = HeaderList.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (*I == CanonicalPath)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Get the handle of a header file entry.
|
|
|
|
// Return HeaderHandleInvalid if not found.
|
|
|
|
HeaderHandle findHeaderHandle(llvm::StringRef HeaderPath) const {
|
2015-02-12 00:45:50 +08:00
|
|
|
std::string CanonicalPath = getCanonicalPath(HeaderPath);
|
2013-07-27 07:56:42 +08:00
|
|
|
HeaderHandle H = 0;
|
|
|
|
for (std::vector<StringHandle>::const_iterator I = HeaderPaths.begin(),
|
|
|
|
E = HeaderPaths.end();
|
|
|
|
I != E; ++I, ++H) {
|
2013-08-06 07:55:14 +08:00
|
|
|
if (**I == CanonicalPath)
|
2013-07-27 07:56:42 +08:00
|
|
|
return H;
|
|
|
|
}
|
|
|
|
return HeaderHandleInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new header file entry, or return existing handle.
|
|
|
|
// Return the header handle.
|
|
|
|
HeaderHandle addHeader(llvm::StringRef HeaderPath) {
|
2015-02-12 00:45:50 +08:00
|
|
|
std::string CanonicalPath = getCanonicalPath(HeaderPath);
|
2013-08-06 07:55:14 +08:00
|
|
|
HeaderHandle H = findHeaderHandle(CanonicalPath);
|
2013-07-27 07:56:42 +08:00
|
|
|
if (H == HeaderHandleInvalid) {
|
|
|
|
H = HeaderPaths.size();
|
2013-08-06 07:55:14 +08:00
|
|
|
HeaderPaths.push_back(addString(CanonicalPath));
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
return H;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a header file path string given its handle.
|
|
|
|
StringHandle getHeaderFilePath(HeaderHandle H) const {
|
|
|
|
if ((H >= 0) && (H < (HeaderHandle)HeaderPaths.size()))
|
|
|
|
return HeaderPaths[H];
|
|
|
|
return StringHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a handle to the inclusion path.
|
|
|
|
InclusionPathHandle pushHeaderHandle(HeaderHandle H) {
|
|
|
|
HeaderStack.push_back(H);
|
|
|
|
return CurrentInclusionPathHandle = addInclusionPathHandle(HeaderStack);
|
|
|
|
}
|
|
|
|
// Pops the last header handle from the stack;
|
|
|
|
void popHeaderHandle() {
|
|
|
|
// assert((HeaderStack.size() != 0) && "Header stack already empty.");
|
|
|
|
if (HeaderStack.size() != 0) {
|
|
|
|
HeaderStack.pop_back();
|
|
|
|
CurrentInclusionPathHandle = addInclusionPathHandle(HeaderStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get the top handle on the header stack.
|
|
|
|
HeaderHandle getCurrentHeaderHandle() const {
|
|
|
|
if (HeaderStack.size() != 0)
|
|
|
|
return HeaderStack.back();
|
|
|
|
return HeaderHandleInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for presence of header handle in the header stack.
|
|
|
|
bool isHeaderHandleInStack(HeaderHandle H) const {
|
|
|
|
for (std::vector<HeaderHandle>::const_iterator I = HeaderStack.begin(),
|
|
|
|
E = HeaderStack.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (*I == H)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handle of a header inclusion path entry.
|
|
|
|
// Return InclusionPathHandleInvalid if not found.
|
|
|
|
InclusionPathHandle
|
|
|
|
findInclusionPathHandle(const std::vector<HeaderHandle> &Path) const {
|
|
|
|
InclusionPathHandle H = 0;
|
|
|
|
for (std::vector<HeaderInclusionPath>::const_iterator
|
|
|
|
I = InclusionPaths.begin(),
|
|
|
|
E = InclusionPaths.end();
|
|
|
|
I != E; ++I, ++H) {
|
|
|
|
if (I->Path == Path)
|
|
|
|
return H;
|
|
|
|
}
|
|
|
|
return HeaderHandleInvalid;
|
|
|
|
}
|
|
|
|
// Add a new header inclusion path entry, or return existing handle.
|
|
|
|
// Return the header inclusion path entry handle.
|
|
|
|
InclusionPathHandle
|
|
|
|
addInclusionPathHandle(const std::vector<HeaderHandle> &Path) {
|
|
|
|
InclusionPathHandle H = findInclusionPathHandle(Path);
|
|
|
|
if (H == HeaderHandleInvalid) {
|
|
|
|
H = InclusionPaths.size();
|
|
|
|
InclusionPaths.push_back(HeaderInclusionPath(Path));
|
|
|
|
}
|
|
|
|
return H;
|
|
|
|
}
|
|
|
|
// Return the current inclusion path handle.
|
|
|
|
InclusionPathHandle getCurrentInclusionPathHandle() const {
|
|
|
|
return CurrentInclusionPathHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an inclusion path given its handle.
|
|
|
|
const std::vector<HeaderHandle> &
|
|
|
|
getInclusionPath(InclusionPathHandle H) const {
|
|
|
|
if ((H >= 0) && (H <= (InclusionPathHandle)InclusionPaths.size()))
|
|
|
|
return InclusionPaths[H].Path;
|
|
|
|
static std::vector<HeaderHandle> Empty;
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a macro expansion instance.
|
|
|
|
void addMacroExpansionInstance(clang::Preprocessor &PP, HeaderHandle H,
|
|
|
|
clang::SourceLocation InstanceLoc,
|
|
|
|
clang::SourceLocation DefinitionLoc,
|
|
|
|
clang::IdentifierInfo *II,
|
|
|
|
llvm::StringRef MacroUnexpanded,
|
|
|
|
llvm::StringRef MacroExpanded,
|
|
|
|
InclusionPathHandle InclusionPathHandle) {
|
2013-08-14 02:11:36 +08:00
|
|
|
if (InNestedHeader)
|
|
|
|
return;
|
2013-07-27 07:56:42 +08:00
|
|
|
StringHandle MacroName = addString(II->getName());
|
|
|
|
PPItemKey InstanceKey(PP, MacroName, H, InstanceLoc);
|
|
|
|
PPItemKey DefinitionKey(PP, MacroName, H, DefinitionLoc);
|
|
|
|
MacroExpansionMapIter I = MacroExpansions.find(InstanceKey);
|
2013-07-30 03:07:00 +08:00
|
|
|
// If existing instance of expansion not found, add one.
|
2013-07-27 07:56:42 +08:00
|
|
|
if (I == MacroExpansions.end()) {
|
|
|
|
std::string InstanceSourceLine =
|
|
|
|
getSourceLocationString(PP, InstanceLoc) + ":\n" +
|
|
|
|
getSourceLine(PP, InstanceLoc) + "\n";
|
|
|
|
std::string DefinitionSourceLine =
|
|
|
|
getSourceLocationString(PP, DefinitionLoc) + ":\n" +
|
|
|
|
getSourceLine(PP, DefinitionLoc) + "\n";
|
|
|
|
MacroExpansions[InstanceKey] = MacroExpansionTracker(
|
|
|
|
addString(MacroUnexpanded), addString(MacroExpanded),
|
|
|
|
addString(InstanceSourceLine), DefinitionKey,
|
|
|
|
addString(DefinitionSourceLine), InclusionPathHandle);
|
|
|
|
} else {
|
2013-07-30 03:07:00 +08:00
|
|
|
// We've seen the macro before. Get its tracker.
|
2013-07-27 07:56:42 +08:00
|
|
|
MacroExpansionTracker &CondTracker = I->second;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Look up an existing instance value for the macro.
|
2013-07-27 07:56:42 +08:00
|
|
|
MacroExpansionInstance *MacroInfo =
|
|
|
|
CondTracker.findMacroExpansionInstance(addString(MacroExpanded),
|
|
|
|
DefinitionKey);
|
2013-07-30 03:07:00 +08:00
|
|
|
// If found, just add the inclusion path to the instance.
|
2014-06-09 10:03:06 +08:00
|
|
|
if (MacroInfo)
|
2013-07-27 07:56:42 +08:00
|
|
|
MacroInfo->addInclusionPathHandle(InclusionPathHandle);
|
|
|
|
else {
|
2013-07-30 03:07:00 +08:00
|
|
|
// Otherwise add a new instance with the unique value.
|
2013-07-27 07:56:42 +08:00
|
|
|
std::string DefinitionSourceLine =
|
|
|
|
getSourceLocationString(PP, DefinitionLoc) + ":\n" +
|
|
|
|
getSourceLine(PP, DefinitionLoc) + "\n";
|
|
|
|
CondTracker.addMacroExpansionInstance(
|
|
|
|
addString(MacroExpanded), DefinitionKey,
|
|
|
|
addString(DefinitionSourceLine), InclusionPathHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a conditional expansion instance.
|
|
|
|
void
|
|
|
|
addConditionalExpansionInstance(clang::Preprocessor &PP, HeaderHandle H,
|
|
|
|
clang::SourceLocation InstanceLoc,
|
|
|
|
clang::tok::PPKeywordKind DirectiveKind,
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionValue,
|
2013-07-27 07:56:42 +08:00
|
|
|
llvm::StringRef ConditionUnexpanded,
|
|
|
|
InclusionPathHandle InclusionPathHandle) {
|
2013-08-06 07:55:14 +08:00
|
|
|
// Ignore header guards, assuming the header guard is the only conditional.
|
|
|
|
if (InNestedHeader)
|
|
|
|
return;
|
2013-07-27 07:56:42 +08:00
|
|
|
StringHandle ConditionUnexpandedHandle(addString(ConditionUnexpanded));
|
|
|
|
PPItemKey InstanceKey(PP, ConditionUnexpandedHandle, H, InstanceLoc);
|
|
|
|
ConditionalExpansionMapIter I = ConditionalExpansions.find(InstanceKey);
|
2013-07-30 03:07:00 +08:00
|
|
|
// If existing instance of condition not found, add one.
|
2013-07-27 07:56:42 +08:00
|
|
|
if (I == ConditionalExpansions.end()) {
|
|
|
|
std::string InstanceSourceLine =
|
|
|
|
getSourceLocationString(PP, InstanceLoc) + ":\n" +
|
|
|
|
getSourceLine(PP, InstanceLoc) + "\n";
|
|
|
|
ConditionalExpansions[InstanceKey] =
|
2013-08-06 07:55:14 +08:00
|
|
|
ConditionalTracker(DirectiveKind, ConditionValue,
|
2013-09-04 02:44:11 +08:00
|
|
|
ConditionUnexpandedHandle, InclusionPathHandle);
|
2013-07-27 07:56:42 +08:00
|
|
|
} else {
|
2013-07-30 03:07:00 +08:00
|
|
|
// We've seen the conditional before. Get its tracker.
|
2013-07-27 07:56:42 +08:00
|
|
|
ConditionalTracker &CondTracker = I->second;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Look up an existing instance value for the condition.
|
2013-07-27 07:56:42 +08:00
|
|
|
ConditionalExpansionInstance *MacroInfo =
|
|
|
|
CondTracker.findConditionalExpansionInstance(ConditionValue);
|
2013-07-30 03:07:00 +08:00
|
|
|
// If found, just add the inclusion path to the instance.
|
2014-06-09 10:03:06 +08:00
|
|
|
if (MacroInfo)
|
2013-07-27 07:56:42 +08:00
|
|
|
MacroInfo->addInclusionPathHandle(InclusionPathHandle);
|
|
|
|
else {
|
2013-07-30 03:07:00 +08:00
|
|
|
// Otherwise add a new instance with the unique value.
|
2013-07-27 07:56:42 +08:00
|
|
|
CondTracker.addConditionalExpansionInstance(ConditionValue,
|
|
|
|
InclusionPathHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report on inconsistent macro instances.
|
|
|
|
// Returns true if any mismatches.
|
2015-04-11 15:59:33 +08:00
|
|
|
bool reportInconsistentMacros(llvm::raw_ostream &OS) override {
|
2013-07-27 07:56:42 +08:00
|
|
|
bool ReturnValue = false;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Walk all the macro expansion trackers in the map.
|
2013-07-27 07:56:42 +08:00
|
|
|
for (MacroExpansionMapIter I = MacroExpansions.begin(),
|
|
|
|
E = MacroExpansions.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const PPItemKey &ItemKey = I->first;
|
|
|
|
MacroExpansionTracker &MacroExpTracker = I->second;
|
2013-07-30 03:07:00 +08:00
|
|
|
// If no mismatch (only one instance value) continue.
|
2013-07-27 07:56:42 +08:00
|
|
|
if (!MacroExpTracker.hasMismatch())
|
|
|
|
continue;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Tell caller we found one or more errors.
|
2013-07-27 07:56:42 +08:00
|
|
|
ReturnValue = true;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Start the error message.
|
2013-07-27 07:56:42 +08:00
|
|
|
OS << *MacroExpTracker.InstanceSourceLine;
|
|
|
|
if (ItemKey.Column > 0)
|
|
|
|
OS << std::string(ItemKey.Column - 1, ' ') << "^\n";
|
|
|
|
OS << "error: Macro instance '" << *MacroExpTracker.MacroUnexpanded
|
|
|
|
<< "' has different values in this header, depending on how it was "
|
|
|
|
"included.\n";
|
2013-07-30 03:07:00 +08:00
|
|
|
// Walk all the instances.
|
2013-07-27 07:56:42 +08:00
|
|
|
for (std::vector<MacroExpansionInstance>::iterator
|
|
|
|
IMT = MacroExpTracker.MacroExpansionInstances.begin(),
|
|
|
|
EMT = MacroExpTracker.MacroExpansionInstances.end();
|
|
|
|
IMT != EMT; ++IMT) {
|
|
|
|
MacroExpansionInstance &MacroInfo = *IMT;
|
|
|
|
OS << " '" << *MacroExpTracker.MacroUnexpanded << "' expanded to: '"
|
|
|
|
<< *MacroInfo.MacroExpanded
|
|
|
|
<< "' with respect to these inclusion paths:\n";
|
2013-07-30 03:07:00 +08:00
|
|
|
// Walk all the inclusion path hierarchies.
|
2013-07-27 07:56:42 +08:00
|
|
|
for (std::vector<InclusionPathHandle>::iterator
|
|
|
|
IIP = MacroInfo.InclusionPathHandles.begin(),
|
|
|
|
EIP = MacroInfo.InclusionPathHandles.end();
|
|
|
|
IIP != EIP; ++IIP) {
|
|
|
|
const std::vector<HeaderHandle> &ip = getInclusionPath(*IIP);
|
|
|
|
int Count = (int)ip.size();
|
|
|
|
for (int Index = 0; Index < Count; ++Index) {
|
|
|
|
HeaderHandle H = ip[Index];
|
|
|
|
OS << std::string((Index * 2) + 4, ' ') << *getHeaderFilePath(H)
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For a macro that wasn't defined, we flag it by using the
|
|
|
|
// instance location.
|
|
|
|
// If there is a definition...
|
|
|
|
if (MacroInfo.DefinitionLocation.Line != ItemKey.Line) {
|
|
|
|
OS << *MacroInfo.DefinitionSourceLine;
|
|
|
|
if (MacroInfo.DefinitionLocation.Column > 0)
|
|
|
|
OS << std::string(MacroInfo.DefinitionLocation.Column - 1, ' ')
|
|
|
|
<< "^\n";
|
|
|
|
OS << "Macro defined here.\n";
|
|
|
|
} else
|
|
|
|
OS << "(no macro definition)"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ReturnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report on inconsistent conditional instances.
|
|
|
|
// Returns true if any mismatches.
|
2015-04-11 15:59:33 +08:00
|
|
|
bool reportInconsistentConditionals(llvm::raw_ostream &OS) override {
|
2013-07-27 07:56:42 +08:00
|
|
|
bool ReturnValue = false;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Walk all the conditional trackers in the map.
|
2013-07-27 07:56:42 +08:00
|
|
|
for (ConditionalExpansionMapIter I = ConditionalExpansions.begin(),
|
|
|
|
E = ConditionalExpansions.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const PPItemKey &ItemKey = I->first;
|
|
|
|
ConditionalTracker &CondTracker = I->second;
|
|
|
|
if (!CondTracker.hasMismatch())
|
|
|
|
continue;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Tell caller we found one or more errors.
|
2013-07-27 07:56:42 +08:00
|
|
|
ReturnValue = true;
|
2013-07-30 03:07:00 +08:00
|
|
|
// Start the error message.
|
2013-07-27 07:56:42 +08:00
|
|
|
OS << *HeaderPaths[ItemKey.File] << ":" << ItemKey.Line << ":"
|
|
|
|
<< ItemKey.Column << "\n";
|
|
|
|
OS << "#" << getDirectiveSpelling(CondTracker.DirectiveKind) << " "
|
|
|
|
<< *CondTracker.ConditionUnexpanded << "\n";
|
|
|
|
OS << "^\n";
|
|
|
|
OS << "error: Conditional expression instance '"
|
|
|
|
<< *CondTracker.ConditionUnexpanded
|
|
|
|
<< "' has different values in this header, depending on how it was "
|
|
|
|
"included.\n";
|
2013-07-30 03:07:00 +08:00
|
|
|
// Walk all the instances.
|
2013-07-27 07:56:42 +08:00
|
|
|
for (std::vector<ConditionalExpansionInstance>::iterator
|
|
|
|
IMT = CondTracker.ConditionalExpansionInstances.begin(),
|
|
|
|
EMT = CondTracker.ConditionalExpansionInstances.end();
|
|
|
|
IMT != EMT; ++IMT) {
|
|
|
|
ConditionalExpansionInstance &MacroInfo = *IMT;
|
|
|
|
OS << " '" << *CondTracker.ConditionUnexpanded << "' expanded to: '"
|
2013-12-07 16:41:15 +08:00
|
|
|
<< ConditionValueKindStrings[MacroInfo.ConditionValue]
|
2013-07-27 07:56:42 +08:00
|
|
|
<< "' with respect to these inclusion paths:\n";
|
2013-07-30 03:07:00 +08:00
|
|
|
// Walk all the inclusion path hierarchies.
|
2013-07-27 07:56:42 +08:00
|
|
|
for (std::vector<InclusionPathHandle>::iterator
|
|
|
|
IIP = MacroInfo.InclusionPathHandles.begin(),
|
|
|
|
EIP = MacroInfo.InclusionPathHandles.end();
|
|
|
|
IIP != EIP; ++IIP) {
|
|
|
|
const std::vector<HeaderHandle> &ip = getInclusionPath(*IIP);
|
|
|
|
int Count = (int)ip.size();
|
|
|
|
for (int Index = 0; Index < Count; ++Index) {
|
|
|
|
HeaderHandle H = ip[Index];
|
|
|
|
OS << std::string((Index * 2) + 4, ' ') << *getHeaderFilePath(H)
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ReturnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get directive spelling.
|
|
|
|
static const char *getDirectiveSpelling(clang::tok::PPKeywordKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case clang::tok::pp_if:
|
|
|
|
return "if";
|
|
|
|
case clang::tok::pp_elif:
|
|
|
|
return "elif";
|
|
|
|
case clang::tok::pp_ifdef:
|
|
|
|
return "ifdef";
|
|
|
|
case clang::tok::pp_ifndef:
|
|
|
|
return "ifndef";
|
|
|
|
default:
|
|
|
|
return "(unknown)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-12 00:58:36 +08:00
|
|
|
llvm::SmallVector<std::string, 32> HeaderList;
|
|
|
|
// Only do extern, namespace check for headers in HeaderList.
|
|
|
|
bool BlockCheckHeaderListOnly;
|
2013-07-27 07:56:42 +08:00
|
|
|
llvm::StringPool Strings;
|
|
|
|
std::vector<StringHandle> HeaderPaths;
|
|
|
|
std::vector<HeaderHandle> HeaderStack;
|
|
|
|
std::vector<HeaderInclusionPath> InclusionPaths;
|
|
|
|
InclusionPathHandle CurrentInclusionPathHandle;
|
2013-08-08 02:49:47 +08:00
|
|
|
llvm::SmallSet<HeaderHandle, 128> HeadersInThisCompile;
|
2013-09-19 02:19:43 +08:00
|
|
|
std::vector<PPItemKey> IncludeDirectives;
|
2013-07-27 07:56:42 +08:00
|
|
|
MacroExpansionMap MacroExpansions;
|
|
|
|
ConditionalExpansionMap ConditionalExpansions;
|
2013-08-06 07:55:14 +08:00
|
|
|
bool InNestedHeader;
|
2013-07-27 07:56:42 +08:00
|
|
|
};
|
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
} // namespace
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// PreprocessorTracker functions.
|
|
|
|
|
|
|
|
// PreprocessorTracker desctructor.
|
|
|
|
PreprocessorTracker::~PreprocessorTracker() {}
|
|
|
|
|
|
|
|
// Create instance of PreprocessorTracker.
|
2015-02-12 00:58:36 +08:00
|
|
|
PreprocessorTracker *PreprocessorTracker::create(
|
|
|
|
llvm::SmallVector<std::string, 32> &Headers,
|
|
|
|
bool DoBlockCheckHeaderListOnly) {
|
|
|
|
return new PreprocessorTrackerImpl(Headers, DoBlockCheckHeaderListOnly);
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preprocessor callbacks for modularize.
|
|
|
|
|
2013-09-19 02:19:43 +08:00
|
|
|
// Handle include directive.
|
|
|
|
void PreprocessorCallbacks::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,
|
|
|
|
const clang::Module *Imported) {
|
|
|
|
int DirectiveLine, DirectiveColumn;
|
|
|
|
std::string HeaderPath = getSourceLocationFile(PP, HashLoc);
|
|
|
|
getSourceLocationLineAndColumn(PP, HashLoc, DirectiveLine, DirectiveColumn);
|
|
|
|
PPTracker.handleIncludeDirective(HeaderPath, DirectiveLine, DirectiveColumn,
|
|
|
|
FileName);
|
|
|
|
}
|
|
|
|
|
2013-07-27 07:56:42 +08:00
|
|
|
// Handle file entry/exit.
|
|
|
|
void PreprocessorCallbacks::FileChanged(
|
|
|
|
clang::SourceLocation Loc, clang::PPCallbacks::FileChangeReason Reason,
|
|
|
|
clang::SrcMgr::CharacteristicKind FileType, clang::FileID PrevFID) {
|
|
|
|
switch (Reason) {
|
|
|
|
case EnterFile:
|
|
|
|
PPTracker.handleHeaderEntry(PP, getSourceLocationFile(PP, Loc));
|
|
|
|
break;
|
2013-09-04 02:44:11 +08:00
|
|
|
case ExitFile: {
|
|
|
|
const clang::FileEntry *F =
|
2013-08-06 07:55:14 +08:00
|
|
|
PP.getSourceManager().getFileEntryForID(PrevFID);
|
2014-06-09 10:03:06 +08:00
|
|
|
if (F)
|
2013-09-04 02:44:11 +08:00
|
|
|
PPTracker.handleHeaderExit(F->getName());
|
|
|
|
} break;
|
2013-07-27 07:56:42 +08:00
|
|
|
case SystemHeaderPragma:
|
|
|
|
case RenameFile:
|
2013-07-27 23:57:46 +08:00
|
|
|
break;
|
2013-07-27 07:56:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle macro expansion.
|
|
|
|
void PreprocessorCallbacks::MacroExpands(const clang::Token &MacroNameTok,
|
|
|
|
const clang::MacroDirective *MD,
|
|
|
|
clang::SourceRange Range,
|
|
|
|
const clang::MacroArgs *Args) {
|
|
|
|
clang::SourceLocation Loc = Range.getBegin();
|
2013-08-09 08:22:20 +08:00
|
|
|
// Ignore macro argument expansions.
|
|
|
|
if (!Loc.isFileID())
|
|
|
|
return;
|
2013-07-27 07:56:42 +08:00
|
|
|
clang::IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
|
|
|
|
const clang::MacroInfo *MI = PP.getMacroInfo(II);
|
|
|
|
std::string MacroName = II->getName().str();
|
|
|
|
std::string Unexpanded(getMacroUnexpandedString(Range, PP, MacroName, MI));
|
|
|
|
std::string Expanded(getMacroExpandedString(PP, MacroName, MI, Args));
|
|
|
|
PPTracker.addMacroExpansionInstance(
|
|
|
|
PP, PPTracker.getCurrentHeaderHandle(), Loc, MI->getDefinitionLoc(), II,
|
|
|
|
Unexpanded, Expanded, PPTracker.getCurrentInclusionPathHandle());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessorCallbacks::Defined(const clang::Token &MacroNameTok,
|
|
|
|
const clang::MacroDirective *MD,
|
|
|
|
clang::SourceRange Range) {
|
|
|
|
clang::SourceLocation Loc(Range.getBegin());
|
|
|
|
clang::IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
|
|
|
|
const clang::MacroInfo *MI = PP.getMacroInfo(II);
|
|
|
|
std::string MacroName = II->getName().str();
|
|
|
|
std::string Unexpanded(getSourceString(PP, Range));
|
|
|
|
PPTracker.addMacroExpansionInstance(
|
|
|
|
PP, PPTracker.getCurrentHeaderHandle(), Loc,
|
|
|
|
(MI ? MI->getDefinitionLoc() : Loc), II, Unexpanded,
|
|
|
|
(MI ? "true" : "false"), PPTracker.getCurrentInclusionPathHandle());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessorCallbacks::If(clang::SourceLocation Loc,
|
|
|
|
clang::SourceRange ConditionRange,
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionResult) {
|
2013-07-27 07:56:42 +08:00
|
|
|
std::string Unexpanded(getSourceString(PP, ConditionRange));
|
|
|
|
PPTracker.addConditionalExpansionInstance(
|
|
|
|
PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_if,
|
|
|
|
ConditionResult, Unexpanded, PPTracker.getCurrentInclusionPathHandle());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessorCallbacks::Elif(clang::SourceLocation Loc,
|
|
|
|
clang::SourceRange ConditionRange,
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind ConditionResult,
|
2013-07-27 07:56:42 +08:00
|
|
|
clang::SourceLocation IfLoc) {
|
|
|
|
std::string Unexpanded(getSourceString(PP, ConditionRange));
|
|
|
|
PPTracker.addConditionalExpansionInstance(
|
|
|
|
PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_elif,
|
|
|
|
ConditionResult, Unexpanded, PPTracker.getCurrentInclusionPathHandle());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessorCallbacks::Ifdef(clang::SourceLocation Loc,
|
|
|
|
const clang::Token &MacroNameTok,
|
|
|
|
const clang::MacroDirective *MD) {
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind IsDefined =
|
2014-06-09 10:03:06 +08:00
|
|
|
(MD ? clang::PPCallbacks::CVK_True : clang::PPCallbacks::CVK_False );
|
2013-07-27 07:56:42 +08:00
|
|
|
PPTracker.addConditionalExpansionInstance(
|
|
|
|
PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_ifdef,
|
|
|
|
IsDefined, PP.getSpelling(MacroNameTok),
|
|
|
|
PPTracker.getCurrentInclusionPathHandle());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessorCallbacks::Ifndef(clang::SourceLocation Loc,
|
|
|
|
const clang::Token &MacroNameTok,
|
|
|
|
const clang::MacroDirective *MD) {
|
2013-12-07 16:41:15 +08:00
|
|
|
clang::PPCallbacks::ConditionValueKind IsNotDefined =
|
2014-06-09 10:03:06 +08:00
|
|
|
(!MD ? clang::PPCallbacks::CVK_True : clang::PPCallbacks::CVK_False );
|
2013-07-27 07:56:42 +08:00
|
|
|
PPTracker.addConditionalExpansionInstance(
|
|
|
|
PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_ifndef,
|
|
|
|
IsNotDefined, PP.getSpelling(MacroNameTok),
|
|
|
|
PPTracker.getCurrentInclusionPathHandle());
|
|
|
|
}
|
|
|
|
} // end namespace Modularize
|