John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1 | //===--- PreprocessorTracker.cpp - Preprocessor tracking -*- C++ -*------===// |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 8 | //===--------------------------------------------------------------------===// |
| 9 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 10 | // The Basic Idea (Macro and Conditional Checking) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 11 | // |
| 12 | // Basically we install a PPCallbacks-derived object to track preprocessor |
| 13 | // activity, namely when a header file is entered/exited, when a macro |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 14 | // is expanded, when "defined" is used, and when #if, #elif, #ifdef, |
| 15 | // and #ifndef are used. We save the state of macro and "defined" |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 16 | // expressions in a map, keyed on a name/file/line/column quadruple. |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 17 | // The map entries store the different states (values) that a macro expansion, |
| 18 | // "defined" expression, or condition expression has in the course of |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 19 | // processing for the one location in the one header containing it, |
| 20 | // plus a list of the nested include stacks for the states. When a macro |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 21 | // or "defined" expression evaluates to the same value, which is the |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 22 | // desired case, only one state is stored. Similarly, for conditional |
| 23 | // directives, we save the condition expression states in a separate map. |
| 24 | // |
| 25 | // This information is collected as modularize compiles all the headers |
| 26 | // given to it to process. After all the compilations are performed, |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 27 | // a check is performed for any entries in the maps that contain more |
| 28 | // than one different state, and for these an output message is generated. |
| 29 | // |
| 30 | // For example: |
| 31 | // |
| 32 | // (...)/SubHeader.h:11:5: |
| 33 | // #if SYMBOL == 1 |
| 34 | // ^ |
| 35 | // error: Macro instance 'SYMBOL' has different values in this header, |
| 36 | // depending on how it was included. |
| 37 | // 'SYMBOL' expanded to: '1' with respect to these inclusion paths: |
| 38 | // (...)/Header1.h |
| 39 | // (...)/SubHeader.h |
| 40 | // (...)/SubHeader.h:3:9: |
| 41 | // #define SYMBOL 1 |
| 42 | // ^ |
| 43 | // Macro defined here. |
| 44 | // 'SYMBOL' expanded to: '2' with respect to these inclusion paths: |
| 45 | // (...)/Header2.h |
| 46 | // (...)/SubHeader.h |
| 47 | // (...)/SubHeader.h:7:9: |
| 48 | // #define SYMBOL 2 |
| 49 | // ^ |
| 50 | // Macro defined here. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 51 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 52 | // The Basic Idea ('Extern "C/C++" {}' Or 'namespace {}') With Nested |
| 53 | // '#include' Checking) |
| 54 | // |
| 55 | // To check for '#include' directives nested inside 'Extern "C/C++" {}' |
| 56 | // or 'namespace {}' blocks, we keep track of the '#include' directives |
| 57 | // while running the preprocessor, and later during a walk of the AST |
| 58 | // we call a function to check for any '#include' directies inside |
| 59 | // an 'Extern "C/C++" {}' or 'namespace {}' block, given its source |
| 60 | // range. |
| 61 | // |
| 62 | // Design and Implementation Details (Macro and Conditional Checking) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 63 | // |
| 64 | // A PreprocessorTrackerImpl class implements the PreprocessorTracker |
| 65 | // interface. It uses a PreprocessorCallbacks class derived from PPCallbacks |
| 66 | // to track preprocessor activity, namely entering/exiting a header, macro |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 67 | // expansions, use of "defined" expressions, and #if, #elif, #ifdef, and |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 68 | // #ifndef conditional directives. PreprocessorTrackerImpl stores a map |
| 69 | // of MacroExpansionTracker objects keyed on a name/file/line/column |
| 70 | // value represented by a light-weight PPItemKey value object. This |
| 71 | // is the key top-level data structure tracking the values of macro |
| 72 | // expansion instances. Similarly, it stores a map of ConditionalTracker |
| 73 | // objects with the same kind of key, for tracking preprocessor conditional |
| 74 | // directives. |
| 75 | // |
| 76 | // The MacroExpansionTracker object represents one macro reference or use |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 77 | // of a "defined" expression in a header file. It stores a handle to a |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 78 | // string representing the unexpanded macro instance, a handle to a string |
| 79 | // representing the unpreprocessed source line containing the unexpanded |
| 80 | // macro instance, and a vector of one or more MacroExpansionInstance |
| 81 | // objects. |
| 82 | // |
| 83 | // The MacroExpansionInstance object represents one or more expansions |
| 84 | // of a macro reference, for the case where the macro expands to the same |
| 85 | // value. MacroExpansionInstance stores a handle to a string representing |
| 86 | // the expanded macro value, a PPItemKey representing the file/line/column |
| 87 | // where the macro was defined, a handle to a string representing the source |
| 88 | // line containing the macro definition, and a vector of InclusionPathHandle |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 89 | // values that represents the hierarchies of include files for each case |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 90 | // where the particular header containing the macro reference was referenced |
| 91 | // or included. |
| 92 | |
| 93 | // In the normal case where a macro instance always expands to the same |
| 94 | // value, the MacroExpansionTracker object will only contain one |
| 95 | // MacroExpansionInstance representing all the macro expansion instances. |
| 96 | // If a case was encountered where a macro instance expands to a value |
| 97 | // that is different from that seen before, or the macro was defined in |
| 98 | // a different place, a new MacroExpansionInstance object representing |
| 99 | // that case will be added to the vector in MacroExpansionTracker. If a |
| 100 | // macro instance expands to a value already seen before, the |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 101 | // InclusionPathHandle representing that case's include file hierarchy |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 102 | // will be added to the existing MacroExpansionInstance object. |
| 103 | |
| 104 | // For checking conditional directives, the ConditionalTracker class |
| 105 | // functions similarly to MacroExpansionTracker, but tracks an #if, |
| 106 | // #elif, #ifdef, or #ifndef directive in a header file. It stores |
| 107 | // a vector of one or two ConditionalExpansionInstance objects, |
| 108 | // representing the cases where the conditional expression evaluates |
| 109 | // to true or false. This latter object stores the evaluated value |
| 110 | // of the condition expression (a bool) and a vector of |
| 111 | // InclusionPathHandles. |
| 112 | // |
| 113 | // To reduce the instances of string and object copying, the |
| 114 | // PreprocessorTrackerImpl class uses a StringPool to save all stored |
| 115 | // strings, and defines a StringHandle type to abstract the references |
| 116 | // to the strings. |
| 117 | // |
| 118 | // PreprocessorTrackerImpl also maintains a list representing the unique |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 119 | // headers, which is just a vector of StringHandle's for the header file |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 120 | // paths. A HeaderHandle abstracts a reference to a header, and is simply |
| 121 | // the index of the stored header file path. |
| 122 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 123 | // A HeaderInclusionPath class abstracts a unique hierarchy of header file |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 124 | // inclusions. It simply stores a vector of HeaderHandles ordered from the |
| 125 | // top-most header (the one from the header list passed to modularize) down |
| 126 | // to the header containing the macro reference. PreprocessorTrackerImpl |
| 127 | // stores a vector of these objects. An InclusionPathHandle typedef |
| 128 | // abstracts a reference to one of the HeaderInclusionPath objects, and is |
| 129 | // simply the index of the stored HeaderInclusionPath object. The |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 130 | // MacroExpansionInstance object stores a vector of these handles so that |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 131 | // the reporting function can display the include hierarchies for the macro |
| 132 | // expansion instances represented by that object, to help the user |
| 133 | // understand how the header was included. (A future enhancement might |
| 134 | // be to associate a line number for the #include directives, but I |
| 135 | // think not doing so is good enough for the present.) |
| 136 | // |
| 137 | // A key reason for using these opaque handles was to try to keep all the |
| 138 | // internal objects light-weight value objects, in order to reduce string |
| 139 | // and object copying overhead, and to abstract this implementation detail. |
| 140 | // |
| 141 | // The key data structures are built up while modularize runs the headers |
| 142 | // through the compilation. A PreprocessorTracker instance is created and |
| 143 | // passed down to the AST action and consumer objects in modularize. For |
| 144 | // each new compilation instance, the consumer calls the |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 145 | // PreprocessorTracker's handleNewPreprocessorEntry function, which sets |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 146 | // up a PreprocessorCallbacks object for the preprocessor. At the end of |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 147 | // the compilation instance, the PreprocessorTracker's |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 148 | // handleNewPreprocessorExit function handles cleaning up with respect |
| 149 | // to the preprocessing instance. |
| 150 | // |
| 151 | // The PreprocessorCallbacks object uses an overidden FileChanged callback |
| 152 | // to determine when a header is entered and exited (including exiting the |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 153 | // header during #include directives). It calls PreprocessorTracker's |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 154 | // handleHeaderEntry and handleHeaderExit functions upon entering and |
| 155 | // exiting a header. These functions manage a stack of header handles |
| 156 | // representing by a vector, pushing and popping header handles as headers |
| 157 | // are entered and exited. When a HeaderInclusionPath object is created, |
| 158 | // it simply copies this stack. |
| 159 | // |
| 160 | // The PreprocessorCallbacks object uses an overridden MacroExpands callback |
| 161 | // to track when a macro expansion is performed. It calls a couple of helper |
| 162 | // functions to get the unexpanded and expanded macro values as strings, but |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 163 | // then calls PreprocessorTrackerImpl's addMacroExpansionInstance function to |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 164 | // do the rest of the work. The getMacroExpandedString function uses the |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 165 | // preprocessor's getSpelling to convert tokens to strings using the |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 166 | // information passed to the MacroExpands callback, and simply concatenates |
| 167 | // them. It makes recursive calls to itself to handle nested macro |
| 168 | // definitions, and also handles function-style macros. |
| 169 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 170 | // PreprocessorTrackerImpl's addMacroExpansionInstance function looks for |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 171 | // an existing MacroExpansionTracker entry in its map of MacroExampleTracker |
| 172 | // objects. If none exists, it adds one with one MacroExpansionInstance and |
| 173 | // returns. If a MacroExpansionTracker object already exists, it looks for |
| 174 | // an existing MacroExpansionInstance object stored in the |
| 175 | // MacroExpansionTracker object, one that matches the macro expanded value |
| 176 | // and the macro definition location. If a matching MacroExpansionInstance |
| 177 | // object is found, it just adds the current HeaderInclusionPath object to |
| 178 | // it. If not found, it creates and stores a new MacroExpantionInstance |
| 179 | // object. The addMacroExpansionInstance function calls a couple of helper |
| 180 | // functions to get the pre-formatted location and source line strings for |
| 181 | // the macro reference and the macro definition stored as string handles. |
| 182 | // These helper functions use the current source manager from the |
| 183 | // preprocessor. This is done in advance at this point in time because the |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 184 | // source manager doesn't exist at the time of the reporting. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 185 | // |
| 186 | // For conditional check, the PreprocessorCallbacks class overrides the |
| 187 | // PPCallbacks handlers for #if, #elif, #ifdef, and #ifndef. These handlers |
| 188 | // call the addConditionalExpansionInstance method of |
| 189 | // PreprocessorTrackerImpl. The process is similar to that of macros, but |
| 190 | // with some different data and error messages. A lookup is performed for |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 191 | // the conditional, and if a ConditionalTracker object doesn't yet exist for |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 192 | // the conditional, a new one is added, including adding a |
| 193 | // ConditionalExpansionInstance object to it to represent the condition |
| 194 | // expression state. If a ConditionalTracker for the conditional does |
| 195 | // exist, a lookup is made for a ConditionalExpansionInstance object |
| 196 | // matching the condition expression state. If one exists, a |
| 197 | // HeaderInclusionPath is added to it. Otherwise a new |
| 198 | // ConditionalExpansionInstance entry is made. If a ConditionalTracker |
| 199 | // has two ConditionalExpansionInstance objects, it means there was a |
| 200 | // conflict, meaning the conditional expression evaluated differently in |
| 201 | // one or more cases. |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 202 | // |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 203 | // After modularize has performed all the compilations, it enters a phase |
| 204 | // of error reporting. This new feature adds to this reporting phase calls |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 205 | // to the PreprocessorTracker's reportInconsistentMacros and |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 206 | // reportInconsistentConditionals functions. These functions walk the maps |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 207 | // of MacroExpansionTracker's and ConditionalTracker's respectively. If |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 208 | // any of these objects have more than one MacroExpansionInstance or |
| 209 | // ConditionalExpansionInstance objects, it formats and outputs an error |
| 210 | // message like the example shown previously, using the stored data. |
| 211 | // |
| 212 | // A potential issue is that there is some overlap between the #if/#elif |
| 213 | // conditional and macro reporting. I could disable the #if and #elif, |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 214 | // leaving just the #ifdef and #ifndef, since these don't overlap. Or, |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 215 | // to make clearer the separate reporting phases, I could add an output |
| 216 | // message marking the phases. |
| 217 | // |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 218 | // Design and Implementation Details ('Extern "C/C++" {}' Or |
| 219 | // 'namespace {}') With Nested '#include' Checking) |
| 220 | // |
| 221 | // We override the InclusionDirective in PPCallbacks to record information |
| 222 | // about each '#include' directive encountered during preprocessing. |
| 223 | // We co-opt the PPItemKey class to store the information about each |
| 224 | // '#include' directive, including the source file name containing the |
| 225 | // directive, the name of the file being included, and the source line |
| 226 | // and column of the directive. We store these object in a vector, |
| 227 | // after first check to see if an entry already exists. |
| 228 | // |
| 229 | // Later, while the AST is being walked for other checks, we provide |
| 230 | // visit handlers for 'extern "C/C++" {}' and 'namespace (name) {}' |
| 231 | // blocks, checking to see if any '#include' directives occurred |
| 232 | // within the blocks, reporting errors if any found. |
| 233 | // |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 234 | // Future Directions |
| 235 | // |
| 236 | // We probably should add options to disable any of the checks, in case |
| 237 | // there is some problem with them, or the messages get too verbose. |
| 238 | // |
| 239 | // With the map of all the macro and conditional expansion instances, |
| 240 | // it might be possible to add to the existing modularize error messages |
| 241 | // (the second part referring to definitions being different), attempting |
| 242 | // to tie them to the last macro conflict encountered with respect to the |
| 243 | // order of the code encountered. |
| 244 | // |
| 245 | //===--------------------------------------------------------------------===// |
| 246 | |
| 247 | #include "clang/Lex/LexDiagnostic.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 248 | #include "PreprocessorTracker.h" |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 249 | #include "clang/Lex/MacroArgs.h" |
| 250 | #include "clang/Lex/PPCallbacks.h" |
John Thompson | 4ed963a | 2013-08-07 18:49:47 +0000 | [diff] [blame] | 251 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 252 | #include "llvm/Support/StringPool.h" |
| 253 | #include "llvm/Support/raw_ostream.h" |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 254 | |
| 255 | namespace Modularize { |
| 256 | |
| 257 | // Forwards. |
| 258 | class PreprocessorTrackerImpl; |
| 259 | |
| 260 | // Some handle types |
| 261 | typedef llvm::PooledStringPtr StringHandle; |
| 262 | |
| 263 | typedef int HeaderHandle; |
| 264 | const HeaderHandle HeaderHandleInvalid = -1; |
| 265 | |
| 266 | typedef int InclusionPathHandle; |
| 267 | const InclusionPathHandle InclusionPathHandleInvalid = -1; |
| 268 | |
| 269 | // Some utility functions. |
| 270 | |
| 271 | // Get a "file:line:column" source location string. |
| 272 | static std::string getSourceLocationString(clang::Preprocessor &PP, |
| 273 | clang::SourceLocation Loc) { |
| 274 | if (Loc.isInvalid()) |
| 275 | return std::string("(none)"); |
| 276 | else |
| 277 | return Loc.printToString(PP.getSourceManager()); |
| 278 | } |
| 279 | |
| 280 | // Get just the file name from a source location. |
| 281 | static std::string getSourceLocationFile(clang::Preprocessor &PP, |
| 282 | clang::SourceLocation Loc) { |
| 283 | std::string Source(getSourceLocationString(PP, Loc)); |
| 284 | size_t Offset = Source.find(':', 2); |
| 285 | if (Offset == std::string::npos) |
| 286 | return Source; |
| 287 | return Source.substr(0, Offset); |
| 288 | } |
| 289 | |
| 290 | // Get just the line and column from a source location. |
| 291 | static void getSourceLocationLineAndColumn(clang::Preprocessor &PP, |
| 292 | clang::SourceLocation Loc, int &Line, |
| 293 | int &Column) { |
| 294 | clang::PresumedLoc PLoc = PP.getSourceManager().getPresumedLoc(Loc); |
| 295 | if (PLoc.isInvalid()) { |
| 296 | Line = 0; |
| 297 | Column = 0; |
| 298 | return; |
| 299 | } |
| 300 | Line = PLoc.getLine(); |
| 301 | Column = PLoc.getColumn(); |
| 302 | } |
| 303 | |
| 304 | // Retrieve source snippet from file image. |
| 305 | std::string getSourceString(clang::Preprocessor &PP, clang::SourceRange Range) { |
| 306 | clang::SourceLocation BeginLoc = Range.getBegin(); |
| 307 | clang::SourceLocation EndLoc = Range.getEnd(); |
| 308 | const char *BeginPtr = PP.getSourceManager().getCharacterData(BeginLoc); |
| 309 | const char *EndPtr = PP.getSourceManager().getCharacterData(EndLoc); |
| 310 | size_t Length = EndPtr - BeginPtr; |
| 311 | return llvm::StringRef(BeginPtr, Length).trim().str(); |
| 312 | } |
| 313 | |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 314 | // Retrieve source line from file image given a location. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 315 | std::string getSourceLine(clang::Preprocessor &PP, clang::SourceLocation Loc) { |
| 316 | const llvm::MemoryBuffer *MemBuffer = |
| 317 | PP.getSourceManager().getBuffer(PP.getSourceManager().getFileID(Loc)); |
| 318 | const char *Buffer = MemBuffer->getBufferStart(); |
| 319 | const char *BufferEnd = MemBuffer->getBufferEnd(); |
| 320 | const char *BeginPtr = PP.getSourceManager().getCharacterData(Loc); |
| 321 | const char *EndPtr = BeginPtr; |
| 322 | while (BeginPtr > Buffer) { |
| 323 | if (*BeginPtr == '\n') { |
| 324 | BeginPtr++; |
| 325 | break; |
| 326 | } |
| 327 | BeginPtr--; |
| 328 | } |
| 329 | while (EndPtr < BufferEnd) { |
| 330 | if (*EndPtr == '\n') { |
| 331 | break; |
| 332 | } |
| 333 | EndPtr++; |
| 334 | } |
| 335 | size_t Length = EndPtr - BeginPtr; |
| 336 | return llvm::StringRef(BeginPtr, Length).str(); |
| 337 | } |
| 338 | |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 339 | // Retrieve source line from file image given a file ID and line number. |
| 340 | std::string getSourceLine(clang::Preprocessor &PP, clang::FileID FileID, |
| 341 | int Line) { |
| 342 | const llvm::MemoryBuffer *MemBuffer = PP.getSourceManager().getBuffer(FileID); |
| 343 | const char *Buffer = MemBuffer->getBufferStart(); |
| 344 | const char *BufferEnd = MemBuffer->getBufferEnd(); |
| 345 | const char *BeginPtr = Buffer; |
| 346 | const char *EndPtr = BufferEnd; |
| 347 | int LineCounter = 1; |
| 348 | if (Line == 1) |
| 349 | BeginPtr = Buffer; |
| 350 | else { |
| 351 | while (Buffer < BufferEnd) { |
| 352 | if (*Buffer == '\n') { |
| 353 | if (++LineCounter == Line) { |
| 354 | BeginPtr = Buffer++ + 1; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | Buffer++; |
| 359 | } |
| 360 | } |
| 361 | while (Buffer < BufferEnd) { |
| 362 | if (*Buffer == '\n') { |
| 363 | EndPtr = Buffer; |
| 364 | break; |
| 365 | } |
| 366 | Buffer++; |
| 367 | } |
| 368 | size_t Length = EndPtr - BeginPtr; |
| 369 | return llvm::StringRef(BeginPtr, Length).str(); |
| 370 | } |
| 371 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 372 | // Get the string for the Unexpanded macro instance. |
| 373 | // The soureRange is expected to end at the last token |
| 374 | // for the macro instance, which in the case of a function-style |
| 375 | // macro will be a ')', but for an object-style macro, it |
| 376 | // will be the macro name itself. |
| 377 | std::string getMacroUnexpandedString(clang::SourceRange Range, |
| 378 | clang::Preprocessor &PP, |
| 379 | llvm::StringRef MacroName, |
| 380 | const clang::MacroInfo *MI) { |
| 381 | clang::SourceLocation BeginLoc(Range.getBegin()); |
| 382 | const char *BeginPtr = PP.getSourceManager().getCharacterData(BeginLoc); |
| 383 | size_t Length; |
| 384 | std::string Unexpanded; |
| 385 | if (MI->isFunctionLike()) { |
| 386 | clang::SourceLocation EndLoc(Range.getEnd()); |
| 387 | const char *EndPtr = PP.getSourceManager().getCharacterData(EndLoc) + 1; |
| 388 | Length = (EndPtr - BeginPtr) + 1; // +1 is ')' width. |
| 389 | } else |
| 390 | Length = MacroName.size(); |
| 391 | return llvm::StringRef(BeginPtr, Length).trim().str(); |
| 392 | } |
| 393 | |
| 394 | // Get the expansion for a macro instance, given the information |
| 395 | // provided by PPCallbacks. |
John Thompson | 91555bd | 2013-08-09 00:22:20 +0000 | [diff] [blame] | 396 | // FIXME: This doesn't support function-style macro instances |
| 397 | // passed as arguments to another function-style macro. However, |
| 398 | // since it still expands the inner arguments, it still |
| 399 | // allows modularize to effectively work with respect to macro |
| 400 | // consistency checking, although it displays the incorrect |
| 401 | // expansion in error messages. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 402 | std::string getMacroExpandedString(clang::Preprocessor &PP, |
| 403 | llvm::StringRef MacroName, |
| 404 | const clang::MacroInfo *MI, |
| 405 | const clang::MacroArgs *Args) { |
| 406 | std::string Expanded; |
| 407 | // Walk over the macro Tokens. |
| 408 | typedef clang::MacroInfo::tokens_iterator Iter; |
| 409 | for (Iter I = MI->tokens_begin(), E = MI->tokens_end(); I != E; ++I) { |
| 410 | clang::IdentifierInfo *II = I->getIdentifierInfo(); |
| 411 | int ArgNo = (II && Args ? MI->getArgumentNum(II) : -1); |
| 412 | if (ArgNo == -1) { |
| 413 | // This isn't an argument, just add it. |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 414 | if (II == nullptr) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 415 | Expanded += PP.getSpelling((*I)); // Not an identifier. |
| 416 | else { |
| 417 | // Token is for an identifier. |
| 418 | std::string Name = II->getName().str(); |
| 419 | // Check for nexted macro references. |
| 420 | clang::MacroInfo *MacroInfo = PP.getMacroInfo(II); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 421 | if (MacroInfo) |
| 422 | Expanded += getMacroExpandedString(PP, Name, MacroInfo, nullptr); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 423 | else |
| 424 | Expanded += Name; |
| 425 | } |
| 426 | continue; |
| 427 | } |
| 428 | // We get here if it's a function-style macro with arguments. |
| 429 | const clang::Token *ResultArgToks; |
| 430 | const clang::Token *ArgTok = Args->getUnexpArgument(ArgNo); |
| 431 | if (Args->ArgNeedsPreexpansion(ArgTok, PP)) |
| 432 | ResultArgToks = &(const_cast<clang::MacroArgs *>(Args)) |
| 433 | ->getPreExpArgument(ArgNo, MI, PP)[0]; |
| 434 | else |
| 435 | ResultArgToks = ArgTok; // Use non-preexpanded Tokens. |
| 436 | // If the arg token didn't expand into anything, ignore it. |
| 437 | if (ResultArgToks->is(clang::tok::eof)) |
| 438 | continue; |
| 439 | unsigned NumToks = clang::MacroArgs::getArgLength(ResultArgToks); |
| 440 | // Append the resulting argument expansions. |
| 441 | for (unsigned ArgumentIndex = 0; ArgumentIndex < NumToks; ++ArgumentIndex) { |
| 442 | const clang::Token &AT = ResultArgToks[ArgumentIndex]; |
| 443 | clang::IdentifierInfo *II = AT.getIdentifierInfo(); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 444 | if (II == nullptr) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 445 | Expanded += PP.getSpelling(AT); // Not an identifier. |
| 446 | else { |
| 447 | // It's an identifier. Check for further expansion. |
| 448 | std::string Name = II->getName().str(); |
| 449 | clang::MacroInfo *MacroInfo = PP.getMacroInfo(II); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 450 | if (MacroInfo) |
| 451 | Expanded += getMacroExpandedString(PP, Name, MacroInfo, nullptr); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 452 | else |
| 453 | Expanded += Name; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | return Expanded; |
| 458 | } |
| 459 | |
| 460 | // Get the string representing a vector of Tokens. |
| 461 | std::string |
| 462 | getTokensSpellingString(clang::Preprocessor &PP, |
| 463 | llvm::SmallVectorImpl<clang::Token> &Tokens) { |
| 464 | std::string Expanded; |
| 465 | // Walk over the macro Tokens. |
| 466 | typedef llvm::SmallVectorImpl<clang::Token>::iterator Iter; |
| 467 | for (Iter I = Tokens.begin(), E = Tokens.end(); I != E; ++I) |
| 468 | Expanded += PP.getSpelling(*I); // Not an identifier. |
| 469 | return llvm::StringRef(Expanded).trim().str(); |
| 470 | } |
| 471 | |
| 472 | // Get the expansion for a macro instance, given the information |
| 473 | // provided by PPCallbacks. |
| 474 | std::string getExpandedString(clang::Preprocessor &PP, |
| 475 | llvm::StringRef MacroName, |
| 476 | const clang::MacroInfo *MI, |
| 477 | const clang::MacroArgs *Args) { |
| 478 | std::string Expanded; |
| 479 | // Walk over the macro Tokens. |
| 480 | typedef clang::MacroInfo::tokens_iterator Iter; |
| 481 | for (Iter I = MI->tokens_begin(), E = MI->tokens_end(); I != E; ++I) { |
| 482 | clang::IdentifierInfo *II = I->getIdentifierInfo(); |
| 483 | int ArgNo = (II && Args ? MI->getArgumentNum(II) : -1); |
| 484 | if (ArgNo == -1) { |
| 485 | // This isn't an argument, just add it. |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 486 | if (II == nullptr) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 487 | Expanded += PP.getSpelling((*I)); // Not an identifier. |
| 488 | else { |
| 489 | // Token is for an identifier. |
| 490 | std::string Name = II->getName().str(); |
| 491 | // Check for nexted macro references. |
| 492 | clang::MacroInfo *MacroInfo = PP.getMacroInfo(II); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 493 | if (MacroInfo) |
| 494 | Expanded += getMacroExpandedString(PP, Name, MacroInfo, nullptr); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 495 | else |
| 496 | Expanded += Name; |
| 497 | } |
| 498 | continue; |
| 499 | } |
| 500 | // We get here if it's a function-style macro with arguments. |
| 501 | const clang::Token *ResultArgToks; |
| 502 | const clang::Token *ArgTok = Args->getUnexpArgument(ArgNo); |
| 503 | if (Args->ArgNeedsPreexpansion(ArgTok, PP)) |
| 504 | ResultArgToks = &(const_cast<clang::MacroArgs *>(Args)) |
| 505 | ->getPreExpArgument(ArgNo, MI, PP)[0]; |
| 506 | else |
| 507 | ResultArgToks = ArgTok; // Use non-preexpanded Tokens. |
| 508 | // If the arg token didn't expand into anything, ignore it. |
| 509 | if (ResultArgToks->is(clang::tok::eof)) |
| 510 | continue; |
| 511 | unsigned NumToks = clang::MacroArgs::getArgLength(ResultArgToks); |
| 512 | // Append the resulting argument expansions. |
| 513 | for (unsigned ArgumentIndex = 0; ArgumentIndex < NumToks; ++ArgumentIndex) { |
| 514 | const clang::Token &AT = ResultArgToks[ArgumentIndex]; |
| 515 | clang::IdentifierInfo *II = AT.getIdentifierInfo(); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 516 | if (II == nullptr) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 517 | Expanded += PP.getSpelling(AT); // Not an identifier. |
| 518 | else { |
| 519 | // It's an identifier. Check for further expansion. |
| 520 | std::string Name = II->getName().str(); |
| 521 | clang::MacroInfo *MacroInfo = PP.getMacroInfo(II); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 522 | if (MacroInfo) |
| 523 | Expanded += getMacroExpandedString(PP, Name, MacroInfo, nullptr); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 524 | else |
| 525 | Expanded += Name; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | return Expanded; |
| 530 | } |
| 531 | |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 532 | // ConditionValueKind strings. |
| 533 | const char * |
| 534 | ConditionValueKindStrings[] = { |
| 535 | "(not evaluated)", "false", "true" |
| 536 | }; |
| 537 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 538 | bool operator<(const StringHandle &H1, const StringHandle &H2) { |
| 539 | const char *S1 = (H1 ? *H1 : ""); |
| 540 | const char *S2 = (H2 ? *H2 : ""); |
| 541 | int Diff = strcmp(S1, S2); |
| 542 | return Diff < 0; |
| 543 | } |
| 544 | bool operator>(const StringHandle &H1, const StringHandle &H2) { |
| 545 | const char *S1 = (H1 ? *H1 : ""); |
| 546 | const char *S2 = (H2 ? *H2 : ""); |
| 547 | int Diff = strcmp(S1, S2); |
| 548 | return Diff > 0; |
| 549 | } |
| 550 | |
| 551 | // Preprocessor item key. |
| 552 | // |
| 553 | // This class represents a location in a source file, for use |
| 554 | // as a key representing a unique name/file/line/column quadruplet, |
| 555 | // which in this case is used to identify a macro expansion instance, |
| 556 | // but could be used for other things as well. |
| 557 | // The file is a header file handle, the line is a line number, |
| 558 | // and the column is a column number. |
| 559 | class PPItemKey { |
| 560 | public: |
| 561 | PPItemKey(clang::Preprocessor &PP, StringHandle Name, HeaderHandle File, |
| 562 | clang::SourceLocation Loc) |
| 563 | : Name(Name), File(File) { |
| 564 | getSourceLocationLineAndColumn(PP, Loc, Line, Column); |
| 565 | } |
| 566 | PPItemKey(StringHandle Name, HeaderHandle File, int Line, int Column) |
| 567 | : Name(Name), File(File), Line(Line), Column(Column) {} |
| 568 | PPItemKey(const PPItemKey &Other) |
| 569 | : Name(Other.Name), File(Other.File), Line(Other.Line), |
| 570 | Column(Other.Column) {} |
| 571 | PPItemKey() : File(HeaderHandleInvalid), Line(0), Column(0) {} |
| 572 | bool operator==(const PPItemKey &Other) const { |
| 573 | if (Name != Other.Name) |
| 574 | return false; |
| 575 | if (File != Other.File) |
| 576 | return false; |
| 577 | if (Line != Other.Line) |
| 578 | return false; |
| 579 | return Column == Other.Column; |
| 580 | } |
| 581 | bool operator<(const PPItemKey &Other) const { |
| 582 | if (Name < Other.Name) |
| 583 | return true; |
| 584 | else if (Name > Other.Name) |
| 585 | return false; |
| 586 | if (File < Other.File) |
| 587 | return true; |
| 588 | else if (File > Other.File) |
| 589 | return false; |
| 590 | if (Line < Other.Line) |
| 591 | return true; |
| 592 | else if (Line > Other.Line) |
| 593 | return false; |
| 594 | return Column < Other.Column; |
| 595 | } |
| 596 | StringHandle Name; |
| 597 | HeaderHandle File; |
| 598 | int Line; |
| 599 | int Column; |
| 600 | }; |
| 601 | |
| 602 | // Header inclusion path. |
| 603 | class HeaderInclusionPath { |
| 604 | public: |
| 605 | HeaderInclusionPath(std::vector<HeaderHandle> HeaderInclusionPath) |
| 606 | : Path(HeaderInclusionPath) {} |
| 607 | HeaderInclusionPath(const HeaderInclusionPath &Other) : Path(Other.Path) {} |
| 608 | HeaderInclusionPath() {} |
| 609 | std::vector<HeaderHandle> Path; |
| 610 | }; |
| 611 | |
| 612 | // Macro expansion instance. |
| 613 | // |
| 614 | // This class represents an instance of a macro expansion with a |
| 615 | // unique value. It also stores the unique header inclusion paths |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 616 | // for use in telling the user the nested include path to the header. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 617 | class MacroExpansionInstance { |
| 618 | public: |
| 619 | MacroExpansionInstance(StringHandle MacroExpanded, |
| 620 | PPItemKey &DefinitionLocation, |
| 621 | StringHandle DefinitionSourceLine, |
| 622 | InclusionPathHandle H) |
| 623 | : MacroExpanded(MacroExpanded), DefinitionLocation(DefinitionLocation), |
| 624 | DefinitionSourceLine(DefinitionSourceLine) { |
| 625 | InclusionPathHandles.push_back(H); |
| 626 | } |
| 627 | MacroExpansionInstance() {} |
| 628 | |
| 629 | // Check for the presence of a header inclusion path handle entry. |
| 630 | // Return false if not found. |
| 631 | bool haveInclusionPathHandle(InclusionPathHandle H) { |
| 632 | for (std::vector<InclusionPathHandle>::iterator |
| 633 | I = InclusionPathHandles.begin(), |
| 634 | E = InclusionPathHandles.end(); |
| 635 | I != E; ++I) { |
| 636 | if (*I == H) |
| 637 | return true; |
| 638 | } |
| 639 | return InclusionPathHandleInvalid; |
| 640 | } |
| 641 | // Add a new header inclusion path entry, if not already present. |
| 642 | void addInclusionPathHandle(InclusionPathHandle H) { |
| 643 | if (!haveInclusionPathHandle(H)) |
| 644 | InclusionPathHandles.push_back(H); |
| 645 | } |
| 646 | |
| 647 | // A string representing the macro instance after preprocessing. |
| 648 | StringHandle MacroExpanded; |
| 649 | // A file/line/column triplet representing the macro definition location. |
| 650 | PPItemKey DefinitionLocation; |
| 651 | // A place to save the macro definition line string. |
| 652 | StringHandle DefinitionSourceLine; |
| 653 | // The header inclusion path handles for all the instances. |
| 654 | std::vector<InclusionPathHandle> InclusionPathHandles; |
| 655 | }; |
| 656 | |
| 657 | // Macro expansion instance tracker. |
| 658 | // |
| 659 | // This class represents one macro expansion, keyed by a PPItemKey. |
| 660 | // It stores a string representing the macro reference in the source, |
| 661 | // and a list of ConditionalExpansionInstances objects representing |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 662 | // the unique values the condition expands to in instances of the header. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 663 | class MacroExpansionTracker { |
| 664 | public: |
| 665 | MacroExpansionTracker(StringHandle MacroUnexpanded, |
| 666 | StringHandle MacroExpanded, |
| 667 | StringHandle InstanceSourceLine, |
| 668 | PPItemKey &DefinitionLocation, |
| 669 | StringHandle DefinitionSourceLine, |
| 670 | InclusionPathHandle InclusionPathHandle) |
| 671 | : MacroUnexpanded(MacroUnexpanded), |
| 672 | InstanceSourceLine(InstanceSourceLine) { |
| 673 | addMacroExpansionInstance(MacroExpanded, DefinitionLocation, |
| 674 | DefinitionSourceLine, InclusionPathHandle); |
| 675 | } |
| 676 | MacroExpansionTracker() {} |
| 677 | |
| 678 | // Find a matching macro expansion instance. |
| 679 | MacroExpansionInstance * |
| 680 | findMacroExpansionInstance(StringHandle MacroExpanded, |
| 681 | PPItemKey &DefinitionLocation) { |
| 682 | for (std::vector<MacroExpansionInstance>::iterator |
| 683 | I = MacroExpansionInstances.begin(), |
| 684 | E = MacroExpansionInstances.end(); |
| 685 | I != E; ++I) { |
| 686 | if ((I->MacroExpanded == MacroExpanded) && |
| 687 | (I->DefinitionLocation == DefinitionLocation)) { |
| 688 | return &*I; // Found. |
| 689 | } |
| 690 | } |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 691 | return nullptr; // Not found. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Add a macro expansion instance. |
| 695 | void addMacroExpansionInstance(StringHandle MacroExpanded, |
| 696 | PPItemKey &DefinitionLocation, |
| 697 | StringHandle DefinitionSourceLine, |
| 698 | InclusionPathHandle InclusionPathHandle) { |
| 699 | MacroExpansionInstances.push_back( |
| 700 | MacroExpansionInstance(MacroExpanded, DefinitionLocation, |
| 701 | DefinitionSourceLine, InclusionPathHandle)); |
| 702 | } |
| 703 | |
| 704 | // Return true if there is a mismatch. |
| 705 | bool hasMismatch() { return MacroExpansionInstances.size() > 1; } |
| 706 | |
| 707 | // A string representing the macro instance without expansion. |
| 708 | StringHandle MacroUnexpanded; |
| 709 | // A place to save the macro instance source line string. |
| 710 | StringHandle InstanceSourceLine; |
| 711 | // The macro expansion instances. |
| 712 | // If all instances of the macro expansion expand to the same value, |
| 713 | // This vector will only have one instance. |
| 714 | std::vector<MacroExpansionInstance> MacroExpansionInstances; |
| 715 | }; |
| 716 | |
| 717 | // Conditional expansion instance. |
| 718 | // |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 719 | // This class represents an instance of a condition exoression result |
| 720 | // with a unique value. It also stores the unique header inclusion paths |
| 721 | // for use in telling the user the nested include path to the header. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 722 | class ConditionalExpansionInstance { |
| 723 | public: |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 724 | ConditionalExpansionInstance(clang::PPCallbacks::ConditionValueKind ConditionValue, InclusionPathHandle H) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 725 | : ConditionValue(ConditionValue) { |
| 726 | InclusionPathHandles.push_back(H); |
| 727 | } |
| 728 | ConditionalExpansionInstance() {} |
| 729 | |
| 730 | // Check for the presence of a header inclusion path handle entry. |
| 731 | // Return false if not found. |
| 732 | bool haveInclusionPathHandle(InclusionPathHandle H) { |
| 733 | for (std::vector<InclusionPathHandle>::iterator |
| 734 | I = InclusionPathHandles.begin(), |
| 735 | E = InclusionPathHandles.end(); |
| 736 | I != E; ++I) { |
| 737 | if (*I == H) |
| 738 | return true; |
| 739 | } |
| 740 | return InclusionPathHandleInvalid; |
| 741 | } |
| 742 | // Add a new header inclusion path entry, if not already present. |
| 743 | void addInclusionPathHandle(InclusionPathHandle H) { |
| 744 | if (!haveInclusionPathHandle(H)) |
| 745 | InclusionPathHandles.push_back(H); |
| 746 | } |
| 747 | |
| 748 | // A flag representing the evaluated condition value. |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 749 | clang::PPCallbacks::ConditionValueKind ConditionValue; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 750 | // The header inclusion path handles for all the instances. |
| 751 | std::vector<InclusionPathHandle> InclusionPathHandles; |
| 752 | }; |
| 753 | |
| 754 | // Conditional directive instance tracker. |
| 755 | // |
| 756 | // This class represents one conditional directive, keyed by a PPItemKey. |
| 757 | // It stores a string representing the macro reference in the source, |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 758 | // and a list of ConditionExpansionInstance objects representing |
| 759 | // the unique value the condition expression expands to in instances of |
| 760 | // the header. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 761 | class ConditionalTracker { |
| 762 | public: |
| 763 | ConditionalTracker(clang::tok::PPKeywordKind DirectiveKind, |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 764 | clang::PPCallbacks::ConditionValueKind ConditionValue, |
| 765 | StringHandle ConditionUnexpanded, |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 766 | InclusionPathHandle InclusionPathHandle) |
| 767 | : DirectiveKind(DirectiveKind), ConditionUnexpanded(ConditionUnexpanded) { |
| 768 | addConditionalExpansionInstance(ConditionValue, InclusionPathHandle); |
| 769 | } |
| 770 | ConditionalTracker() {} |
| 771 | |
| 772 | // Find a matching condition expansion instance. |
| 773 | ConditionalExpansionInstance * |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 774 | findConditionalExpansionInstance(clang::PPCallbacks::ConditionValueKind ConditionValue) { |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 775 | for (std::vector<ConditionalExpansionInstance>::iterator |
| 776 | I = ConditionalExpansionInstances.begin(), |
| 777 | E = ConditionalExpansionInstances.end(); |
| 778 | I != E; ++I) { |
| 779 | if (I->ConditionValue == ConditionValue) { |
| 780 | return &*I; // Found. |
| 781 | } |
| 782 | } |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 783 | return nullptr; // Not found. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // Add a conditional expansion instance. |
| 787 | void |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 788 | addConditionalExpansionInstance(clang::PPCallbacks::ConditionValueKind ConditionValue, |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 789 | InclusionPathHandle InclusionPathHandle) { |
| 790 | ConditionalExpansionInstances.push_back( |
| 791 | ConditionalExpansionInstance(ConditionValue, InclusionPathHandle)); |
| 792 | } |
| 793 | |
| 794 | // Return true if there is a mismatch. |
| 795 | bool hasMismatch() { return ConditionalExpansionInstances.size() > 1; } |
| 796 | |
| 797 | // The kind of directive. |
| 798 | clang::tok::PPKeywordKind DirectiveKind; |
| 799 | // A string representing the macro instance without expansion. |
| 800 | StringHandle ConditionUnexpanded; |
| 801 | // The condition expansion instances. |
| 802 | // If all instances of the conditional expression expand to the same value, |
| 803 | // This vector will only have one instance. |
| 804 | std::vector<ConditionalExpansionInstance> ConditionalExpansionInstances; |
| 805 | }; |
| 806 | |
| 807 | // Preprocessor callbacks for modularize. |
| 808 | // |
| 809 | // This class derives from the Clang PPCallbacks class to track preprocessor |
| 810 | // actions, such as changing files and handling preprocessor directives and |
| 811 | // macro expansions. It has to figure out when a new header file is entered |
| 812 | // and left, as the provided handler is not particularly clear about it. |
| 813 | class PreprocessorCallbacks : public clang::PPCallbacks { |
| 814 | public: |
| 815 | PreprocessorCallbacks(PreprocessorTrackerImpl &ppTracker, |
| 816 | clang::Preprocessor &PP, llvm::StringRef rootHeaderFile) |
| 817 | : PPTracker(ppTracker), PP(PP), RootHeaderFile(rootHeaderFile) {} |
| 818 | ~PreprocessorCallbacks() {} |
| 819 | |
| 820 | // Overridden handlers. |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 821 | void InclusionDirective(clang::SourceLocation HashLoc, |
| 822 | const clang::Token &IncludeTok, |
| 823 | llvm::StringRef FileName, bool IsAngled, |
| 824 | clang::CharSourceRange FilenameRange, |
| 825 | const clang::FileEntry *File, |
| 826 | llvm::StringRef SearchPath, |
| 827 | llvm::StringRef RelativePath, |
| 828 | const clang::Module *Imported); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 829 | void FileChanged(clang::SourceLocation Loc, |
| 830 | clang::PPCallbacks::FileChangeReason Reason, |
| 831 | clang::SrcMgr::CharacteristicKind FileType, |
| 832 | clang::FileID PrevFID = clang::FileID()); |
| 833 | void MacroExpands(const clang::Token &MacroNameTok, |
| 834 | const clang::MacroDirective *MD, clang::SourceRange Range, |
| 835 | const clang::MacroArgs *Args); |
| 836 | void Defined(const clang::Token &MacroNameTok, |
| 837 | const clang::MacroDirective *MD, clang::SourceRange Range); |
| 838 | void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange, |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 839 | clang::PPCallbacks::ConditionValueKind ConditionResult); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 840 | void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange, |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 841 | clang::PPCallbacks::ConditionValueKind ConditionResult, clang::SourceLocation IfLoc); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 842 | void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, |
| 843 | const clang::MacroDirective *MD); |
| 844 | void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, |
| 845 | const clang::MacroDirective *MD); |
| 846 | |
| 847 | private: |
| 848 | PreprocessorTrackerImpl &PPTracker; |
| 849 | clang::Preprocessor &PP; |
| 850 | std::string RootHeaderFile; |
| 851 | }; |
| 852 | |
| 853 | // Preprocessor macro expansion item map types. |
| 854 | typedef std::map<PPItemKey, MacroExpansionTracker> MacroExpansionMap; |
| 855 | typedef std::map<PPItemKey, MacroExpansionTracker>::iterator |
| 856 | MacroExpansionMapIter; |
| 857 | |
| 858 | // Preprocessor conditional expansion item map types. |
| 859 | typedef std::map<PPItemKey, ConditionalTracker> ConditionalExpansionMap; |
| 860 | typedef std::map<PPItemKey, ConditionalTracker>::iterator |
| 861 | ConditionalExpansionMapIter; |
| 862 | |
| 863 | // Preprocessor tracker for modularize. |
| 864 | // |
| 865 | // This class stores information about all the headers processed in the |
| 866 | // course of running modularize. |
| 867 | class PreprocessorTrackerImpl : public PreprocessorTracker { |
| 868 | public: |
| 869 | PreprocessorTrackerImpl() |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 870 | : CurrentInclusionPathHandle(InclusionPathHandleInvalid), |
| 871 | InNestedHeader(false) {} |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 872 | ~PreprocessorTrackerImpl() {} |
| 873 | |
| 874 | // Handle entering a preprocessing session. |
| 875 | void handlePreprocessorEntry(clang::Preprocessor &PP, |
| 876 | llvm::StringRef rootHeaderFile) { |
John Thompson | 4ed963a | 2013-08-07 18:49:47 +0000 | [diff] [blame] | 877 | HeadersInThisCompile.clear(); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 878 | assert((HeaderStack.size() == 0) && "Header stack should be empty."); |
| 879 | pushHeaderHandle(addHeader(rootHeaderFile)); |
Craig Topper | 775862a | 2014-09-10 05:07:57 +0000 | [diff] [blame^] | 880 | PP.addPPCallbacks(llvm::make_unique<PreprocessorCallbacks>(*this, PP, |
| 881 | rootHeaderFile)); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 882 | } |
| 883 | // Handle exiting a preprocessing session. |
| 884 | void handlePreprocessorExit() { HeaderStack.clear(); } |
| 885 | |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 886 | // Handle include directive. |
| 887 | // This function is called every time an include directive is seen by the |
| 888 | // preprocessor, for the purpose of later checking for 'extern "" {}' or |
| 889 | // "namespace {}" blocks containing #include directives. |
| 890 | void handleIncludeDirective(llvm::StringRef DirectivePath, int DirectiveLine, |
| 891 | int DirectiveColumn, llvm::StringRef TargetPath) { |
| 892 | HeaderHandle CurrentHeaderHandle = findHeaderHandle(DirectivePath); |
| 893 | StringHandle IncludeHeaderHandle = addString(TargetPath); |
| 894 | for (std::vector<PPItemKey>::const_iterator I = IncludeDirectives.begin(), |
| 895 | E = IncludeDirectives.end(); |
| 896 | I != E; ++I) { |
| 897 | // If we already have an entry for this directive, return now. |
| 898 | if ((I->File == CurrentHeaderHandle) && (I->Line == DirectiveLine)) |
| 899 | return; |
| 900 | } |
| 901 | PPItemKey IncludeDirectiveItem(IncludeHeaderHandle, CurrentHeaderHandle, |
| 902 | DirectiveLine, DirectiveColumn); |
| 903 | IncludeDirectives.push_back(IncludeDirectiveItem); |
| 904 | } |
| 905 | |
| 906 | // Check for include directives within the given source line range. |
| 907 | // Report errors if any found. Returns true if no include directives |
| 908 | // found in block. |
| 909 | bool checkForIncludesInBlock(clang::Preprocessor &PP, |
| 910 | clang::SourceRange BlockSourceRange, |
| 911 | const char *BlockIdentifierMessage, |
| 912 | llvm::raw_ostream &OS) { |
| 913 | clang::SourceLocation BlockStartLoc = BlockSourceRange.getBegin(); |
| 914 | clang::SourceLocation BlockEndLoc = BlockSourceRange.getEnd(); |
| 915 | // Use block location to get FileID of both the include directive |
| 916 | // and block statement. |
| 917 | clang::FileID FileID = PP.getSourceManager().getFileID(BlockStartLoc); |
| 918 | std::string SourcePath = getSourceLocationFile(PP, BlockStartLoc); |
| 919 | HeaderHandle SourceHandle = findHeaderHandle(SourcePath); |
| 920 | int BlockStartLine, BlockStartColumn, BlockEndLine, BlockEndColumn; |
| 921 | bool returnValue = true; |
| 922 | getSourceLocationLineAndColumn(PP, BlockStartLoc, BlockStartLine, |
| 923 | BlockStartColumn); |
| 924 | getSourceLocationLineAndColumn(PP, BlockEndLoc, BlockEndLine, |
| 925 | BlockEndColumn); |
| 926 | for (std::vector<PPItemKey>::const_iterator I = IncludeDirectives.begin(), |
| 927 | E = IncludeDirectives.end(); |
| 928 | I != E; ++I) { |
| 929 | // If we find an entry within the block, report an error. |
| 930 | if ((I->File == SourceHandle) && (I->Line >= BlockStartLine) && |
| 931 | (I->Line < BlockEndLine)) { |
| 932 | returnValue = false; |
John Thompson | a2b6687 | 2013-09-20 14:44:20 +0000 | [diff] [blame] | 933 | OS << SourcePath << ":" << I->Line << ":" << I->Column << ":\n"; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 934 | OS << getSourceLine(PP, FileID, I->Line) << "\n"; |
| 935 | if (I->Column > 0) |
| 936 | OS << std::string(I->Column - 1, ' ') << "^\n"; |
| 937 | OS << "error: Include directive within " << BlockIdentifierMessage |
| 938 | << ".\n"; |
| 939 | OS << SourcePath << ":" << BlockStartLine << ":" << BlockStartColumn |
John Thompson | a2b6687 | 2013-09-20 14:44:20 +0000 | [diff] [blame] | 940 | << ":\n"; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 941 | OS << getSourceLine(PP, BlockStartLoc) << "\n"; |
| 942 | if (BlockStartColumn > 0) |
| 943 | OS << std::string(BlockStartColumn - 1, ' ') << "^\n"; |
| 944 | OS << "The \"" << BlockIdentifierMessage << "\" block is here.\n"; |
| 945 | } |
| 946 | } |
| 947 | return returnValue; |
| 948 | } |
| 949 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 950 | // Handle entering a header source file. |
| 951 | void handleHeaderEntry(clang::Preprocessor &PP, llvm::StringRef HeaderPath) { |
| 952 | // Ignore <built-in> and <command-line> to reduce message clutter. |
| 953 | if (HeaderPath.startswith("<")) |
| 954 | return; |
| 955 | HeaderHandle H = addHeader(HeaderPath); |
John Thompson | 4ed963a | 2013-08-07 18:49:47 +0000 | [diff] [blame] | 956 | if (H != getCurrentHeaderHandle()) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 957 | pushHeaderHandle(H); |
John Thompson | 4ed963a | 2013-08-07 18:49:47 +0000 | [diff] [blame] | 958 | // Check for nested header. |
| 959 | if (!InNestedHeader) |
| 960 | InNestedHeader = !HeadersInThisCompile.insert(H); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 961 | } |
| 962 | // Handle exiting a header source file. |
| 963 | void handleHeaderExit(llvm::StringRef HeaderPath) { |
| 964 | // Ignore <built-in> and <command-line> to reduce message clutter. |
| 965 | if (HeaderPath.startswith("<")) |
| 966 | return; |
| 967 | HeaderHandle H = findHeaderHandle(HeaderPath); |
| 968 | if (isHeaderHandleInStack(H)) { |
| 969 | while ((H != getCurrentHeaderHandle()) && (HeaderStack.size() != 0)) |
| 970 | popHeaderHandle(); |
| 971 | } |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 972 | InNestedHeader = false; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | // Lookup/add string. |
| 976 | StringHandle addString(llvm::StringRef Str) { return Strings.intern(Str); } |
| 977 | |
| 978 | // Get the handle of a header file entry. |
| 979 | // Return HeaderHandleInvalid if not found. |
| 980 | HeaderHandle findHeaderHandle(llvm::StringRef HeaderPath) const { |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 981 | std::string CanonicalPath(HeaderPath); |
| 982 | std::replace(CanonicalPath.begin(), CanonicalPath.end(), '\\', '/'); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 983 | HeaderHandle H = 0; |
| 984 | for (std::vector<StringHandle>::const_iterator I = HeaderPaths.begin(), |
| 985 | E = HeaderPaths.end(); |
| 986 | I != E; ++I, ++H) { |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 987 | if (**I == CanonicalPath) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 988 | return H; |
| 989 | } |
| 990 | return HeaderHandleInvalid; |
| 991 | } |
| 992 | |
| 993 | // Add a new header file entry, or return existing handle. |
| 994 | // Return the header handle. |
| 995 | HeaderHandle addHeader(llvm::StringRef HeaderPath) { |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 996 | std::string CanonicalPath(HeaderPath); |
| 997 | std::replace(CanonicalPath.begin(), CanonicalPath.end(), '\\', '/'); |
| 998 | HeaderHandle H = findHeaderHandle(CanonicalPath); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 999 | if (H == HeaderHandleInvalid) { |
| 1000 | H = HeaderPaths.size(); |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 1001 | HeaderPaths.push_back(addString(CanonicalPath)); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1002 | } |
| 1003 | return H; |
| 1004 | } |
| 1005 | |
| 1006 | // Return a header file path string given its handle. |
| 1007 | StringHandle getHeaderFilePath(HeaderHandle H) const { |
| 1008 | if ((H >= 0) && (H < (HeaderHandle)HeaderPaths.size())) |
| 1009 | return HeaderPaths[H]; |
| 1010 | return StringHandle(); |
| 1011 | } |
| 1012 | |
| 1013 | // Returns a handle to the inclusion path. |
| 1014 | InclusionPathHandle pushHeaderHandle(HeaderHandle H) { |
| 1015 | HeaderStack.push_back(H); |
| 1016 | return CurrentInclusionPathHandle = addInclusionPathHandle(HeaderStack); |
| 1017 | } |
| 1018 | // Pops the last header handle from the stack; |
| 1019 | void popHeaderHandle() { |
| 1020 | // assert((HeaderStack.size() != 0) && "Header stack already empty."); |
| 1021 | if (HeaderStack.size() != 0) { |
| 1022 | HeaderStack.pop_back(); |
| 1023 | CurrentInclusionPathHandle = addInclusionPathHandle(HeaderStack); |
| 1024 | } |
| 1025 | } |
| 1026 | // Get the top handle on the header stack. |
| 1027 | HeaderHandle getCurrentHeaderHandle() const { |
| 1028 | if (HeaderStack.size() != 0) |
| 1029 | return HeaderStack.back(); |
| 1030 | return HeaderHandleInvalid; |
| 1031 | } |
| 1032 | |
| 1033 | // Check for presence of header handle in the header stack. |
| 1034 | bool isHeaderHandleInStack(HeaderHandle H) const { |
| 1035 | for (std::vector<HeaderHandle>::const_iterator I = HeaderStack.begin(), |
| 1036 | E = HeaderStack.end(); |
| 1037 | I != E; ++I) { |
| 1038 | if (*I == H) |
| 1039 | return true; |
| 1040 | } |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | // Get the handle of a header inclusion path entry. |
| 1045 | // Return InclusionPathHandleInvalid if not found. |
| 1046 | InclusionPathHandle |
| 1047 | findInclusionPathHandle(const std::vector<HeaderHandle> &Path) const { |
| 1048 | InclusionPathHandle H = 0; |
| 1049 | for (std::vector<HeaderInclusionPath>::const_iterator |
| 1050 | I = InclusionPaths.begin(), |
| 1051 | E = InclusionPaths.end(); |
| 1052 | I != E; ++I, ++H) { |
| 1053 | if (I->Path == Path) |
| 1054 | return H; |
| 1055 | } |
| 1056 | return HeaderHandleInvalid; |
| 1057 | } |
| 1058 | // Add a new header inclusion path entry, or return existing handle. |
| 1059 | // Return the header inclusion path entry handle. |
| 1060 | InclusionPathHandle |
| 1061 | addInclusionPathHandle(const std::vector<HeaderHandle> &Path) { |
| 1062 | InclusionPathHandle H = findInclusionPathHandle(Path); |
| 1063 | if (H == HeaderHandleInvalid) { |
| 1064 | H = InclusionPaths.size(); |
| 1065 | InclusionPaths.push_back(HeaderInclusionPath(Path)); |
| 1066 | } |
| 1067 | return H; |
| 1068 | } |
| 1069 | // Return the current inclusion path handle. |
| 1070 | InclusionPathHandle getCurrentInclusionPathHandle() const { |
| 1071 | return CurrentInclusionPathHandle; |
| 1072 | } |
| 1073 | |
| 1074 | // Return an inclusion path given its handle. |
| 1075 | const std::vector<HeaderHandle> & |
| 1076 | getInclusionPath(InclusionPathHandle H) const { |
| 1077 | if ((H >= 0) && (H <= (InclusionPathHandle)InclusionPaths.size())) |
| 1078 | return InclusionPaths[H].Path; |
| 1079 | static std::vector<HeaderHandle> Empty; |
| 1080 | return Empty; |
| 1081 | } |
| 1082 | |
| 1083 | // Add a macro expansion instance. |
| 1084 | void addMacroExpansionInstance(clang::Preprocessor &PP, HeaderHandle H, |
| 1085 | clang::SourceLocation InstanceLoc, |
| 1086 | clang::SourceLocation DefinitionLoc, |
| 1087 | clang::IdentifierInfo *II, |
| 1088 | llvm::StringRef MacroUnexpanded, |
| 1089 | llvm::StringRef MacroExpanded, |
| 1090 | InclusionPathHandle InclusionPathHandle) { |
John Thompson | c8d710c | 2013-08-13 18:11:36 +0000 | [diff] [blame] | 1091 | if (InNestedHeader) |
| 1092 | return; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1093 | StringHandle MacroName = addString(II->getName()); |
| 1094 | PPItemKey InstanceKey(PP, MacroName, H, InstanceLoc); |
| 1095 | PPItemKey DefinitionKey(PP, MacroName, H, DefinitionLoc); |
| 1096 | MacroExpansionMapIter I = MacroExpansions.find(InstanceKey); |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1097 | // If existing instance of expansion not found, add one. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1098 | if (I == MacroExpansions.end()) { |
| 1099 | std::string InstanceSourceLine = |
| 1100 | getSourceLocationString(PP, InstanceLoc) + ":\n" + |
| 1101 | getSourceLine(PP, InstanceLoc) + "\n"; |
| 1102 | std::string DefinitionSourceLine = |
| 1103 | getSourceLocationString(PP, DefinitionLoc) + ":\n" + |
| 1104 | getSourceLine(PP, DefinitionLoc) + "\n"; |
| 1105 | MacroExpansions[InstanceKey] = MacroExpansionTracker( |
| 1106 | addString(MacroUnexpanded), addString(MacroExpanded), |
| 1107 | addString(InstanceSourceLine), DefinitionKey, |
| 1108 | addString(DefinitionSourceLine), InclusionPathHandle); |
| 1109 | } else { |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1110 | // We've seen the macro before. Get its tracker. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1111 | MacroExpansionTracker &CondTracker = I->second; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1112 | // Look up an existing instance value for the macro. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1113 | MacroExpansionInstance *MacroInfo = |
| 1114 | CondTracker.findMacroExpansionInstance(addString(MacroExpanded), |
| 1115 | DefinitionKey); |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1116 | // If found, just add the inclusion path to the instance. |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 1117 | if (MacroInfo) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1118 | MacroInfo->addInclusionPathHandle(InclusionPathHandle); |
| 1119 | else { |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1120 | // Otherwise add a new instance with the unique value. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1121 | std::string DefinitionSourceLine = |
| 1122 | getSourceLocationString(PP, DefinitionLoc) + ":\n" + |
| 1123 | getSourceLine(PP, DefinitionLoc) + "\n"; |
| 1124 | CondTracker.addMacroExpansionInstance( |
| 1125 | addString(MacroExpanded), DefinitionKey, |
| 1126 | addString(DefinitionSourceLine), InclusionPathHandle); |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // Add a conditional expansion instance. |
| 1132 | void |
| 1133 | addConditionalExpansionInstance(clang::Preprocessor &PP, HeaderHandle H, |
| 1134 | clang::SourceLocation InstanceLoc, |
| 1135 | clang::tok::PPKeywordKind DirectiveKind, |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 1136 | clang::PPCallbacks::ConditionValueKind ConditionValue, |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1137 | llvm::StringRef ConditionUnexpanded, |
| 1138 | InclusionPathHandle InclusionPathHandle) { |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 1139 | // Ignore header guards, assuming the header guard is the only conditional. |
| 1140 | if (InNestedHeader) |
| 1141 | return; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1142 | StringHandle ConditionUnexpandedHandle(addString(ConditionUnexpanded)); |
| 1143 | PPItemKey InstanceKey(PP, ConditionUnexpandedHandle, H, InstanceLoc); |
| 1144 | ConditionalExpansionMapIter I = ConditionalExpansions.find(InstanceKey); |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1145 | // If existing instance of condition not found, add one. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1146 | if (I == ConditionalExpansions.end()) { |
| 1147 | std::string InstanceSourceLine = |
| 1148 | getSourceLocationString(PP, InstanceLoc) + ":\n" + |
| 1149 | getSourceLine(PP, InstanceLoc) + "\n"; |
| 1150 | ConditionalExpansions[InstanceKey] = |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 1151 | ConditionalTracker(DirectiveKind, ConditionValue, |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 1152 | ConditionUnexpandedHandle, InclusionPathHandle); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1153 | } else { |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1154 | // We've seen the conditional before. Get its tracker. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1155 | ConditionalTracker &CondTracker = I->second; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1156 | // Look up an existing instance value for the condition. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1157 | ConditionalExpansionInstance *MacroInfo = |
| 1158 | CondTracker.findConditionalExpansionInstance(ConditionValue); |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1159 | // If found, just add the inclusion path to the instance. |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 1160 | if (MacroInfo) |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1161 | MacroInfo->addInclusionPathHandle(InclusionPathHandle); |
| 1162 | else { |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1163 | // Otherwise add a new instance with the unique value. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1164 | CondTracker.addConditionalExpansionInstance(ConditionValue, |
| 1165 | InclusionPathHandle); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | // Report on inconsistent macro instances. |
| 1171 | // Returns true if any mismatches. |
| 1172 | bool reportInconsistentMacros(llvm::raw_ostream &OS) { |
| 1173 | bool ReturnValue = false; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1174 | // Walk all the macro expansion trackers in the map. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1175 | for (MacroExpansionMapIter I = MacroExpansions.begin(), |
| 1176 | E = MacroExpansions.end(); |
| 1177 | I != E; ++I) { |
| 1178 | const PPItemKey &ItemKey = I->first; |
| 1179 | MacroExpansionTracker &MacroExpTracker = I->second; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1180 | // If no mismatch (only one instance value) continue. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1181 | if (!MacroExpTracker.hasMismatch()) |
| 1182 | continue; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1183 | // Tell caller we found one or more errors. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1184 | ReturnValue = true; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1185 | // Start the error message. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1186 | OS << *MacroExpTracker.InstanceSourceLine; |
| 1187 | if (ItemKey.Column > 0) |
| 1188 | OS << std::string(ItemKey.Column - 1, ' ') << "^\n"; |
| 1189 | OS << "error: Macro instance '" << *MacroExpTracker.MacroUnexpanded |
| 1190 | << "' has different values in this header, depending on how it was " |
| 1191 | "included.\n"; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1192 | // Walk all the instances. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1193 | for (std::vector<MacroExpansionInstance>::iterator |
| 1194 | IMT = MacroExpTracker.MacroExpansionInstances.begin(), |
| 1195 | EMT = MacroExpTracker.MacroExpansionInstances.end(); |
| 1196 | IMT != EMT; ++IMT) { |
| 1197 | MacroExpansionInstance &MacroInfo = *IMT; |
| 1198 | OS << " '" << *MacroExpTracker.MacroUnexpanded << "' expanded to: '" |
| 1199 | << *MacroInfo.MacroExpanded |
| 1200 | << "' with respect to these inclusion paths:\n"; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1201 | // Walk all the inclusion path hierarchies. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1202 | for (std::vector<InclusionPathHandle>::iterator |
| 1203 | IIP = MacroInfo.InclusionPathHandles.begin(), |
| 1204 | EIP = MacroInfo.InclusionPathHandles.end(); |
| 1205 | IIP != EIP; ++IIP) { |
| 1206 | const std::vector<HeaderHandle> &ip = getInclusionPath(*IIP); |
| 1207 | int Count = (int)ip.size(); |
| 1208 | for (int Index = 0; Index < Count; ++Index) { |
| 1209 | HeaderHandle H = ip[Index]; |
| 1210 | OS << std::string((Index * 2) + 4, ' ') << *getHeaderFilePath(H) |
| 1211 | << "\n"; |
| 1212 | } |
| 1213 | } |
| 1214 | // For a macro that wasn't defined, we flag it by using the |
| 1215 | // instance location. |
| 1216 | // If there is a definition... |
| 1217 | if (MacroInfo.DefinitionLocation.Line != ItemKey.Line) { |
| 1218 | OS << *MacroInfo.DefinitionSourceLine; |
| 1219 | if (MacroInfo.DefinitionLocation.Column > 0) |
| 1220 | OS << std::string(MacroInfo.DefinitionLocation.Column - 1, ' ') |
| 1221 | << "^\n"; |
| 1222 | OS << "Macro defined here.\n"; |
| 1223 | } else |
| 1224 | OS << "(no macro definition)" |
| 1225 | << "\n"; |
| 1226 | } |
| 1227 | } |
| 1228 | return ReturnValue; |
| 1229 | } |
| 1230 | |
| 1231 | // Report on inconsistent conditional instances. |
| 1232 | // Returns true if any mismatches. |
| 1233 | bool reportInconsistentConditionals(llvm::raw_ostream &OS) { |
| 1234 | bool ReturnValue = false; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1235 | // Walk all the conditional trackers in the map. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1236 | for (ConditionalExpansionMapIter I = ConditionalExpansions.begin(), |
| 1237 | E = ConditionalExpansions.end(); |
| 1238 | I != E; ++I) { |
| 1239 | const PPItemKey &ItemKey = I->first; |
| 1240 | ConditionalTracker &CondTracker = I->second; |
| 1241 | if (!CondTracker.hasMismatch()) |
| 1242 | continue; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1243 | // Tell caller we found one or more errors. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1244 | ReturnValue = true; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1245 | // Start the error message. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1246 | OS << *HeaderPaths[ItemKey.File] << ":" << ItemKey.Line << ":" |
| 1247 | << ItemKey.Column << "\n"; |
| 1248 | OS << "#" << getDirectiveSpelling(CondTracker.DirectiveKind) << " " |
| 1249 | << *CondTracker.ConditionUnexpanded << "\n"; |
| 1250 | OS << "^\n"; |
| 1251 | OS << "error: Conditional expression instance '" |
| 1252 | << *CondTracker.ConditionUnexpanded |
| 1253 | << "' has different values in this header, depending on how it was " |
| 1254 | "included.\n"; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1255 | // Walk all the instances. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1256 | for (std::vector<ConditionalExpansionInstance>::iterator |
| 1257 | IMT = CondTracker.ConditionalExpansionInstances.begin(), |
| 1258 | EMT = CondTracker.ConditionalExpansionInstances.end(); |
| 1259 | IMT != EMT; ++IMT) { |
| 1260 | ConditionalExpansionInstance &MacroInfo = *IMT; |
| 1261 | OS << " '" << *CondTracker.ConditionUnexpanded << "' expanded to: '" |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 1262 | << ConditionValueKindStrings[MacroInfo.ConditionValue] |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1263 | << "' with respect to these inclusion paths:\n"; |
John Thompson | 7c6e79f3 | 2013-07-29 19:07:00 +0000 | [diff] [blame] | 1264 | // Walk all the inclusion path hierarchies. |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1265 | for (std::vector<InclusionPathHandle>::iterator |
| 1266 | IIP = MacroInfo.InclusionPathHandles.begin(), |
| 1267 | EIP = MacroInfo.InclusionPathHandles.end(); |
| 1268 | IIP != EIP; ++IIP) { |
| 1269 | const std::vector<HeaderHandle> &ip = getInclusionPath(*IIP); |
| 1270 | int Count = (int)ip.size(); |
| 1271 | for (int Index = 0; Index < Count; ++Index) { |
| 1272 | HeaderHandle H = ip[Index]; |
| 1273 | OS << std::string((Index * 2) + 4, ' ') << *getHeaderFilePath(H) |
| 1274 | << "\n"; |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | return ReturnValue; |
| 1280 | } |
| 1281 | |
| 1282 | // Get directive spelling. |
| 1283 | static const char *getDirectiveSpelling(clang::tok::PPKeywordKind kind) { |
| 1284 | switch (kind) { |
| 1285 | case clang::tok::pp_if: |
| 1286 | return "if"; |
| 1287 | case clang::tok::pp_elif: |
| 1288 | return "elif"; |
| 1289 | case clang::tok::pp_ifdef: |
| 1290 | return "ifdef"; |
| 1291 | case clang::tok::pp_ifndef: |
| 1292 | return "ifndef"; |
| 1293 | default: |
| 1294 | return "(unknown)"; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | private: |
| 1299 | llvm::StringPool Strings; |
| 1300 | std::vector<StringHandle> HeaderPaths; |
| 1301 | std::vector<HeaderHandle> HeaderStack; |
| 1302 | std::vector<HeaderInclusionPath> InclusionPaths; |
| 1303 | InclusionPathHandle CurrentInclusionPathHandle; |
John Thompson | 4ed963a | 2013-08-07 18:49:47 +0000 | [diff] [blame] | 1304 | llvm::SmallSet<HeaderHandle, 128> HeadersInThisCompile; |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 1305 | std::vector<PPItemKey> IncludeDirectives; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1306 | MacroExpansionMap MacroExpansions; |
| 1307 | ConditionalExpansionMap ConditionalExpansions; |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 1308 | bool InNestedHeader; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1309 | }; |
| 1310 | |
| 1311 | // PreprocessorTracker functions. |
| 1312 | |
| 1313 | // PreprocessorTracker desctructor. |
| 1314 | PreprocessorTracker::~PreprocessorTracker() {} |
| 1315 | |
| 1316 | // Create instance of PreprocessorTracker. |
| 1317 | PreprocessorTracker *PreprocessorTracker::create() { |
| 1318 | return new PreprocessorTrackerImpl(); |
| 1319 | } |
| 1320 | |
| 1321 | // Preprocessor callbacks for modularize. |
| 1322 | |
John Thompson | 7408392 | 2013-09-18 18:19:43 +0000 | [diff] [blame] | 1323 | // Handle include directive. |
| 1324 | void PreprocessorCallbacks::InclusionDirective( |
| 1325 | clang::SourceLocation HashLoc, const clang::Token &IncludeTok, |
| 1326 | llvm::StringRef FileName, bool IsAngled, |
| 1327 | clang::CharSourceRange FilenameRange, const clang::FileEntry *File, |
| 1328 | llvm::StringRef SearchPath, llvm::StringRef RelativePath, |
| 1329 | const clang::Module *Imported) { |
| 1330 | int DirectiveLine, DirectiveColumn; |
| 1331 | std::string HeaderPath = getSourceLocationFile(PP, HashLoc); |
| 1332 | getSourceLocationLineAndColumn(PP, HashLoc, DirectiveLine, DirectiveColumn); |
| 1333 | PPTracker.handleIncludeDirective(HeaderPath, DirectiveLine, DirectiveColumn, |
| 1334 | FileName); |
| 1335 | } |
| 1336 | |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1337 | // Handle file entry/exit. |
| 1338 | void PreprocessorCallbacks::FileChanged( |
| 1339 | clang::SourceLocation Loc, clang::PPCallbacks::FileChangeReason Reason, |
| 1340 | clang::SrcMgr::CharacteristicKind FileType, clang::FileID PrevFID) { |
| 1341 | switch (Reason) { |
| 1342 | case EnterFile: |
| 1343 | PPTracker.handleHeaderEntry(PP, getSourceLocationFile(PP, Loc)); |
| 1344 | break; |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 1345 | case ExitFile: { |
| 1346 | const clang::FileEntry *F = |
John Thompson | 48df096 | 2013-08-05 23:55:14 +0000 | [diff] [blame] | 1347 | PP.getSourceManager().getFileEntryForID(PrevFID); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 1348 | if (F) |
John Thompson | cc2e291 | 2013-09-03 18:44:11 +0000 | [diff] [blame] | 1349 | PPTracker.handleHeaderExit(F->getName()); |
| 1350 | } break; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1351 | case SystemHeaderPragma: |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1352 | case RenameFile: |
Benjamin Kramer | f257681 | 2013-07-27 15:57:46 +0000 | [diff] [blame] | 1353 | break; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | // Handle macro expansion. |
| 1358 | void PreprocessorCallbacks::MacroExpands(const clang::Token &MacroNameTok, |
| 1359 | const clang::MacroDirective *MD, |
| 1360 | clang::SourceRange Range, |
| 1361 | const clang::MacroArgs *Args) { |
| 1362 | clang::SourceLocation Loc = Range.getBegin(); |
John Thompson | 91555bd | 2013-08-09 00:22:20 +0000 | [diff] [blame] | 1363 | // Ignore macro argument expansions. |
| 1364 | if (!Loc.isFileID()) |
| 1365 | return; |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1366 | clang::IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 1367 | const clang::MacroInfo *MI = PP.getMacroInfo(II); |
| 1368 | std::string MacroName = II->getName().str(); |
| 1369 | std::string Unexpanded(getMacroUnexpandedString(Range, PP, MacroName, MI)); |
| 1370 | std::string Expanded(getMacroExpandedString(PP, MacroName, MI, Args)); |
| 1371 | PPTracker.addMacroExpansionInstance( |
| 1372 | PP, PPTracker.getCurrentHeaderHandle(), Loc, MI->getDefinitionLoc(), II, |
| 1373 | Unexpanded, Expanded, PPTracker.getCurrentInclusionPathHandle()); |
| 1374 | } |
| 1375 | |
| 1376 | void PreprocessorCallbacks::Defined(const clang::Token &MacroNameTok, |
| 1377 | const clang::MacroDirective *MD, |
| 1378 | clang::SourceRange Range) { |
| 1379 | clang::SourceLocation Loc(Range.getBegin()); |
| 1380 | clang::IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 1381 | const clang::MacroInfo *MI = PP.getMacroInfo(II); |
| 1382 | std::string MacroName = II->getName().str(); |
| 1383 | std::string Unexpanded(getSourceString(PP, Range)); |
| 1384 | PPTracker.addMacroExpansionInstance( |
| 1385 | PP, PPTracker.getCurrentHeaderHandle(), Loc, |
| 1386 | (MI ? MI->getDefinitionLoc() : Loc), II, Unexpanded, |
| 1387 | (MI ? "true" : "false"), PPTracker.getCurrentInclusionPathHandle()); |
| 1388 | } |
| 1389 | |
| 1390 | void PreprocessorCallbacks::If(clang::SourceLocation Loc, |
| 1391 | clang::SourceRange ConditionRange, |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 1392 | clang::PPCallbacks::ConditionValueKind ConditionResult) { |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1393 | std::string Unexpanded(getSourceString(PP, ConditionRange)); |
| 1394 | PPTracker.addConditionalExpansionInstance( |
| 1395 | PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_if, |
| 1396 | ConditionResult, Unexpanded, PPTracker.getCurrentInclusionPathHandle()); |
| 1397 | } |
| 1398 | |
| 1399 | void PreprocessorCallbacks::Elif(clang::SourceLocation Loc, |
| 1400 | clang::SourceRange ConditionRange, |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 1401 | clang::PPCallbacks::ConditionValueKind ConditionResult, |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1402 | clang::SourceLocation IfLoc) { |
| 1403 | std::string Unexpanded(getSourceString(PP, ConditionRange)); |
| 1404 | PPTracker.addConditionalExpansionInstance( |
| 1405 | PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_elif, |
| 1406 | ConditionResult, Unexpanded, PPTracker.getCurrentInclusionPathHandle()); |
| 1407 | } |
| 1408 | |
| 1409 | void PreprocessorCallbacks::Ifdef(clang::SourceLocation Loc, |
| 1410 | const clang::Token &MacroNameTok, |
| 1411 | const clang::MacroDirective *MD) { |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 1412 | clang::PPCallbacks::ConditionValueKind IsDefined = |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 1413 | (MD ? clang::PPCallbacks::CVK_True : clang::PPCallbacks::CVK_False ); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1414 | PPTracker.addConditionalExpansionInstance( |
| 1415 | PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_ifdef, |
| 1416 | IsDefined, PP.getSpelling(MacroNameTok), |
| 1417 | PPTracker.getCurrentInclusionPathHandle()); |
| 1418 | } |
| 1419 | |
| 1420 | void PreprocessorCallbacks::Ifndef(clang::SourceLocation Loc, |
| 1421 | const clang::Token &MacroNameTok, |
| 1422 | const clang::MacroDirective *MD) { |
John Thompson | 87f9fef | 2013-12-07 08:41:15 +0000 | [diff] [blame] | 1423 | clang::PPCallbacks::ConditionValueKind IsNotDefined = |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 1424 | (!MD ? clang::PPCallbacks::CVK_True : clang::PPCallbacks::CVK_False ); |
John Thompson | 94faa4d | 2013-07-26 23:56:42 +0000 | [diff] [blame] | 1425 | PPTracker.addConditionalExpansionInstance( |
| 1426 | PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_ifndef, |
| 1427 | IsNotDefined, PP.getSpelling(MacroNameTok), |
| 1428 | PPTracker.getCurrentInclusionPathHandle()); |
| 1429 | } |
| 1430 | } // end namespace Modularize |