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