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