Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1 | //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the top level handling of macro expasion for the |
| 11 | // preprocessor. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "MacroArgs.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 20 | #include "clang/Lex/CodeCompletionHandler.h" |
| 21 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Lex/LexDiagnostic.h" |
| 23 | #include "clang/Lex/MacroInfo.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 27 | #include "llvm/Config/llvm-config.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Format.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 31 | #include <cstdio> |
| 32 | #include <ctime> |
| 33 | using namespace clang; |
| 34 | |
Dmitri Gribenko | b395847 | 2013-01-14 00:36:42 +0000 | [diff] [blame] | 35 | MacroInfo *Preprocessor::getMacroInfoHistory(const IdentifierInfo *II) const { |
Alexander Kornienko | 4d7e0ce | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 36 | assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 37 | |
| 38 | macro_iterator Pos = Macros.find(II); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 39 | assert(Pos != Macros.end() && "Identifier macro info is missing!"); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 40 | return Pos->second; |
| 41 | } |
| 42 | |
| 43 | /// setMacroInfo - Specify a macro for this identifier. |
| 44 | /// |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 45 | void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) { |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 46 | assert(MI && "MacroInfo should be non-zero!"); |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 47 | assert(MI->getUndefLoc().isInvalid() && |
| 48 | "Undefined macros cannot be registered"); |
| 49 | |
| 50 | MacroInfo *&StoredMI = Macros[II]; |
| 51 | MI->setPreviousDefinition(StoredMI); |
| 52 | StoredMI = MI; |
Alexander Kornienko | 4d7e0ce | 2012-09-25 17:18:14 +0000 | [diff] [blame] | 53 | II->setHasMacroDefinition(MI->getUndefLoc().isInvalid()); |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 54 | if (II->isFromAST()) |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 55 | II->setChangedSinceDeserialization(); |
| 56 | } |
| 57 | |
Douglas Gregor | 3ab50fe | 2012-10-11 17:41:54 +0000 | [diff] [blame] | 58 | void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI, |
| 59 | MacroInfo *Hint) { |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 60 | assert(MI && "Missing macro?"); |
| 61 | assert(MI->isFromAST() && "Macro is not from an AST?"); |
| 62 | assert(!MI->getPreviousDefinition() && "Macro already in chain?"); |
| 63 | |
| 64 | MacroInfo *&StoredMI = Macros[II]; |
| 65 | |
| 66 | // Easy case: this is the first macro definition for this macro. |
| 67 | if (!StoredMI) { |
| 68 | StoredMI = MI; |
| 69 | |
| 70 | if (MI->isDefined()) |
| 71 | II->setHasMacroDefinition(true); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // If this macro is a definition and this identifier has been neither |
| 76 | // defined nor undef'd in the current translation unit, add this macro |
| 77 | // to the end of the chain of definitions. |
| 78 | if (MI->isDefined() && StoredMI->isFromAST()) { |
| 79 | // Simple case: if this is the first actual definition, just put it at |
| 80 | // th beginning. |
| 81 | if (!StoredMI->isDefined()) { |
| 82 | MI->setPreviousDefinition(StoredMI); |
| 83 | StoredMI = MI; |
| 84 | |
| 85 | II->setHasMacroDefinition(true); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // Find the end of the definition chain. |
Douglas Gregor | 54c8a40 | 2012-10-12 00:16:50 +0000 | [diff] [blame] | 90 | MacroInfo *Prev; |
| 91 | MacroInfo *PrevPrev = StoredMI; |
Douglas Gregor | e8219a6 | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 92 | bool Ambiguous = StoredMI->isAmbiguous(); |
| 93 | bool MatchedOther = false; |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 94 | do { |
Douglas Gregor | 54c8a40 | 2012-10-12 00:16:50 +0000 | [diff] [blame] | 95 | Prev = PrevPrev; |
| 96 | |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 97 | // If the macros are not identical, we have an ambiguity. |
Douglas Gregor | e8219a6 | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 98 | if (!Prev->isIdenticalTo(*MI, *this)) { |
| 99 | if (!Ambiguous) { |
| 100 | Ambiguous = true; |
| 101 | StoredMI->setAmbiguous(true); |
| 102 | } |
| 103 | } else { |
| 104 | MatchedOther = true; |
| 105 | } |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 106 | } while ((PrevPrev = Prev->getPreviousDefinition()) && |
| 107 | PrevPrev->isDefined()); |
| 108 | |
Douglas Gregor | e8219a6 | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 109 | // If there are ambiguous definitions, and we didn't match any other |
| 110 | // definition, then mark us as ambiguous. |
| 111 | if (Ambiguous && !MatchedOther) |
| 112 | MI->setAmbiguous(true); |
Douglas Gregor | 7097be9 | 2012-10-11 00:48:48 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 114 | // Wire this macro information into the chain. |
| 115 | MI->setPreviousDefinition(Prev->getPreviousDefinition()); |
| 116 | Prev->setPreviousDefinition(MI); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // The macro is not a definition; put it at the end of the list. |
Douglas Gregor | 3ab50fe | 2012-10-11 17:41:54 +0000 | [diff] [blame] | 121 | MacroInfo *Prev = Hint? Hint : StoredMI; |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 122 | while (Prev->getPreviousDefinition()) |
| 123 | Prev = Prev->getPreviousDefinition(); |
| 124 | Prev->setPreviousDefinition(MI); |
| 125 | } |
| 126 | |
| 127 | void Preprocessor::makeLoadedMacroInfoVisible(IdentifierInfo *II, |
| 128 | MacroInfo *MI) { |
| 129 | assert(MI->isFromAST() && "Macro must be from the AST"); |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 130 | |
| 131 | MacroInfo *&StoredMI = Macros[II]; |
| 132 | if (StoredMI == MI) { |
| 133 | // Easy case: this is the first macro anyway. |
Douglas Gregor | 54c8a40 | 2012-10-12 00:16:50 +0000 | [diff] [blame] | 134 | II->setHasMacroDefinition(MI->isDefined()); |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
| 138 | // Go find the macro and pull it out of the list. |
Douglas Gregor | 54c8a40 | 2012-10-12 00:16:50 +0000 | [diff] [blame] | 139 | // FIXME: Yes, this is O(N), and making a pile of macros visible or hidden |
| 140 | // would be quadratic, but it's extremely rare. |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 141 | MacroInfo *Prev = StoredMI; |
| 142 | while (Prev->getPreviousDefinition() != MI) |
| 143 | Prev = Prev->getPreviousDefinition(); |
| 144 | Prev->setPreviousDefinition(MI->getPreviousDefinition()); |
Douglas Gregor | 54c8a40 | 2012-10-12 00:16:50 +0000 | [diff] [blame] | 145 | MI->setPreviousDefinition(0); |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 146 | |
| 147 | // Add the macro back to the list. |
| 148 | addLoadedMacroInfo(II, MI); |
Douglas Gregor | 54c8a40 | 2012-10-12 00:16:50 +0000 | [diff] [blame] | 149 | |
| 150 | II->setHasMacroDefinition(StoredMI->isDefined()); |
| 151 | if (II->isFromAST()) |
| 152 | II->setChangedSinceDeserialization(); |
Douglas Gregor | 6c6c54a | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 155 | /// \brief Undefine a macro for this identifier. |
| 156 | void Preprocessor::clearMacroInfo(IdentifierInfo *II) { |
| 157 | assert(II->hasMacroDefinition() && "Macro is not defined!"); |
| 158 | assert(Macros[II]->getUndefLoc().isValid() && "Macro is still defined!"); |
| 159 | II->setHasMacroDefinition(false); |
| 160 | if (II->isFromAST()) |
| 161 | II->setChangedSinceDeserialization(); |
| 162 | } |
| 163 | |
| 164 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 165 | /// table and mark it as a builtin macro to be expanded. |
| 166 | static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ |
| 167 | // Get the identifier. |
| 168 | IdentifierInfo *Id = PP.getIdentifierInfo(Name); |
| 169 | |
| 170 | // Mark it as being a macro that is builtin. |
| 171 | MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); |
| 172 | MI->setIsBuiltinMacro(); |
| 173 | PP.setMacroInfo(Id, MI); |
| 174 | return Id; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 179 | /// identifier table. |
| 180 | void Preprocessor::RegisterBuiltinMacros() { |
| 181 | Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); |
| 182 | Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); |
| 183 | Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); |
| 184 | Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); |
| 185 | Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); |
| 186 | Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); |
| 187 | |
| 188 | // GCC Extensions. |
| 189 | Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); |
| 190 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); |
| 191 | Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); |
| 192 | |
| 193 | // Clang Extensions. |
| 194 | Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); |
| 195 | Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); |
| 196 | Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); |
| 197 | Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); |
| 198 | Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); |
| 199 | Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); |
| 200 | Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); |
| 201 | |
Douglas Gregor | b09de51 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 202 | // Modules. |
| 203 | if (LangOpts.Modules) { |
| 204 | Ident__building_module = RegisterBuiltinMacro(*this, "__building_module"); |
| 205 | |
| 206 | // __MODULE__ |
| 207 | if (!LangOpts.CurrentModule.empty()) |
| 208 | Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__"); |
| 209 | else |
| 210 | Ident__MODULE__ = 0; |
| 211 | } else { |
| 212 | Ident__building_module = 0; |
| 213 | Ident__MODULE__ = 0; |
| 214 | } |
| 215 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 216 | // Microsoft Extensions. |
| 217 | if (LangOpts.MicrosoftExt) |
| 218 | Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); |
| 219 | else |
| 220 | Ident__pragma = 0; |
| 221 | } |
| 222 | |
| 223 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 224 | /// in its expansion, currently expands to that token literally. |
| 225 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
| 226 | const IdentifierInfo *MacroIdent, |
| 227 | Preprocessor &PP) { |
| 228 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 229 | |
| 230 | // If the token isn't an identifier, it's always literally expanded. |
| 231 | if (II == 0) return true; |
| 232 | |
| 233 | // If the information about this identifier is out of date, update it from |
| 234 | // the external source. |
| 235 | if (II->isOutOfDate()) |
| 236 | PP.getExternalSource()->updateOutOfDateIdentifier(*II); |
| 237 | |
| 238 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 239 | // expanded so it's not a trivial expansion. |
| 240 | if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && |
| 241 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 242 | II != MacroIdent) |
| 243 | return false; |
| 244 | |
| 245 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 246 | // it. |
| 247 | if (MI->isObjectLike()) return true; |
| 248 | |
| 249 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 250 | // as long as the identifier is not a macro argument. |
| 251 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 252 | I != E; ++I) |
| 253 | if (*I == II) |
| 254 | return false; // Identifier is a macro argument. |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 261 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 262 | /// method should have no observable side-effect on the lexed tokens. |
| 263 | bool Preprocessor::isNextPPTokenLParen() { |
| 264 | // Do some quick tests for rejection cases. |
| 265 | unsigned Val; |
| 266 | if (CurLexer) |
| 267 | Val = CurLexer->isNextPPTokenLParen(); |
| 268 | else if (CurPTHLexer) |
| 269 | Val = CurPTHLexer->isNextPPTokenLParen(); |
| 270 | else |
| 271 | Val = CurTokenLexer->isNextTokenLParen(); |
| 272 | |
| 273 | if (Val == 2) { |
| 274 | // We have run off the end. If it's a source file we don't |
| 275 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 276 | // macro stack. |
| 277 | if (CurPPLexer) |
| 278 | return false; |
| 279 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 280 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 281 | if (Entry.TheLexer) |
| 282 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
| 283 | else if (Entry.ThePTHLexer) |
| 284 | Val = Entry.ThePTHLexer->isNextPPTokenLParen(); |
| 285 | else |
| 286 | Val = Entry.TheTokenLexer->isNextTokenLParen(); |
| 287 | |
| 288 | if (Val != 2) |
| 289 | break; |
| 290 | |
| 291 | // Ran off the end of a source file? |
| 292 | if (Entry.ThePPLexer) |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 298 | // have found something that isn't a '(' or we found the end of the |
| 299 | // translation unit. In either case, return false. |
| 300 | return Val == 1; |
| 301 | } |
| 302 | |
| 303 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 304 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 305 | bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, |
| 306 | MacroInfo *MI) { |
| 307 | // If this is a macro expansion in the "#if !defined(x)" line for the file, |
| 308 | // then the macro could expand to different things in other contexts, we need |
| 309 | // to disable the optimization in this case. |
| 310 | if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); |
| 311 | |
| 312 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 313 | if (MI->isBuiltinMacro()) { |
| 314 | if (Callbacks) Callbacks->MacroExpands(Identifier, MI, |
| 315 | Identifier.getLocation()); |
| 316 | ExpandBuiltinMacro(Identifier); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | /// Args - If this is a function-like macro expansion, this contains, |
| 321 | /// for each macro argument, the list of tokens that were provided to the |
| 322 | /// invocation. |
| 323 | MacroArgs *Args = 0; |
| 324 | |
| 325 | // Remember where the end of the expansion occurred. For an object-like |
| 326 | // macro, this is the identifier. For a function-like macro, this is the ')'. |
| 327 | SourceLocation ExpansionEnd = Identifier.getLocation(); |
| 328 | |
| 329 | // If this is a function-like macro, read the arguments. |
| 330 | if (MI->isFunctionLike()) { |
| 331 | // C99 6.10.3p10: If the preprocessing token immediately after the macro |
| 332 | // name isn't a '(', this macro should not be expanded. |
| 333 | if (!isNextPPTokenLParen()) |
| 334 | return true; |
| 335 | |
| 336 | // Remember that we are now parsing the arguments to a macro invocation. |
| 337 | // Preprocessor directives used inside macro arguments are not portable, and |
| 338 | // this enables the warning. |
| 339 | InMacroArgs = true; |
| 340 | Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); |
| 341 | |
| 342 | // Finished parsing args. |
| 343 | InMacroArgs = false; |
| 344 | |
| 345 | // If there was an error parsing the arguments, bail out. |
| 346 | if (Args == 0) return false; |
| 347 | |
| 348 | ++NumFnMacroExpanded; |
| 349 | } else { |
| 350 | ++NumMacroExpanded; |
| 351 | } |
| 352 | |
| 353 | // Notice that this macro has been used. |
| 354 | markMacroAsUsed(MI); |
| 355 | |
| 356 | // Remember where the token is expanded. |
| 357 | SourceLocation ExpandLoc = Identifier.getLocation(); |
| 358 | SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); |
| 359 | |
| 360 | if (Callbacks) { |
| 361 | if (InMacroArgs) { |
| 362 | // We can have macro expansion inside a conditional directive while |
| 363 | // reading the function macro arguments. To ensure, in that case, that |
| 364 | // MacroExpands callbacks still happen in source order, queue this |
| 365 | // callback to have it happen after the function macro callback. |
| 366 | DelayedMacroExpandsCallbacks.push_back( |
| 367 | MacroExpandsInfo(Identifier, MI, ExpansionRange)); |
| 368 | } else { |
| 369 | Callbacks->MacroExpands(Identifier, MI, ExpansionRange); |
| 370 | if (!DelayedMacroExpandsCallbacks.empty()) { |
| 371 | for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { |
| 372 | MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; |
| 373 | Callbacks->MacroExpands(Info.Tok, Info.MI, Info.Range); |
| 374 | } |
| 375 | DelayedMacroExpandsCallbacks.clear(); |
| 376 | } |
| 377 | } |
| 378 | } |
Douglas Gregor | e8219a6 | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 379 | |
| 380 | // If the macro definition is ambiguous, complain. |
| 381 | if (MI->isAmbiguous()) { |
| 382 | Diag(Identifier, diag::warn_pp_ambiguous_macro) |
| 383 | << Identifier.getIdentifierInfo(); |
| 384 | Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) |
| 385 | << Identifier.getIdentifierInfo(); |
| 386 | for (MacroInfo *PrevMI = MI->getPreviousDefinition(); |
| 387 | PrevMI && PrevMI->isDefined(); |
| 388 | PrevMI = PrevMI->getPreviousDefinition()) { |
| 389 | if (PrevMI->isAmbiguous()) { |
| 390 | Diag(PrevMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other) |
| 391 | << Identifier.getIdentifierInfo(); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 396 | // If we started lexing a macro, enter the macro expansion body. |
| 397 | |
| 398 | // If this macro expands to no tokens, don't bother to push it onto the |
| 399 | // expansion stack, only to take it right back off. |
| 400 | if (MI->getNumTokens() == 0) { |
| 401 | // No need for arg info. |
| 402 | if (Args) Args->destroy(*this); |
| 403 | |
| 404 | // Ignore this macro use, just return the next token in the current |
| 405 | // buffer. |
| 406 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 407 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 408 | |
| 409 | Lex(Identifier); |
| 410 | |
| 411 | // If the identifier isn't on some OTHER line, inherit the leading |
| 412 | // whitespace/first-on-a-line property of this token. This handles |
| 413 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 414 | // empty. |
| 415 | if (!Identifier.isAtStartOfLine()) { |
| 416 | if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine); |
| 417 | if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace); |
| 418 | } |
| 419 | Identifier.setFlag(Token::LeadingEmptyMacro); |
| 420 | ++NumFastMacroExpanded; |
| 421 | return false; |
| 422 | |
| 423 | } else if (MI->getNumTokens() == 1 && |
| 424 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), |
| 425 | *this)) { |
| 426 | // Otherwise, if this macro expands into a single trivially-expanded |
| 427 | // token: expand it now. This handles common cases like |
| 428 | // "#define VAL 42". |
| 429 | |
| 430 | // No need for arg info. |
| 431 | if (Args) Args->destroy(*this); |
| 432 | |
| 433 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 434 | // identifier to the expanded token. |
| 435 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 436 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 437 | |
| 438 | // Replace the result token. |
| 439 | Identifier = MI->getReplacementToken(0); |
| 440 | |
| 441 | // Restore the StartOfLine/LeadingSpace markers. |
| 442 | Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); |
| 443 | Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); |
| 444 | |
| 445 | // Update the tokens location to include both its expansion and physical |
| 446 | // locations. |
| 447 | SourceLocation Loc = |
| 448 | SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, |
| 449 | ExpansionEnd,Identifier.getLength()); |
| 450 | Identifier.setLocation(Loc); |
| 451 | |
| 452 | // If this is a disabled macro or #define X X, we must mark the result as |
| 453 | // unexpandable. |
| 454 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { |
| 455 | if (MacroInfo *NewMI = getMacroInfo(NewII)) |
| 456 | if (!NewMI->isEnabled() || NewMI == MI) { |
| 457 | Identifier.setFlag(Token::DisableExpand); |
| 458 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Since this is not an identifier token, it can't be macro expanded, so |
| 463 | // we're done. |
| 464 | ++NumFastMacroExpanded; |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | // Start expanding the macro. |
| 469 | EnterMacro(Identifier, ExpansionEnd, MI, Args); |
| 470 | |
| 471 | // Now that the macro is at the top of the include stack, ask the |
| 472 | // preprocessor to read the next token from it. |
| 473 | Lex(Identifier); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next |
| 478 | /// token is the '(' of the macro, this method is invoked to read all of the |
| 479 | /// actual arguments specified for the macro invocation. This returns null on |
| 480 | /// error. |
| 481 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, |
| 482 | MacroInfo *MI, |
| 483 | SourceLocation &MacroEnd) { |
| 484 | // The number of fixed arguments to parse. |
| 485 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 486 | bool isVariadic = MI->isVariadic(); |
| 487 | |
| 488 | // Outer loop, while there are more arguments, keep reading them. |
| 489 | Token Tok; |
| 490 | |
| 491 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 492 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 493 | LexUnexpandedToken(Tok); |
| 494 | assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); |
| 495 | |
| 496 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 497 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 498 | // heap allocations in the common case. |
| 499 | SmallVector<Token, 64> ArgTokens; |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 500 | bool ContainsCodeCompletionTok = false; |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 501 | |
| 502 | unsigned NumActuals = 0; |
| 503 | while (Tok.isNot(tok::r_paren)) { |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 504 | if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod))) |
| 505 | break; |
| 506 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 507 | assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && |
| 508 | "only expect argument separators here"); |
| 509 | |
| 510 | unsigned ArgTokenStart = ArgTokens.size(); |
| 511 | SourceLocation ArgStartLoc = Tok.getLocation(); |
| 512 | |
| 513 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 514 | // that we already consumed the first one. |
| 515 | unsigned NumParens = 0; |
| 516 | |
| 517 | while (1) { |
| 518 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 519 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 520 | LexUnexpandedToken(Tok); |
| 521 | |
| 522 | if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n" |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 523 | if (!ContainsCodeCompletionTok) { |
| 524 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 525 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 526 | << MacroName.getIdentifierInfo(); |
| 527 | // Do not lose the EOF/EOD. Return it to the client. |
| 528 | MacroName = Tok; |
| 529 | return 0; |
| 530 | } else { |
Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 531 | // Do not lose the EOF/EOD. |
| 532 | Token *Toks = new Token[1]; |
| 533 | Toks[0] = Tok; |
| 534 | EnterTokenStream(Toks, 1, true, true); |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 535 | break; |
| 536 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 537 | } else if (Tok.is(tok::r_paren)) { |
| 538 | // If we found the ) token, the macro arg list is done. |
| 539 | if (NumParens-- == 0) { |
| 540 | MacroEnd = Tok.getLocation(); |
| 541 | break; |
| 542 | } |
| 543 | } else if (Tok.is(tok::l_paren)) { |
| 544 | ++NumParens; |
Nico Weber | 93dec51 | 2012-09-26 08:19:01 +0000 | [diff] [blame] | 545 | } else if (Tok.is(tok::comma) && NumParens == 0) { |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 546 | // Comma ends this argument if there are more fixed arguments expected. |
| 547 | // However, if this is a variadic macro, and this is part of the |
| 548 | // variadic part, then the comma is just an argument token. |
| 549 | if (!isVariadic) break; |
| 550 | if (NumFixedArgsLeft > 1) |
| 551 | break; |
| 552 | } else if (Tok.is(tok::comment) && !KeepMacroComments) { |
| 553 | // If this is a comment token in the argument list and we're just in |
| 554 | // -C mode (not -CC mode), discard the comment. |
| 555 | continue; |
| 556 | } else if (Tok.getIdentifierInfo() != 0) { |
| 557 | // Reading macro arguments can cause macros that we are currently |
| 558 | // expanding from to be popped off the expansion stack. Doing so causes |
| 559 | // them to be reenabled for expansion. Here we record whether any |
| 560 | // identifiers we lex as macro arguments correspond to disabled macros. |
| 561 | // If so, we mark the token as noexpand. This is a subtle aspect of |
| 562 | // C99 6.10.3.4p2. |
| 563 | if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) |
| 564 | if (!MI->isEnabled()) |
| 565 | Tok.setFlag(Token::DisableExpand); |
| 566 | } else if (Tok.is(tok::code_completion)) { |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 567 | ContainsCodeCompletionTok = true; |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 568 | if (CodeComplete) |
| 569 | CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), |
| 570 | MI, NumActuals); |
| 571 | // Don't mark that we reached the code-completion point because the |
| 572 | // parser is going to handle the token and there will be another |
| 573 | // code-completion callback. |
| 574 | } |
| 575 | |
| 576 | ArgTokens.push_back(Tok); |
| 577 | } |
| 578 | |
| 579 | // If this was an empty argument list foo(), don't add this as an empty |
| 580 | // argument. |
| 581 | if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) |
| 582 | break; |
| 583 | |
| 584 | // If this is not a variadic macro, and too many args were specified, emit |
| 585 | // an error. |
| 586 | if (!isVariadic && NumFixedArgsLeft == 0) { |
| 587 | if (ArgTokens.size() != ArgTokenStart) |
| 588 | ArgStartLoc = ArgTokens[ArgTokenStart].getLocation(); |
| 589 | |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 590 | if (!ContainsCodeCompletionTok) { |
| 591 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 592 | // Emitting it at the , could be far away from the macro name. |
| 593 | Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc); |
| 594 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 595 | << MacroName.getIdentifierInfo(); |
| 596 | return 0; |
| 597 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // Empty arguments are standard in C99 and C++0x, and are supported as an extension in |
| 601 | // other modes. |
| 602 | if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 603 | Diag(Tok, LangOpts.CPlusPlus11 ? |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 604 | diag::warn_cxx98_compat_empty_fnmacro_arg : |
| 605 | diag::ext_empty_fnmacro_arg); |
| 606 | |
| 607 | // Add a marker EOF token to the end of the token list for this argument. |
| 608 | Token EOFTok; |
| 609 | EOFTok.startToken(); |
| 610 | EOFTok.setKind(tok::eof); |
| 611 | EOFTok.setLocation(Tok.getLocation()); |
| 612 | EOFTok.setLength(0); |
| 613 | ArgTokens.push_back(EOFTok); |
| 614 | ++NumActuals; |
| 615 | assert(NumFixedArgsLeft != 0 && "Too many arguments parsed"); |
| 616 | --NumFixedArgsLeft; |
| 617 | } |
| 618 | |
| 619 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 620 | // arguments. |
| 621 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 622 | |
| 623 | // See MacroArgs instance var for description of this. |
| 624 | bool isVarargsElided = false; |
| 625 | |
Argyrios Kyrtzidis | f1e5b15 | 2012-12-21 01:51:12 +0000 | [diff] [blame] | 626 | if (ContainsCodeCompletionTok) { |
| 627 | // Recover from not-fully-formed macro invocation during code-completion. |
| 628 | Token EOFTok; |
| 629 | EOFTok.startToken(); |
| 630 | EOFTok.setKind(tok::eof); |
| 631 | EOFTok.setLocation(Tok.getLocation()); |
| 632 | EOFTok.setLength(0); |
| 633 | for (; NumActuals < MinArgsExpected; ++NumActuals) |
| 634 | ArgTokens.push_back(EOFTok); |
| 635 | } |
| 636 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 637 | if (NumActuals < MinArgsExpected) { |
| 638 | // There are several cases where too few arguments is ok, handle them now. |
| 639 | if (NumActuals == 0 && MinArgsExpected == 1) { |
| 640 | // #define A(X) or #define A(...) ---> A() |
| 641 | |
| 642 | // If there is exactly one argument, and that argument is missing, |
| 643 | // then we have an empty "()" argument empty list. This is fine, even if |
| 644 | // the macro expects one argument (the argument is just empty). |
| 645 | isVarargsElided = MI->isVariadic(); |
| 646 | } else if (MI->isVariadic() && |
| 647 | (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) |
| 648 | (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() |
| 649 | // Varargs where the named vararg parameter is missing: OK as extension. |
| 650 | // #define A(x, ...) |
| 651 | // A("blah") |
Eli Friedman | 4fa4b48 | 2012-11-14 02:18:46 +0000 | [diff] [blame] | 652 | // |
| 653 | // If the macro contains the comma pasting extension, the diagnostic |
| 654 | // is suppressed; we know we'll get another diagnostic later. |
| 655 | if (!MI->hasCommaPasting()) { |
| 656 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 657 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 658 | << MacroName.getIdentifierInfo(); |
| 659 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 660 | |
| 661 | // Remember this occurred, allowing us to elide the comma when used for |
| 662 | // cases like: |
| 663 | // #define A(x, foo...) blah(a, ## foo) |
| 664 | // #define B(x, ...) blah(a, ## __VA_ARGS__) |
| 665 | // #define C(...) blah(a, ## __VA_ARGS__) |
| 666 | // A(x) B(x) C() |
| 667 | isVarargsElided = true; |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 668 | } else if (!ContainsCodeCompletionTok) { |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 669 | // Otherwise, emit the error. |
| 670 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
Argyrios Kyrtzidis | 0ee8de7 | 2012-12-14 18:53:47 +0000 | [diff] [blame] | 671 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 672 | << MacroName.getIdentifierInfo(); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | // Add a marker EOF token to the end of the token list for this argument. |
| 677 | SourceLocation EndLoc = Tok.getLocation(); |
| 678 | Tok.startToken(); |
| 679 | Tok.setKind(tok::eof); |
| 680 | Tok.setLocation(EndLoc); |
| 681 | Tok.setLength(0); |
| 682 | ArgTokens.push_back(Tok); |
| 683 | |
| 684 | // If we expect two arguments, add both as empty. |
| 685 | if (NumActuals == 0 && MinArgsExpected == 2) |
| 686 | ArgTokens.push_back(Tok); |
| 687 | |
Argyrios Kyrtzidis | cd0fd18 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 688 | } else if (NumActuals > MinArgsExpected && !MI->isVariadic() && |
| 689 | !ContainsCodeCompletionTok) { |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 690 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 691 | // Emitting it at the , could be far away from the macro name. |
| 692 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
Argyrios Kyrtzidis | 0ee8de7 | 2012-12-14 18:53:47 +0000 | [diff] [blame] | 693 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 694 | << MacroName.getIdentifierInfo(); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); |
| 699 | } |
| 700 | |
| 701 | /// \brief Keeps macro expanded tokens for TokenLexers. |
| 702 | // |
| 703 | /// Works like a stack; a TokenLexer adds the macro expanded tokens that is |
| 704 | /// going to lex in the cache and when it finishes the tokens are removed |
| 705 | /// from the end of the cache. |
| 706 | Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, |
| 707 | ArrayRef<Token> tokens) { |
| 708 | assert(tokLexer); |
| 709 | if (tokens.empty()) |
| 710 | return 0; |
| 711 | |
| 712 | size_t newIndex = MacroExpandedTokens.size(); |
| 713 | bool cacheNeedsToGrow = tokens.size() > |
| 714 | MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); |
| 715 | MacroExpandedTokens.append(tokens.begin(), tokens.end()); |
| 716 | |
| 717 | if (cacheNeedsToGrow) { |
| 718 | // Go through all the TokenLexers whose 'Tokens' pointer points in the |
| 719 | // buffer and update the pointers to the (potential) new buffer array. |
| 720 | for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { |
| 721 | TokenLexer *prevLexer; |
| 722 | size_t tokIndex; |
| 723 | llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; |
| 724 | prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); |
| 729 | return MacroExpandedTokens.data() + newIndex; |
| 730 | } |
| 731 | |
| 732 | void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { |
| 733 | assert(!MacroExpandingLexersStack.empty()); |
| 734 | size_t tokIndex = MacroExpandingLexersStack.back().second; |
| 735 | assert(tokIndex < MacroExpandedTokens.size()); |
| 736 | // Pop the cached macro expanded tokens from the end. |
| 737 | MacroExpandedTokens.resize(tokIndex); |
| 738 | MacroExpandingLexersStack.pop_back(); |
| 739 | } |
| 740 | |
| 741 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 742 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 743 | /// the identifier tokens inserted. |
| 744 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 745 | Preprocessor &PP) { |
| 746 | time_t TT = time(0); |
| 747 | struct tm *TM = localtime(&TT); |
| 748 | |
| 749 | static const char * const Months[] = { |
| 750 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 751 | }; |
| 752 | |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 753 | { |
| 754 | SmallString<32> TmpBuffer; |
| 755 | llvm::raw_svector_ostream TmpStream(TmpBuffer); |
| 756 | TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon], |
| 757 | TM->tm_mday, TM->tm_year + 1900); |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 758 | Token TmpTok; |
| 759 | TmpTok.startToken(); |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 760 | PP.CreateString(TmpStream.str(), TmpTok); |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 761 | DATELoc = TmpTok.getLocation(); |
| 762 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 763 | |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 764 | { |
| 765 | SmallString<32> TmpBuffer; |
| 766 | llvm::raw_svector_ostream TmpStream(TmpBuffer); |
| 767 | TmpStream << llvm::format("\"%02d:%02d:%02d\"", |
| 768 | TM->tm_hour, TM->tm_min, TM->tm_sec); |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 769 | Token TmpTok; |
| 770 | TmpTok.startToken(); |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 771 | PP.CreateString(TmpStream.str(), TmpTok); |
Dmitri Gribenko | 33d054b | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 772 | TIMELoc = TmpTok.getLocation(); |
| 773 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
| 777 | /// HasFeature - Return true if we recognize and implement the feature |
| 778 | /// specified by the identifier as a standard language feature. |
| 779 | static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { |
| 780 | const LangOptions &LangOpts = PP.getLangOpts(); |
| 781 | StringRef Feature = II->getName(); |
| 782 | |
| 783 | // Normalize the feature name, __foo__ becomes foo. |
| 784 | if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) |
| 785 | Feature = Feature.substr(2, Feature.size() - 4); |
| 786 | |
| 787 | return llvm::StringSwitch<bool>(Feature) |
Richard Smith | ca1b62a | 2012-11-05 21:48:12 +0000 | [diff] [blame] | 788 | .Case("address_sanitizer", LangOpts.SanitizeAddress) |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 789 | .Case("attribute_analyzer_noreturn", true) |
| 790 | .Case("attribute_availability", true) |
| 791 | .Case("attribute_availability_with_message", true) |
| 792 | .Case("attribute_cf_returns_not_retained", true) |
| 793 | .Case("attribute_cf_returns_retained", true) |
| 794 | .Case("attribute_deprecated_with_message", true) |
| 795 | .Case("attribute_ext_vector_type", true) |
| 796 | .Case("attribute_ns_returns_not_retained", true) |
| 797 | .Case("attribute_ns_returns_retained", true) |
| 798 | .Case("attribute_ns_consumes_self", true) |
| 799 | .Case("attribute_ns_consumed", true) |
| 800 | .Case("attribute_cf_consumed", true) |
| 801 | .Case("attribute_objc_ivar_unused", true) |
| 802 | .Case("attribute_objc_method_family", true) |
| 803 | .Case("attribute_overloadable", true) |
| 804 | .Case("attribute_unavailable_with_message", true) |
| 805 | .Case("attribute_unused_on_fields", true) |
| 806 | .Case("blocks", LangOpts.Blocks) |
| 807 | .Case("cxx_exceptions", LangOpts.Exceptions) |
| 808 | .Case("cxx_rtti", LangOpts.RTTI) |
| 809 | .Case("enumerator_attributes", true) |
Evgeniy Stepanov | 5c70ef4 | 2012-12-20 12:03:13 +0000 | [diff] [blame] | 810 | .Case("memory_sanitizer", LangOpts.SanitizeMemory) |
Dmitry Vyukov | 728e212 | 2012-12-17 08:52:05 +0000 | [diff] [blame] | 811 | .Case("thread_sanitizer", LangOpts.SanitizeThread) |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 812 | // Objective-C features |
| 813 | .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? |
| 814 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
| 815 | .Case("objc_arc_weak", LangOpts.ObjCARCWeak) |
| 816 | .Case("objc_default_synthesize_properties", LangOpts.ObjC2) |
| 817 | .Case("objc_fixed_enum", LangOpts.ObjC2) |
| 818 | .Case("objc_instancetype", LangOpts.ObjC2) |
| 819 | .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) |
| 820 | .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) |
Ted Kremenek | e4057c2 | 2013-01-04 19:04:44 +0000 | [diff] [blame] | 821 | .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword? |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 822 | .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) |
| 823 | .Case("ownership_holds", true) |
| 824 | .Case("ownership_returns", true) |
| 825 | .Case("ownership_takes", true) |
| 826 | .Case("objc_bool", true) |
| 827 | .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) |
| 828 | .Case("objc_array_literals", LangOpts.ObjC2) |
| 829 | .Case("objc_dictionary_literals", LangOpts.ObjC2) |
| 830 | .Case("objc_boxed_expressions", LangOpts.ObjC2) |
| 831 | .Case("arc_cf_code_audited", true) |
| 832 | // C11 features |
| 833 | .Case("c_alignas", LangOpts.C11) |
| 834 | .Case("c_atomic", LangOpts.C11) |
| 835 | .Case("c_generic_selections", LangOpts.C11) |
| 836 | .Case("c_static_assert", LangOpts.C11) |
| 837 | // C++11 features |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 838 | .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11) |
| 839 | .Case("cxx_alias_templates", LangOpts.CPlusPlus11) |
| 840 | .Case("cxx_alignas", LangOpts.CPlusPlus11) |
| 841 | .Case("cxx_atomic", LangOpts.CPlusPlus11) |
| 842 | .Case("cxx_attributes", LangOpts.CPlusPlus11) |
| 843 | .Case("cxx_auto_type", LangOpts.CPlusPlus11) |
| 844 | .Case("cxx_constexpr", LangOpts.CPlusPlus11) |
| 845 | .Case("cxx_decltype", LangOpts.CPlusPlus11) |
| 846 | .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11) |
| 847 | .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11) |
| 848 | .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11) |
| 849 | .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11) |
| 850 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus11) |
| 851 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11) |
| 852 | .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11) |
| 853 | .Case("cxx_implicit_moves", LangOpts.CPlusPlus11) |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 854 | //.Case("cxx_inheriting_constructors", false) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 855 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11) |
| 856 | .Case("cxx_lambdas", LangOpts.CPlusPlus11) |
| 857 | .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11) |
| 858 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11) |
| 859 | .Case("cxx_noexcept", LangOpts.CPlusPlus11) |
| 860 | .Case("cxx_nullptr", LangOpts.CPlusPlus11) |
| 861 | .Case("cxx_override_control", LangOpts.CPlusPlus11) |
| 862 | .Case("cxx_range_for", LangOpts.CPlusPlus11) |
| 863 | .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11) |
| 864 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11) |
| 865 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus11) |
| 866 | .Case("cxx_strong_enums", LangOpts.CPlusPlus11) |
| 867 | .Case("cxx_static_assert", LangOpts.CPlusPlus11) |
| 868 | .Case("cxx_trailing_return", LangOpts.CPlusPlus11) |
| 869 | .Case("cxx_unicode_literals", LangOpts.CPlusPlus11) |
| 870 | .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11) |
| 871 | .Case("cxx_user_literals", LangOpts.CPlusPlus11) |
| 872 | .Case("cxx_variadic_templates", LangOpts.CPlusPlus11) |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 873 | // Type traits |
| 874 | .Case("has_nothrow_assign", LangOpts.CPlusPlus) |
| 875 | .Case("has_nothrow_copy", LangOpts.CPlusPlus) |
| 876 | .Case("has_nothrow_constructor", LangOpts.CPlusPlus) |
| 877 | .Case("has_trivial_assign", LangOpts.CPlusPlus) |
| 878 | .Case("has_trivial_copy", LangOpts.CPlusPlus) |
| 879 | .Case("has_trivial_constructor", LangOpts.CPlusPlus) |
| 880 | .Case("has_trivial_destructor", LangOpts.CPlusPlus) |
| 881 | .Case("has_virtual_destructor", LangOpts.CPlusPlus) |
| 882 | .Case("is_abstract", LangOpts.CPlusPlus) |
| 883 | .Case("is_base_of", LangOpts.CPlusPlus) |
| 884 | .Case("is_class", LangOpts.CPlusPlus) |
| 885 | .Case("is_convertible_to", LangOpts.CPlusPlus) |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 886 | .Case("is_empty", LangOpts.CPlusPlus) |
| 887 | .Case("is_enum", LangOpts.CPlusPlus) |
| 888 | .Case("is_final", LangOpts.CPlusPlus) |
| 889 | .Case("is_literal", LangOpts.CPlusPlus) |
| 890 | .Case("is_standard_layout", LangOpts.CPlusPlus) |
| 891 | .Case("is_pod", LangOpts.CPlusPlus) |
| 892 | .Case("is_polymorphic", LangOpts.CPlusPlus) |
| 893 | .Case("is_trivial", LangOpts.CPlusPlus) |
| 894 | .Case("is_trivially_assignable", LangOpts.CPlusPlus) |
| 895 | .Case("is_trivially_constructible", LangOpts.CPlusPlus) |
| 896 | .Case("is_trivially_copyable", LangOpts.CPlusPlus) |
| 897 | .Case("is_union", LangOpts.CPlusPlus) |
| 898 | .Case("modules", LangOpts.Modules) |
| 899 | .Case("tls", PP.getTargetInfo().isTLSSupported()) |
| 900 | .Case("underlying_type", LangOpts.CPlusPlus) |
| 901 | .Default(false); |
| 902 | } |
| 903 | |
| 904 | /// HasExtension - Return true if we recognize and implement the feature |
| 905 | /// specified by the identifier, either as an extension or a standard language |
| 906 | /// feature. |
| 907 | static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { |
| 908 | if (HasFeature(PP, II)) |
| 909 | return true; |
| 910 | |
| 911 | // If the use of an extension results in an error diagnostic, extensions are |
| 912 | // effectively unavailable, so just return false here. |
| 913 | if (PP.getDiagnostics().getExtensionHandlingBehavior() == |
| 914 | DiagnosticsEngine::Ext_Error) |
| 915 | return false; |
| 916 | |
| 917 | const LangOptions &LangOpts = PP.getLangOpts(); |
| 918 | StringRef Extension = II->getName(); |
| 919 | |
| 920 | // Normalize the extension name, __foo__ becomes foo. |
| 921 | if (Extension.startswith("__") && Extension.endswith("__") && |
| 922 | Extension.size() >= 4) |
| 923 | Extension = Extension.substr(2, Extension.size() - 4); |
| 924 | |
| 925 | // Because we inherit the feature list from HasFeature, this string switch |
| 926 | // must be less restrictive than HasFeature's. |
| 927 | return llvm::StringSwitch<bool>(Extension) |
| 928 | // C11 features supported by other languages as extensions. |
| 929 | .Case("c_alignas", true) |
| 930 | .Case("c_atomic", true) |
| 931 | .Case("c_generic_selections", true) |
| 932 | .Case("c_static_assert", true) |
| 933 | // C++0x features supported by other languages as extensions. |
| 934 | .Case("cxx_atomic", LangOpts.CPlusPlus) |
| 935 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus) |
| 936 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) |
| 937 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) |
| 938 | .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) |
| 939 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) |
| 940 | .Case("cxx_override_control", LangOpts.CPlusPlus) |
| 941 | .Case("cxx_range_for", LangOpts.CPlusPlus) |
| 942 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) |
| 943 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus) |
| 944 | .Default(false); |
| 945 | } |
| 946 | |
| 947 | /// HasAttribute - Return true if we recognize and implement the attribute |
| 948 | /// specified by the given identifier. |
| 949 | static bool HasAttribute(const IdentifierInfo *II) { |
| 950 | StringRef Name = II->getName(); |
| 951 | // Normalize the attribute name, __foo__ becomes foo. |
| 952 | if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4) |
| 953 | Name = Name.substr(2, Name.size() - 4); |
| 954 | |
| 955 | // FIXME: Do we need to handle namespaces here? |
| 956 | return llvm::StringSwitch<bool>(Name) |
| 957 | #include "clang/Lex/AttrSpellings.inc" |
| 958 | .Default(false); |
| 959 | } |
| 960 | |
| 961 | /// EvaluateHasIncludeCommon - Process a '__has_include("path")' |
| 962 | /// or '__has_include_next("path")' expression. |
| 963 | /// Returns true if successful. |
| 964 | static bool EvaluateHasIncludeCommon(Token &Tok, |
| 965 | IdentifierInfo *II, Preprocessor &PP, |
| 966 | const DirectoryLookup *LookupFrom) { |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 967 | // Save the location of the current token. If a '(' is later found, use |
Aaron Ballman | 6b716c5 | 2013-01-15 21:59:46 +0000 | [diff] [blame^] | 968 | // that location. If not, use the end of this location instead. |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 969 | SourceLocation LParenLoc = Tok.getLocation(); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 970 | |
| 971 | // Get '('. |
| 972 | PP.LexNonComment(Tok); |
| 973 | |
| 974 | // Ensure we have a '('. |
| 975 | if (Tok.isNot(tok::l_paren)) { |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 976 | // No '(', use end of last token. |
| 977 | LParenLoc = PP.getLocForEndOfToken(LParenLoc); |
| 978 | PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName(); |
| 979 | // If the next token looks like a filename or the start of one, |
| 980 | // assume it is and process it as such. |
| 981 | if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) && |
| 982 | !Tok.is(tok::less)) |
| 983 | return false; |
| 984 | } else { |
| 985 | // Save '(' location for possible missing ')' message. |
| 986 | LParenLoc = Tok.getLocation(); |
| 987 | |
Eli Friedman | a0f2d02 | 2013-01-09 02:20:00 +0000 | [diff] [blame] | 988 | if (PP.getCurrentLexer()) { |
| 989 | // Get the file name. |
| 990 | PP.getCurrentLexer()->LexIncludeFilename(Tok); |
| 991 | } else { |
| 992 | // We're in a macro, so we can't use LexIncludeFilename; just |
| 993 | // grab the next token. |
| 994 | PP.Lex(Tok); |
| 995 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 998 | // Reserve a buffer to get the spelling. |
| 999 | SmallString<128> FilenameBuffer; |
| 1000 | StringRef Filename; |
| 1001 | SourceLocation EndLoc; |
| 1002 | |
| 1003 | switch (Tok.getKind()) { |
| 1004 | case tok::eod: |
| 1005 | // If the token kind is EOD, the error has already been diagnosed. |
| 1006 | return false; |
| 1007 | |
| 1008 | case tok::angle_string_literal: |
| 1009 | case tok::string_literal: { |
| 1010 | bool Invalid = false; |
| 1011 | Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); |
| 1012 | if (Invalid) |
| 1013 | return false; |
| 1014 | break; |
| 1015 | } |
| 1016 | |
| 1017 | case tok::less: |
| 1018 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1019 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1020 | FilenameBuffer.push_back('<'); |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1021 | if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) { |
| 1022 | // Let the caller know a <eod> was found by changing the Token kind. |
| 1023 | Tok.setKind(tok::eod); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1024 | return false; // Found <eod> but no ">"? Diagnostic already emitted. |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1025 | } |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1026 | Filename = FilenameBuffer.str(); |
| 1027 | break; |
| 1028 | default: |
| 1029 | PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1033 | SourceLocation FilenameLoc = Tok.getLocation(); |
| 1034 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1035 | // Get ')'. |
| 1036 | PP.LexNonComment(Tok); |
| 1037 | |
| 1038 | // Ensure we have a trailing ). |
| 1039 | if (Tok.isNot(tok::r_paren)) { |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1040 | PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen) |
| 1041 | << II->getName(); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1042 | PP.Diag(LParenLoc, diag::note_matching) << "("; |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); |
| 1047 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1048 | // error. |
| 1049 | if (Filename.empty()) |
| 1050 | return false; |
| 1051 | |
| 1052 | // Search include directories. |
| 1053 | const DirectoryLookup *CurDir; |
| 1054 | const FileEntry *File = |
| 1055 | PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL); |
| 1056 | |
| 1057 | // Get the result value. A result of true means the file exists. |
| 1058 | return File != 0; |
| 1059 | } |
| 1060 | |
| 1061 | /// EvaluateHasInclude - Process a '__has_include("path")' expression. |
| 1062 | /// Returns true if successful. |
| 1063 | static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, |
| 1064 | Preprocessor &PP) { |
| 1065 | return EvaluateHasIncludeCommon(Tok, II, PP, NULL); |
| 1066 | } |
| 1067 | |
| 1068 | /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. |
| 1069 | /// Returns true if successful. |
| 1070 | static bool EvaluateHasIncludeNext(Token &Tok, |
| 1071 | IdentifierInfo *II, Preprocessor &PP) { |
| 1072 | // __has_include_next is like __has_include, except that we start |
| 1073 | // searching after the current found directory. If we can't do this, |
| 1074 | // issue a diagnostic. |
| 1075 | const DirectoryLookup *Lookup = PP.GetCurDirLookup(); |
| 1076 | if (PP.isInPrimaryFile()) { |
| 1077 | Lookup = 0; |
| 1078 | PP.Diag(Tok, diag::pp_include_next_in_primary); |
| 1079 | } else if (Lookup == 0) { |
| 1080 | PP.Diag(Tok, diag::pp_include_next_absolute_path); |
| 1081 | } else { |
| 1082 | // Start looking up in the next directory. |
| 1083 | ++Lookup; |
| 1084 | } |
| 1085 | |
| 1086 | return EvaluateHasIncludeCommon(Tok, II, PP, Lookup); |
| 1087 | } |
| 1088 | |
Douglas Gregor | b09de51 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1089 | /// \brief Process __building_module(identifier) expression. |
| 1090 | /// \returns true if we are building the named module, false otherwise. |
| 1091 | static bool EvaluateBuildingModule(Token &Tok, |
| 1092 | IdentifierInfo *II, Preprocessor &PP) { |
| 1093 | // Get '('. |
| 1094 | PP.LexNonComment(Tok); |
| 1095 | |
| 1096 | // Ensure we have a '('. |
| 1097 | if (Tok.isNot(tok::l_paren)) { |
| 1098 | PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName(); |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | // Save '(' location for possible missing ')' message. |
| 1103 | SourceLocation LParenLoc = Tok.getLocation(); |
| 1104 | |
| 1105 | // Get the module name. |
| 1106 | PP.LexNonComment(Tok); |
| 1107 | |
| 1108 | // Ensure that we have an identifier. |
| 1109 | if (Tok.isNot(tok::identifier)) { |
| 1110 | PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module); |
| 1111 | return false; |
| 1112 | } |
| 1113 | |
| 1114 | bool Result |
| 1115 | = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule; |
| 1116 | |
| 1117 | // Get ')'. |
| 1118 | PP.LexNonComment(Tok); |
| 1119 | |
| 1120 | // Ensure we have a trailing ). |
| 1121 | if (Tok.isNot(tok::r_paren)) { |
| 1122 | PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName(); |
| 1123 | PP.Diag(LParenLoc, diag::note_matching) << "("; |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | return Result; |
| 1128 | } |
| 1129 | |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1130 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 1131 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 1132 | void Preprocessor::ExpandBuiltinMacro(Token &Tok) { |
| 1133 | // Figure out which token this is. |
| 1134 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1135 | assert(II && "Can't be a macro without id info!"); |
| 1136 | |
| 1137 | // If this is an _Pragma or Microsoft __pragma directive, expand it, |
| 1138 | // invoke the pragma handler, then lex the token after it. |
| 1139 | if (II == Ident_Pragma) |
| 1140 | return Handle_Pragma(Tok); |
| 1141 | else if (II == Ident__pragma) // in non-MS mode this is null |
| 1142 | return HandleMicrosoft__pragma(Tok); |
| 1143 | |
| 1144 | ++NumBuiltinMacroExpanded; |
| 1145 | |
| 1146 | SmallString<128> TmpBuffer; |
| 1147 | llvm::raw_svector_ostream OS(TmpBuffer); |
| 1148 | |
| 1149 | // Set up the return result. |
| 1150 | Tok.setIdentifierInfo(0); |
| 1151 | Tok.clearFlag(Token::NeedsCleaning); |
| 1152 | |
| 1153 | if (II == Ident__LINE__) { |
| 1154 | // C99 6.10.8: "__LINE__: The presumed line number (within the current |
| 1155 | // source file) of the current source line (an integer constant)". This can |
| 1156 | // be affected by #line. |
| 1157 | SourceLocation Loc = Tok.getLocation(); |
| 1158 | |
| 1159 | // Advance to the location of the first _, this might not be the first byte |
| 1160 | // of the token if it starts with an escaped newline. |
| 1161 | Loc = AdvanceToTokenCharacter(Loc, 0); |
| 1162 | |
| 1163 | // One wrinkle here is that GCC expands __LINE__ to location of the *end* of |
| 1164 | // a macro expansion. This doesn't matter for object-like macros, but |
| 1165 | // can matter for a function-like macro that expands to contain __LINE__. |
| 1166 | // Skip down through expansion points until we find a file loc for the |
| 1167 | // end of the expansion history. |
| 1168 | Loc = SourceMgr.getExpansionRange(Loc).second; |
| 1169 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); |
| 1170 | |
| 1171 | // __LINE__ expands to a simple numeric value. |
| 1172 | OS << (PLoc.isValid()? PLoc.getLine() : 1); |
| 1173 | Tok.setKind(tok::numeric_constant); |
| 1174 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
| 1175 | // C99 6.10.8: "__FILE__: The presumed name of the current source file (a |
| 1176 | // character string literal)". This can be affected by #line. |
| 1177 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 1178 | |
| 1179 | // __BASE_FILE__ is a GNU extension that returns the top of the presumed |
| 1180 | // #include stack instead of the current file. |
| 1181 | if (II == Ident__BASE_FILE__ && PLoc.isValid()) { |
| 1182 | SourceLocation NextLoc = PLoc.getIncludeLoc(); |
| 1183 | while (NextLoc.isValid()) { |
| 1184 | PLoc = SourceMgr.getPresumedLoc(NextLoc); |
| 1185 | if (PLoc.isInvalid()) |
| 1186 | break; |
| 1187 | |
| 1188 | NextLoc = PLoc.getIncludeLoc(); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
| 1193 | SmallString<128> FN; |
| 1194 | if (PLoc.isValid()) { |
| 1195 | FN += PLoc.getFilename(); |
| 1196 | Lexer::Stringify(FN); |
| 1197 | OS << '"' << FN.str() << '"'; |
| 1198 | } |
| 1199 | Tok.setKind(tok::string_literal); |
| 1200 | } else if (II == Ident__DATE__) { |
| 1201 | if (!DATELoc.isValid()) |
| 1202 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 1203 | Tok.setKind(tok::string_literal); |
| 1204 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
| 1205 | Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), |
| 1206 | Tok.getLocation(), |
| 1207 | Tok.getLength())); |
| 1208 | return; |
| 1209 | } else if (II == Ident__TIME__) { |
| 1210 | if (!TIMELoc.isValid()) |
| 1211 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 1212 | Tok.setKind(tok::string_literal); |
| 1213 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
| 1214 | Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), |
| 1215 | Tok.getLocation(), |
| 1216 | Tok.getLength())); |
| 1217 | return; |
| 1218 | } else if (II == Ident__INCLUDE_LEVEL__) { |
| 1219 | // Compute the presumed include depth of this token. This can be affected |
| 1220 | // by GNU line markers. |
| 1221 | unsigned Depth = 0; |
| 1222 | |
| 1223 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 1224 | if (PLoc.isValid()) { |
| 1225 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 1226 | for (; PLoc.isValid(); ++Depth) |
| 1227 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 1228 | } |
| 1229 | |
| 1230 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 1231 | OS << Depth; |
| 1232 | Tok.setKind(tok::numeric_constant); |
| 1233 | } else if (II == Ident__TIMESTAMP__) { |
| 1234 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 1235 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 1236 | |
| 1237 | // Get the file that we are lexing out of. If we're currently lexing from |
| 1238 | // a macro, dig into the include stack. |
| 1239 | const FileEntry *CurFile = 0; |
| 1240 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
| 1241 | |
| 1242 | if (TheLexer) |
| 1243 | CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); |
| 1244 | |
| 1245 | const char *Result; |
| 1246 | if (CurFile) { |
| 1247 | time_t TT = CurFile->getModificationTime(); |
| 1248 | struct tm *TM = localtime(&TT); |
| 1249 | Result = asctime(TM); |
| 1250 | } else { |
| 1251 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 1252 | } |
| 1253 | // Surround the string with " and strip the trailing newline. |
| 1254 | OS << '"' << StringRef(Result, strlen(Result)-1) << '"'; |
| 1255 | Tok.setKind(tok::string_literal); |
| 1256 | } else if (II == Ident__COUNTER__) { |
| 1257 | // __COUNTER__ expands to a simple numeric value. |
| 1258 | OS << CounterValue++; |
| 1259 | Tok.setKind(tok::numeric_constant); |
| 1260 | } else if (II == Ident__has_feature || |
| 1261 | II == Ident__has_extension || |
| 1262 | II == Ident__has_builtin || |
| 1263 | II == Ident__has_attribute) { |
| 1264 | // The argument to these builtins should be a parenthesized identifier. |
| 1265 | SourceLocation StartLoc = Tok.getLocation(); |
| 1266 | |
| 1267 | bool IsValid = false; |
| 1268 | IdentifierInfo *FeatureII = 0; |
| 1269 | |
| 1270 | // Read the '('. |
Andy Gibbs | 3f03b58 | 2012-11-17 19:18:27 +0000 | [diff] [blame] | 1271 | LexUnexpandedToken(Tok); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1272 | if (Tok.is(tok::l_paren)) { |
| 1273 | // Read the identifier |
Andy Gibbs | 3f03b58 | 2012-11-17 19:18:27 +0000 | [diff] [blame] | 1274 | LexUnexpandedToken(Tok); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1275 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_const)) { |
| 1276 | FeatureII = Tok.getIdentifierInfo(); |
| 1277 | |
| 1278 | // Read the ')'. |
Andy Gibbs | 3f03b58 | 2012-11-17 19:18:27 +0000 | [diff] [blame] | 1279 | LexUnexpandedToken(Tok); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1280 | if (Tok.is(tok::r_paren)) |
| 1281 | IsValid = true; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | bool Value = false; |
| 1286 | if (!IsValid) |
| 1287 | Diag(StartLoc, diag::err_feature_check_malformed); |
| 1288 | else if (II == Ident__has_builtin) { |
| 1289 | // Check for a builtin is trivial. |
| 1290 | Value = FeatureII->getBuiltinID() != 0; |
| 1291 | } else if (II == Ident__has_attribute) |
| 1292 | Value = HasAttribute(FeatureII); |
| 1293 | else if (II == Ident__has_extension) |
| 1294 | Value = HasExtension(*this, FeatureII); |
| 1295 | else { |
| 1296 | assert(II == Ident__has_feature && "Must be feature check"); |
| 1297 | Value = HasFeature(*this, FeatureII); |
| 1298 | } |
| 1299 | |
| 1300 | OS << (int)Value; |
| 1301 | if (IsValid) |
| 1302 | Tok.setKind(tok::numeric_constant); |
| 1303 | } else if (II == Ident__has_include || |
| 1304 | II == Ident__has_include_next) { |
| 1305 | // The argument to these two builtins should be a parenthesized |
| 1306 | // file name string literal using angle brackets (<>) or |
| 1307 | // double-quotes (""). |
| 1308 | bool Value; |
| 1309 | if (II == Ident__has_include) |
| 1310 | Value = EvaluateHasInclude(Tok, II, *this); |
| 1311 | else |
| 1312 | Value = EvaluateHasIncludeNext(Tok, II, *this); |
| 1313 | OS << (int)Value; |
Richard Trieu | 97bc3d5 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1314 | if (Tok.is(tok::r_paren)) |
| 1315 | Tok.setKind(tok::numeric_constant); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1316 | } else if (II == Ident__has_warning) { |
| 1317 | // The argument should be a parenthesized string literal. |
| 1318 | // The argument to these builtins should be a parenthesized identifier. |
| 1319 | SourceLocation StartLoc = Tok.getLocation(); |
| 1320 | bool IsValid = false; |
| 1321 | bool Value = false; |
| 1322 | // Read the '('. |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1323 | LexUnexpandedToken(Tok); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1324 | do { |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1325 | if (Tok.isNot(tok::l_paren)) { |
| 1326 | Diag(StartLoc, diag::err_warning_check_malformed); |
| 1327 | break; |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1328 | } |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1329 | |
| 1330 | LexUnexpandedToken(Tok); |
| 1331 | std::string WarningName; |
| 1332 | SourceLocation StrStartLoc = Tok.getLocation(); |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1333 | if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", |
| 1334 | /*MacroExpansion=*/false)) { |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1335 | // Eat tokens until ')'. |
Andy Gibbs | 6d534d4 | 2012-11-17 22:17:28 +0000 | [diff] [blame] | 1336 | while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && |
| 1337 | Tok.isNot(tok::eof)) |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1338 | LexUnexpandedToken(Tok); |
| 1339 | break; |
| 1340 | } |
| 1341 | |
| 1342 | // Is the end a ')'? |
| 1343 | if (!(IsValid = Tok.is(tok::r_paren))) { |
| 1344 | Diag(StartLoc, diag::err_warning_check_malformed); |
| 1345 | break; |
| 1346 | } |
| 1347 | |
| 1348 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 1349 | WarningName[1] != 'W') { |
| 1350 | Diag(StrStartLoc, diag::warn_has_warning_invalid_option); |
| 1351 | break; |
| 1352 | } |
| 1353 | |
| 1354 | // Finally, check if the warning flags maps to a diagnostic group. |
| 1355 | // We construct a SmallVector here to talk to getDiagnosticIDs(). |
| 1356 | // Although we don't use the result, this isn't a hot path, and not |
| 1357 | // worth special casing. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1358 | SmallVector<diag::kind, 10> Diags; |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1359 | Value = !getDiagnostics().getDiagnosticIDs()-> |
| 1360 | getDiagnosticsInGroup(WarningName.substr(2), Diags); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1361 | } while (false); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1362 | |
| 1363 | OS << (int)Value; |
Andy Gibbs | b9971ba | 2012-11-17 19:14:53 +0000 | [diff] [blame] | 1364 | if (IsValid) |
| 1365 | Tok.setKind(tok::numeric_constant); |
Douglas Gregor | b09de51 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1366 | } else if (II == Ident__building_module) { |
| 1367 | // The argument to this builtin should be an identifier. The |
| 1368 | // builtin evaluates to 1 when that identifier names the module we are |
| 1369 | // currently building. |
| 1370 | OS << (int)EvaluateBuildingModule(Tok, II, *this); |
| 1371 | Tok.setKind(tok::numeric_constant); |
| 1372 | } else if (II == Ident__MODULE__) { |
| 1373 | // The current module as an identifier. |
| 1374 | OS << getLangOpts().CurrentModule; |
| 1375 | IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); |
| 1376 | Tok.setIdentifierInfo(ModuleII); |
| 1377 | Tok.setKind(ModuleII->getTokenID()); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1378 | } else { |
| 1379 | llvm_unreachable("Unknown identifier!"); |
| 1380 | } |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 1381 | CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); |
Joao Matos | 3e1ec72 | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | void Preprocessor::markMacroAsUsed(MacroInfo *MI) { |
| 1385 | // If the 'used' status changed, and the macro requires 'unused' warning, |
| 1386 | // remove its SourceLocation from the warn-for-unused-macro locations. |
| 1387 | if (MI->isWarnIfUnused() && !MI->isUsed()) |
| 1388 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 1389 | MI->setIsUsed(true); |
| 1390 | } |