Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the top level handling of macro expasion for the |
| 11 | // preprocessor. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "MacroArgs.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/FileManager.h" |
Eric Christopher | 1f84f8d | 2010-06-24 02:02:00 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 21 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 22 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 295a2a6 | 2010-10-30 00:23:06 +0000 | [diff] [blame] | 23 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Ted Kremenek | d768150 | 2011-10-12 19:46:30 +0000 | [diff] [blame] | 24 | #include "clang/Lex/LiteralSupport.h" |
Benjamin Kramer | 32592e8 | 2010-01-09 18:53:11 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Argyrios Kyrtzidis | 5b3284a | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Dylan Noblesmith | 1770e0d | 2011-12-22 22:49:47 +0000 | [diff] [blame] | 27 | #include "llvm/Config/llvm-config.h" |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 30 | #include <cstdio> |
Chris Lattner | f90a248 | 2008-03-18 05:59:11 +0000 | [diff] [blame] | 31 | #include <ctime> |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
Douglas Gregor | 295a2a6 | 2010-10-30 00:23:06 +0000 | [diff] [blame] | 34 | MacroInfo *Preprocessor::getInfoForMacro(IdentifierInfo *II) const { |
| 35 | assert(II->hasMacroDefinition() && "Identifier is not a macro!"); |
| 36 | |
| 37 | llvm::DenseMap<IdentifierInfo*, MacroInfo*>::const_iterator Pos |
| 38 | = Macros.find(II); |
| 39 | if (Pos == Macros.end()) { |
| 40 | // Load this macro from the external source. |
| 41 | getExternalSource()->LoadMacroDefinition(II); |
| 42 | Pos = Macros.find(II); |
| 43 | } |
| 44 | assert(Pos != Macros.end() && "Identifier macro info is missing!"); |
| 45 | return Pos->second; |
| 46 | } |
| 47 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 48 | /// setMacroInfo - Specify a macro for this identifier. |
| 49 | /// |
| 50 | void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) { |
Chris Lattner | 555589d | 2009-04-10 21:17:07 +0000 | [diff] [blame] | 51 | if (MI) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 52 | Macros[II] = MI; |
| 53 | II->setHasMacroDefinition(true); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 54 | if (II->isFromAST()) |
| 55 | II->setChangedSinceDeserialization(); |
Chris Lattner | 555589d | 2009-04-10 21:17:07 +0000 | [diff] [blame] | 56 | } else if (II->hasMacroDefinition()) { |
| 57 | Macros.erase(II); |
| 58 | II->setHasMacroDefinition(false); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 59 | if (II->isFromAST()) |
| 60 | II->setChangedSinceDeserialization(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 65 | /// table and mark it as a builtin macro to be expanded. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 66 | static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 67 | // Get the identifier. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 68 | IdentifierInfo *Id = PP.getIdentifierInfo(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 70 | // Mark it as being a macro that is builtin. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 71 | MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 72 | MI->setIsBuiltinMacro(); |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 73 | PP.setMacroInfo(Id, MI); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 74 | return Id; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 79 | /// identifier table. |
| 80 | void Preprocessor::RegisterBuiltinMacros() { |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 81 | Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); |
| 82 | Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); |
| 83 | Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); |
| 84 | Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); |
| 85 | Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); |
| 86 | Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 88 | // GCC Extensions. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 89 | Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); |
| 90 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); |
| 91 | Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 93 | // Clang Extensions. |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 94 | Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 95 | Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 96 | Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); |
Anders Carlsson | cae5095 | 2010-10-20 02:31:43 +0000 | [diff] [blame] | 97 | Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 98 | Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); |
| 99 | Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); |
Ted Kremenek | d768150 | 2011-10-12 19:46:30 +0000 | [diff] [blame] | 100 | Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 101 | |
| 102 | // Microsoft Extensions. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 103 | if (Features.MicrosoftExt) |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 104 | Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); |
| 105 | else |
| 106 | Ident__pragma = 0; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 110 | /// in its expansion, currently expands to that token literally. |
| 111 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
| 112 | const IdentifierInfo *MacroIdent, |
| 113 | Preprocessor &PP) { |
| 114 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 115 | |
| 116 | // If the token isn't an identifier, it's always literally expanded. |
| 117 | if (II == 0) return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Argyrios Kyrtzidis | 373cb78 | 2011-12-17 04:13:31 +0000 | [diff] [blame] | 119 | // If the information about this identifier is out of date, update it from |
| 120 | // the external source. |
| 121 | if (II->isOutOfDate()) |
| 122 | PP.getExternalSource()->updateOutOfDateIdentifier(*II); |
| 123 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 124 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 125 | // expanded so it's not a trivial expansion. |
| 126 | if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && |
| 127 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 128 | II != MacroIdent) |
| 129 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 131 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 132 | // it. |
| 133 | if (MI->isObjectLike()) return true; |
| 134 | |
| 135 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 136 | // as long as the identifier is not a macro argument. |
| 137 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 138 | I != E; ++I) |
| 139 | if (*I == II) |
| 140 | return false; // Identifier is a macro argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 147 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 148 | /// method should have no observable side-effect on the lexed tokens. |
| 149 | bool Preprocessor::isNextPPTokenLParen() { |
| 150 | // Do some quick tests for rejection cases. |
| 151 | unsigned Val; |
| 152 | if (CurLexer) |
| 153 | Val = CurLexer->isNextPPTokenLParen(); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 154 | else if (CurPTHLexer) |
| 155 | Val = CurPTHLexer->isNextPPTokenLParen(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 156 | else |
| 157 | Val = CurTokenLexer->isNextTokenLParen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 159 | if (Val == 2) { |
| 160 | // We have run off the end. If it's a source file we don't |
| 161 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 162 | // macro stack. |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 163 | if (CurPPLexer) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 164 | return false; |
| 165 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 166 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 167 | if (Entry.TheLexer) |
| 168 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
Ted Kremenek | dd95d6c | 2008-11-20 16:46:54 +0000 | [diff] [blame] | 169 | else if (Entry.ThePTHLexer) |
| 170 | Val = Entry.ThePTHLexer->isNextPPTokenLParen(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 171 | else |
| 172 | Val = Entry.TheTokenLexer->isNextTokenLParen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 174 | if (Val != 2) |
| 175 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 177 | // Ran off the end of a source file? |
Ted Kremenek | dd95d6c | 2008-11-20 16:46:54 +0000 | [diff] [blame] | 178 | if (Entry.ThePPLexer) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 184 | // have found something that isn't a '(' or we found the end of the |
| 185 | // translation unit. In either case, return false. |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 186 | return Val == 1; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 190 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 192 | MacroInfo *MI) { |
Douglas Gregor | 1367897 | 2010-01-26 19:43:43 +0000 | [diff] [blame] | 193 | // If this is a macro expansion in the "#if !defined(x)" line for the file, |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 194 | // then the macro could expand to different things in other contexts, we need |
| 195 | // to disable the optimization in this case. |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 196 | if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 198 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 199 | if (MI->isBuiltinMacro()) { |
Argyrios Kyrtzidis | 1b2d536 | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 200 | if (Callbacks) Callbacks->MacroExpands(Identifier, MI, |
| 201 | Identifier.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 202 | ExpandBuiltinMacro(Identifier); |
| 203 | return false; |
| 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 206 | /// Args - If this is a function-like macro expansion, this contains, |
| 207 | /// for each macro argument, the list of tokens that were provided to the |
| 208 | /// invocation. |
| 209 | MacroArgs *Args = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 211 | // Remember where the end of the expansion occurred. For an object-like |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 212 | // macro, this is the identifier. For a function-like macro, this is the ')'. |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 213 | SourceLocation ExpansionEnd = Identifier.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 215 | // If this is a function-like macro, read the arguments. |
| 216 | if (MI->isFunctionLike()) { |
| 217 | // C99 6.10.3p10: If the preprocessing token immediately after the the macro |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 218 | // name isn't a '(', this macro should not be expanded. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 219 | if (!isNextPPTokenLParen()) |
| 220 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 222 | // Remember that we are now parsing the arguments to a macro invocation. |
| 223 | // Preprocessor directives used inside macro arguments are not portable, and |
| 224 | // this enables the warning. |
| 225 | InMacroArgs = true; |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 226 | Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 228 | // Finished parsing args. |
| 229 | InMacroArgs = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 231 | // If there was an error parsing the arguments, bail out. |
| 232 | if (Args == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 234 | ++NumFnMacroExpanded; |
| 235 | } else { |
| 236 | ++NumMacroExpanded; |
| 237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 239 | // Notice that this macro has been used. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 240 | markMacroAsUsed(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 242 | // Remember where the token is expanded. |
| 243 | SourceLocation ExpandLoc = Identifier.getLocation(); |
Argyrios Kyrtzidis | b7d98d3 | 2011-04-27 05:04:02 +0000 | [diff] [blame] | 244 | |
Argyrios Kyrtzidis | 1b2d536 | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 245 | if (Callbacks) Callbacks->MacroExpands(Identifier, MI, |
| 246 | SourceRange(ExpandLoc, ExpansionEnd)); |
| 247 | |
| 248 | // If we started lexing a macro, enter the macro expansion body. |
| 249 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 250 | // If this macro expands to no tokens, don't bother to push it onto the |
| 251 | // expansion stack, only to take it right back off. |
| 252 | if (MI->getNumTokens() == 0) { |
| 253 | // No need for arg info. |
Chris Lattner | 561395b | 2009-12-14 22:12:52 +0000 | [diff] [blame] | 254 | if (Args) Args->destroy(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 256 | // Ignore this macro use, just return the next token in the current |
| 257 | // buffer. |
| 258 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 259 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 261 | Lex(Identifier); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 263 | // If the identifier isn't on some OTHER line, inherit the leading |
| 264 | // whitespace/first-on-a-line property of this token. This handles |
| 265 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 266 | // empty. |
| 267 | if (!Identifier.isAtStartOfLine()) { |
| 268 | if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine); |
| 269 | if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace); |
| 270 | } |
Argyrios Kyrtzidis | 44aa1f3 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 271 | Identifier.setFlag(Token::LeadingEmptyMacro); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 272 | ++NumFastMacroExpanded; |
| 273 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 275 | } else if (MI->getNumTokens() == 1 && |
| 276 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 277 | *this)) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 278 | // Otherwise, if this macro expands into a single trivially-expanded |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | // token: expand it now. This handles common cases like |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 280 | // "#define VAL 42". |
Sam Bishop | 9a4939f | 2008-03-21 07:13:02 +0000 | [diff] [blame] | 281 | |
| 282 | // No need for arg info. |
Chris Lattner | 561395b | 2009-12-14 22:12:52 +0000 | [diff] [blame] | 283 | if (Args) Args->destroy(*this); |
Sam Bishop | 9a4939f | 2008-03-21 07:13:02 +0000 | [diff] [blame] | 284 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 285 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 286 | // identifier to the expanded token. |
| 287 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 288 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 290 | // Replace the result token. |
| 291 | Identifier = MI->getReplacementToken(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 293 | // Restore the StartOfLine/LeadingSpace markers. |
| 294 | Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); |
| 295 | Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 297 | // Update the tokens location to include both its expansion and physical |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 298 | // locations. |
| 299 | SourceLocation Loc = |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 300 | SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, |
| 301 | ExpansionEnd,Identifier.getLength()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 302 | Identifier.setLocation(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 8ff66de | 2010-03-26 17:49:16 +0000 | [diff] [blame] | 304 | // If this is a disabled macro or #define X X, we must mark the result as |
| 305 | // unexpandable. |
| 306 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { |
| 307 | if (MacroInfo *NewMI = getMacroInfo(NewII)) |
Abramo Bagnara | 1e8a067 | 2012-01-02 10:08:26 +0000 | [diff] [blame] | 308 | if (!NewMI->isEnabled() || NewMI == MI) { |
Chris Lattner | 8ff66de | 2010-03-26 17:49:16 +0000 | [diff] [blame] | 309 | Identifier.setFlag(Token::DisableExpand); |
Abramo Bagnara | 1e8a067 | 2012-01-02 10:08:26 +0000 | [diff] [blame] | 310 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
| 311 | } |
Chris Lattner | 8ff66de | 2010-03-26 17:49:16 +0000 | [diff] [blame] | 312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 314 | // Since this is not an identifier token, it can't be macro expanded, so |
| 315 | // we're done. |
| 316 | ++NumFastMacroExpanded; |
| 317 | return false; |
| 318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 320 | // Start expanding the macro. |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 321 | EnterMacro(Identifier, ExpansionEnd, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 323 | // Now that the macro is at the top of the include stack, ask the |
| 324 | // preprocessor to read the next token from it. |
| 325 | Lex(Identifier); |
| 326 | return false; |
| 327 | } |
| 328 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 329 | /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next |
| 330 | /// token is the '(' of the macro, this method is invoked to read all of the |
| 331 | /// actual arguments specified for the macro invocation. This returns null on |
| 332 | /// error. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 333 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 334 | MacroInfo *MI, |
| 335 | SourceLocation &MacroEnd) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 336 | // The number of fixed arguments to parse. |
| 337 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 338 | bool isVariadic = MI->isVariadic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 340 | // Outer loop, while there are more arguments, keep reading them. |
| 341 | Token Tok; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 343 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 344 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 345 | LexUnexpandedToken(Tok); |
| 346 | assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 348 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 349 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 350 | // heap allocations in the common case. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 351 | SmallVector<Token, 64> ArgTokens; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 352 | |
| 353 | unsigned NumActuals = 0; |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 354 | while (Tok.isNot(tok::r_paren)) { |
| 355 | assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && |
| 356 | "only expect argument separators here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 358 | unsigned ArgTokenStart = ArgTokens.size(); |
| 359 | SourceLocation ArgStartLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 361 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 362 | // that we already consumed the first one. |
| 363 | unsigned NumParens = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 365 | while (1) { |
| 366 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 367 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 368 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 370 | if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n" |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 371 | Diag(MacroName, diag::err_unterm_macro_invoc); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 372 | // Do not lose the EOF/EOD. Return it to the client. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 373 | MacroName = Tok; |
| 374 | return 0; |
| 375 | } else if (Tok.is(tok::r_paren)) { |
| 376 | // If we found the ) token, the macro arg list is done. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 377 | if (NumParens-- == 0) { |
| 378 | MacroEnd = Tok.getLocation(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 379 | break; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 381 | } else if (Tok.is(tok::l_paren)) { |
| 382 | ++NumParens; |
| 383 | } else if (Tok.is(tok::comma) && NumParens == 0) { |
| 384 | // Comma ends this argument if there are more fixed arguments expected. |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 385 | // However, if this is a variadic macro, and this is part of the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | // variadic part, then the comma is just an argument token. |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 387 | if (!isVariadic) break; |
| 388 | if (NumFixedArgsLeft > 1) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 389 | break; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 390 | } else if (Tok.is(tok::comment) && !KeepMacroComments) { |
| 391 | // If this is a comment token in the argument list and we're just in |
| 392 | // -C mode (not -CC mode), discard the comment. |
| 393 | continue; |
Chris Lattner | 5c497a8 | 2009-04-18 06:44:18 +0000 | [diff] [blame] | 394 | } else if (Tok.getIdentifierInfo() != 0) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 395 | // Reading macro arguments can cause macros that we are currently |
| 396 | // expanding from to be popped off the expansion stack. Doing so causes |
| 397 | // them to be reenabled for expansion. Here we record whether any |
| 398 | // identifiers we lex as macro arguments correspond to disabled macros. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | // If so, we mark the token as noexpand. This is a subtle aspect of |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 400 | // C99 6.10.3.4p2. |
| 401 | if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) |
| 402 | if (!MI->isEnabled()) |
| 403 | Tok.setFlag(Token::DisableExpand); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 404 | } else if (Tok.is(tok::code_completion)) { |
| 405 | if (CodeComplete) |
| 406 | CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), |
| 407 | MI, NumActuals); |
| 408 | // Don't mark that we reached the code-completion point because the |
| 409 | // parser is going to handle the token and there will be another |
| 410 | // code-completion callback. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 411 | } |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 412 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 413 | ArgTokens.push_back(Tok); |
| 414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 416 | // If this was an empty argument list foo(), don't add this as an empty |
| 417 | // argument. |
| 418 | if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) |
| 419 | break; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 421 | // If this is not a variadic macro, and too many args were specified, emit |
| 422 | // an error. |
| 423 | if (!isVariadic && NumFixedArgsLeft == 0) { |
| 424 | if (ArgTokens.size() != ArgTokenStart) |
| 425 | ArgStartLoc = ArgTokens[ArgTokenStart].getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 427 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 428 | // Emitting it at the , could be far away from the macro name. |
| 429 | Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc); |
| 430 | return 0; |
| 431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 32c1388 | 2011-04-22 23:25:09 +0000 | [diff] [blame] | 433 | // Empty arguments are standard in C99 and C++0x, and are supported as an extension in |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 434 | // other modes. |
Richard Smith | 661a996 | 2011-10-15 01:18:56 +0000 | [diff] [blame] | 435 | if (ArgTokens.size() == ArgTokenStart && !Features.C99) |
| 436 | Diag(Tok, Features.CPlusPlus0x ? |
| 437 | diag::warn_cxx98_compat_empty_fnmacro_arg : |
| 438 | diag::ext_empty_fnmacro_arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 440 | // Add a marker EOF token to the end of the token list for this argument. |
| 441 | Token EOFTok; |
| 442 | EOFTok.startToken(); |
| 443 | EOFTok.setKind(tok::eof); |
Chris Lattner | e768988 | 2009-01-26 20:24:53 +0000 | [diff] [blame] | 444 | EOFTok.setLocation(Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 445 | EOFTok.setLength(0); |
| 446 | ArgTokens.push_back(EOFTok); |
| 447 | ++NumActuals; |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 448 | assert(NumFixedArgsLeft != 0 && "Too many arguments parsed"); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 449 | --NumFixedArgsLeft; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 452 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 453 | // arguments. |
| 454 | unsigned MinArgsExpected = MI->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 456 | // See MacroArgs instance var for description of this. |
| 457 | bool isVarargsElided = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 459 | if (NumActuals < MinArgsExpected) { |
| 460 | // There are several cases where too few arguments is ok, handle them now. |
Chris Lattner | 97e2de1 | 2009-04-20 21:08:10 +0000 | [diff] [blame] | 461 | if (NumActuals == 0 && MinArgsExpected == 1) { |
| 462 | // #define A(X) or #define A(...) ---> A() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 97e2de1 | 2009-04-20 21:08:10 +0000 | [diff] [blame] | 464 | // If there is exactly one argument, and that argument is missing, |
| 465 | // then we have an empty "()" argument empty list. This is fine, even if |
| 466 | // the macro expects one argument (the argument is just empty). |
| 467 | isVarargsElided = MI->isVariadic(); |
| 468 | } else if (MI->isVariadic() && |
| 469 | (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) |
| 470 | (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 471 | // Varargs where the named vararg parameter is missing: ok as extension. |
| 472 | // #define A(x, ...) |
| 473 | // A("blah") |
| 474 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 475 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 476 | // Remember this occurred, allowing us to elide the comma when used for |
Chris Lattner | 63bc035 | 2008-05-08 05:10:33 +0000 | [diff] [blame] | 477 | // cases like: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | // #define A(x, foo...) blah(a, ## foo) |
| 479 | // #define B(x, ...) blah(a, ## __VA_ARGS__) |
| 480 | // #define C(...) blah(a, ## __VA_ARGS__) |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 481 | // A(x) B(x) C() |
Chris Lattner | 97e2de1 | 2009-04-20 21:08:10 +0000 | [diff] [blame] | 482 | isVarargsElided = true; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 483 | } else { |
| 484 | // Otherwise, emit the error. |
| 485 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
| 486 | return 0; |
| 487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 489 | // Add a marker EOF token to the end of the token list for this argument. |
| 490 | SourceLocation EndLoc = Tok.getLocation(); |
| 491 | Tok.startToken(); |
| 492 | Tok.setKind(tok::eof); |
| 493 | Tok.setLocation(EndLoc); |
| 494 | Tok.setLength(0); |
| 495 | ArgTokens.push_back(Tok); |
Chris Lattner | 9fc9e77 | 2009-05-13 00:55:26 +0000 | [diff] [blame] | 496 | |
| 497 | // If we expect two arguments, add both as empty. |
| 498 | if (NumActuals == 0 && MinArgsExpected == 2) |
| 499 | ArgTokens.push_back(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 501 | } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) { |
| 502 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 503 | // Emitting it at the , could be far away from the macro name. |
| 504 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
| 505 | return 0; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
David Blaikie | d7bb6a0 | 2011-09-22 02:03:12 +0000 | [diff] [blame] | 508 | return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Argyrios Kyrtzidis | 5b3284a | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 511 | /// \brief Keeps macro expanded tokens for TokenLexers. |
| 512 | // |
| 513 | /// Works like a stack; a TokenLexer adds the macro expanded tokens that is |
| 514 | /// going to lex in the cache and when it finishes the tokens are removed |
| 515 | /// from the end of the cache. |
| 516 | Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 517 | ArrayRef<Token> tokens) { |
Argyrios Kyrtzidis | 5b3284a | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 518 | assert(tokLexer); |
| 519 | if (tokens.empty()) |
| 520 | return 0; |
| 521 | |
| 522 | size_t newIndex = MacroExpandedTokens.size(); |
| 523 | bool cacheNeedsToGrow = tokens.size() > |
| 524 | MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); |
| 525 | MacroExpandedTokens.append(tokens.begin(), tokens.end()); |
| 526 | |
| 527 | if (cacheNeedsToGrow) { |
| 528 | // Go through all the TokenLexers whose 'Tokens' pointer points in the |
| 529 | // buffer and update the pointers to the (potential) new buffer array. |
| 530 | for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { |
| 531 | TokenLexer *prevLexer; |
| 532 | size_t tokIndex; |
| 533 | llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; |
| 534 | prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); |
| 539 | return MacroExpandedTokens.data() + newIndex; |
| 540 | } |
| 541 | |
| 542 | void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { |
| 543 | assert(!MacroExpandingLexersStack.empty()); |
| 544 | size_t tokIndex = MacroExpandingLexersStack.back().second; |
| 545 | assert(tokIndex < MacroExpandedTokens.size()); |
| 546 | // Pop the cached macro expanded tokens from the end. |
| 547 | MacroExpandedTokens.resize(tokIndex); |
| 548 | MacroExpandingLexersStack.pop_back(); |
| 549 | } |
| 550 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 551 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 552 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 553 | /// the identifier tokens inserted. |
| 554 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 555 | Preprocessor &PP) { |
| 556 | time_t TT = time(0); |
| 557 | struct tm *TM = localtime(&TT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 559 | static const char * const Months[] = { |
| 560 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 561 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Douglas Gregor | 5e0fb35 | 2010-11-09 03:20:07 +0000 | [diff] [blame] | 563 | char TmpBuffer[32]; |
Douglas Gregor | b87b29e | 2010-11-09 04:38:09 +0000 | [diff] [blame] | 564 | #ifdef LLVM_ON_WIN32 |
| 565 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 566 | TM->tm_year+1900); |
| 567 | #else |
Douglas Gregor | 5e0fb35 | 2010-11-09 03:20:07 +0000 | [diff] [blame] | 568 | snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 569 | TM->tm_year+1900); |
Douglas Gregor | b87b29e | 2010-11-09 04:38:09 +0000 | [diff] [blame] | 570 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 572 | Token TmpTok; |
| 573 | TmpTok.startToken(); |
| 574 | PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok); |
| 575 | DATELoc = TmpTok.getLocation(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 576 | |
NAKAMURA Takumi | 513038d | 2010-11-09 06:27:32 +0000 | [diff] [blame] | 577 | #ifdef LLVM_ON_WIN32 |
| 578 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 579 | #else |
Douglas Gregor | 5e0fb35 | 2010-11-09 03:20:07 +0000 | [diff] [blame] | 580 | snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
NAKAMURA Takumi | 513038d | 2010-11-09 06:27:32 +0000 | [diff] [blame] | 581 | #endif |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 582 | PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok); |
| 583 | TIMELoc = TmpTok.getLocation(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 586 | |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 587 | /// HasFeature - Return true if we recognize and implement the feature |
| 588 | /// specified by the identifier as a standard language feature. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 589 | static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { |
| 590 | const LangOptions &LangOpts = PP.getLangOptions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Benjamin Kramer | 32592e8 | 2010-01-09 18:53:11 +0000 | [diff] [blame] | 592 | return llvm::StringSwitch<bool>(II->getName()) |
Kostya Serebryany | b619688 | 2011-11-22 01:28:36 +0000 | [diff] [blame] | 593 | .Case("address_sanitizer", LangOpts.AddressSanitizer) |
Benjamin Kramer | 32592e8 | 2010-01-09 18:53:11 +0000 | [diff] [blame] | 594 | .Case("attribute_analyzer_noreturn", true) |
Douglas Gregor | dceb531 | 2011-03-26 12:16:15 +0000 | [diff] [blame] | 595 | .Case("attribute_availability", true) |
Ted Kremenek | 1359300 | 2010-02-18 00:06:04 +0000 | [diff] [blame] | 596 | .Case("attribute_cf_returns_not_retained", true) |
Benjamin Kramer | 32592e8 | 2010-01-09 18:53:11 +0000 | [diff] [blame] | 597 | .Case("attribute_cf_returns_retained", true) |
John McCall | 4820908 | 2010-11-08 19:48:17 +0000 | [diff] [blame] | 598 | .Case("attribute_deprecated_with_message", true) |
Ted Kremenek | 6d9afd9 | 2010-04-29 02:06:42 +0000 | [diff] [blame] | 599 | .Case("attribute_ext_vector_type", true) |
Ted Kremenek | 1359300 | 2010-02-18 00:06:04 +0000 | [diff] [blame] | 600 | .Case("attribute_ns_returns_not_retained", true) |
| 601 | .Case("attribute_ns_returns_retained", true) |
Ted Kremenek | 12b9434 | 2011-01-27 06:54:14 +0000 | [diff] [blame] | 602 | .Case("attribute_ns_consumes_self", true) |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 603 | .Case("attribute_ns_consumed", true) |
| 604 | .Case("attribute_cf_consumed", true) |
Ted Kremenek | 444b035 | 2010-03-05 22:43:32 +0000 | [diff] [blame] | 605 | .Case("attribute_objc_ivar_unused", true) |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 606 | .Case("attribute_objc_method_family", true) |
Ted Kremenek | 6d9afd9 | 2010-04-29 02:06:42 +0000 | [diff] [blame] | 607 | .Case("attribute_overloadable", true) |
John McCall | 4820908 | 2010-11-08 19:48:17 +0000 | [diff] [blame] | 608 | .Case("attribute_unavailable_with_message", true) |
Ted Kremenek | 6d9afd9 | 2010-04-29 02:06:42 +0000 | [diff] [blame] | 609 | .Case("blocks", LangOpts.Blocks) |
Ted Kremenek | 6d9afd9 | 2010-04-29 02:06:42 +0000 | [diff] [blame] | 610 | .Case("cxx_exceptions", LangOpts.Exceptions) |
| 611 | .Case("cxx_rtti", LangOpts.RTTI) |
John McCall | 4820908 | 2010-11-08 19:48:17 +0000 | [diff] [blame] | 612 | .Case("enumerator_attributes", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 613 | // Objective-C features |
| 614 | .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? |
| 615 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
| 616 | .Case("objc_arc_weak", LangOpts.ObjCAutoRefCount && |
John McCall | 9f084a3 | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 617 | LangOpts.ObjCRuntimeHasWeak) |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 618 | .Case("objc_fixed_enum", LangOpts.ObjC2) |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 619 | .Case("objc_instancetype", LangOpts.ObjC2) |
Douglas Gregor | bd507c5 | 2012-01-04 21:16:09 +0000 | [diff] [blame] | 620 | .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) |
Ted Kremenek | 6d9afd9 | 2010-04-29 02:06:42 +0000 | [diff] [blame] | 621 | .Case("objc_nonfragile_abi", LangOpts.ObjCNonFragileABI) |
Ted Kremenek | 3ff9d11 | 2010-04-29 02:06:46 +0000 | [diff] [blame] | 622 | .Case("objc_weak_class", LangOpts.ObjCNonFragileABI) |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 623 | .Case("ownership_holds", true) |
| 624 | .Case("ownership_returns", true) |
| 625 | .Case("ownership_takes", true) |
John McCall | eb2ac8b | 2011-10-18 21:18:53 +0000 | [diff] [blame] | 626 | .Case("arc_cf_code_audited", true) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 627 | // C11 features |
| 628 | .Case("c_alignas", LangOpts.C11) |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame^] | 629 | .Case("c_atomic", LangOpts.C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 630 | .Case("c_generic_selections", LangOpts.C11) |
| 631 | .Case("c_static_assert", LangOpts.C11) |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 632 | // C++0x features |
Douglas Gregor | 7822ee3 | 2011-05-11 23:45:11 +0000 | [diff] [blame] | 633 | .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 634 | .Case("cxx_alias_templates", LangOpts.CPlusPlus0x) |
Peter Collingbourne | fd5f686 | 2011-10-14 23:44:46 +0000 | [diff] [blame] | 635 | .Case("cxx_alignas", LangOpts.CPlusPlus0x) |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame^] | 636 | .Case("cxx_atomic", LangOpts.CPlusPlus0x) |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 637 | .Case("cxx_attributes", LangOpts.CPlusPlus0x) |
Richard Smith | 738291e | 2011-02-20 12:13:05 +0000 | [diff] [blame] | 638 | .Case("cxx_auto_type", LangOpts.CPlusPlus0x) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 639 | //.Case("cxx_constexpr", false); |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 640 | .Case("cxx_decltype", LangOpts.CPlusPlus0x) |
Douglas Gregor | 0750800 | 2011-02-05 20:35:30 +0000 | [diff] [blame] | 641 | .Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x) |
Douglas Gregor | f695a69 | 2011-11-01 01:19:34 +0000 | [diff] [blame] | 642 | .Case("cxx_defaulted_functions", LangOpts.CPlusPlus0x) |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 643 | .Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x) |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 644 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus0x) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 645 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x) |
Sean Hunt | e1f6dea | 2011-08-07 00:34:32 +0000 | [diff] [blame] | 646 | //.Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 647 | .Case("cxx_implicit_moves", LangOpts.CPlusPlus0x) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 648 | //.Case("cxx_inheriting_constructors", false) |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 649 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x) |
Ted Kremenek | 6d9afd9 | 2010-04-29 02:06:42 +0000 | [diff] [blame] | 650 | //.Case("cxx_lambdas", false) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 651 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x) |
Sebastian Redl | 4561ecd | 2011-03-15 21:17:12 +0000 | [diff] [blame] | 652 | .Case("cxx_noexcept", LangOpts.CPlusPlus0x) |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 653 | .Case("cxx_nullptr", LangOpts.CPlusPlus0x) |
Anders Carlsson | c8b9f79 | 2011-03-25 15:04:23 +0000 | [diff] [blame] | 654 | .Case("cxx_override_control", LangOpts.CPlusPlus0x) |
Richard Smith | a391a46 | 2011-04-15 15:14:40 +0000 | [diff] [blame] | 655 | .Case("cxx_range_for", LangOpts.CPlusPlus0x) |
Douglas Gregor | 172b221 | 2011-11-01 01:23:44 +0000 | [diff] [blame] | 656 | .Case("cxx_raw_string_literals", LangOpts.CPlusPlus0x) |
Douglas Gregor | 56209ff | 2011-01-26 21:25:54 +0000 | [diff] [blame] | 657 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x) |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 658 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus0x) |
| 659 | .Case("cxx_strong_enums", LangOpts.CPlusPlus0x) |
| 660 | .Case("cxx_static_assert", LangOpts.CPlusPlus0x) |
| 661 | .Case("cxx_trailing_return", LangOpts.CPlusPlus0x) |
Douglas Gregor | 172b221 | 2011-11-01 01:23:44 +0000 | [diff] [blame] | 662 | .Case("cxx_unicode_literals", LangOpts.CPlusPlus0x) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 663 | //.Case("cxx_unrestricted_unions", false) |
| 664 | //.Case("cxx_user_literals", false) |
Douglas Gregor | c78e259 | 2011-01-26 15:36:03 +0000 | [diff] [blame] | 665 | .Case("cxx_variadic_templates", LangOpts.CPlusPlus0x) |
Douglas Gregor | afdf137 | 2011-02-03 21:57:35 +0000 | [diff] [blame] | 666 | // Type traits |
| 667 | .Case("has_nothrow_assign", LangOpts.CPlusPlus) |
| 668 | .Case("has_nothrow_copy", LangOpts.CPlusPlus) |
| 669 | .Case("has_nothrow_constructor", LangOpts.CPlusPlus) |
| 670 | .Case("has_trivial_assign", LangOpts.CPlusPlus) |
| 671 | .Case("has_trivial_copy", LangOpts.CPlusPlus) |
| 672 | .Case("has_trivial_constructor", LangOpts.CPlusPlus) |
| 673 | .Case("has_trivial_destructor", LangOpts.CPlusPlus) |
| 674 | .Case("has_virtual_destructor", LangOpts.CPlusPlus) |
| 675 | .Case("is_abstract", LangOpts.CPlusPlus) |
| 676 | .Case("is_base_of", LangOpts.CPlusPlus) |
| 677 | .Case("is_class", LangOpts.CPlusPlus) |
| 678 | .Case("is_convertible_to", LangOpts.CPlusPlus) |
Douglas Gregor | b3f8c24 | 2011-08-03 17:01:05 +0000 | [diff] [blame] | 679 | // __is_empty is available only if the horrible |
| 680 | // "struct __is_empty" parsing hack hasn't been needed in this |
| 681 | // translation unit. If it has, __is_empty reverts to a normal |
| 682 | // identifier and __has_feature(is_empty) evaluates false. |
Douglas Gregor | 6887614 | 2011-07-30 07:01:49 +0000 | [diff] [blame] | 683 | .Case("is_empty", |
Douglas Gregor | 9a14ecb | 2011-07-30 07:08:19 +0000 | [diff] [blame] | 684 | LangOpts.CPlusPlus && |
| 685 | PP.getIdentifierInfo("__is_empty")->getTokenID() |
| 686 | != tok::identifier) |
Douglas Gregor | afdf137 | 2011-02-03 21:57:35 +0000 | [diff] [blame] | 687 | .Case("is_enum", LangOpts.CPlusPlus) |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 688 | .Case("is_final", LangOpts.CPlusPlus) |
Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 689 | .Case("is_literal", LangOpts.CPlusPlus) |
Howard Hinnant | a55e68b | 2011-05-12 19:52:14 +0000 | [diff] [blame] | 690 | .Case("is_standard_layout", LangOpts.CPlusPlus) |
Douglas Gregor | b3f8c24 | 2011-08-03 17:01:05 +0000 | [diff] [blame] | 691 | // __is_pod is available only if the horrible |
| 692 | // "struct __is_pod" parsing hack hasn't been needed in this |
| 693 | // translation unit. If it has, __is_pod reverts to a normal |
| 694 | // identifier and __has_feature(is_pod) evaluates false. |
Douglas Gregor | 6887614 | 2011-07-30 07:01:49 +0000 | [diff] [blame] | 695 | .Case("is_pod", |
Douglas Gregor | 9a14ecb | 2011-07-30 07:08:19 +0000 | [diff] [blame] | 696 | LangOpts.CPlusPlus && |
| 697 | PP.getIdentifierInfo("__is_pod")->getTokenID() |
| 698 | != tok::identifier) |
Douglas Gregor | afdf137 | 2011-02-03 21:57:35 +0000 | [diff] [blame] | 699 | .Case("is_polymorphic", LangOpts.CPlusPlus) |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 700 | .Case("is_trivial", LangOpts.CPlusPlus) |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 701 | .Case("is_trivially_copyable", LangOpts.CPlusPlus) |
Douglas Gregor | afdf137 | 2011-02-03 21:57:35 +0000 | [diff] [blame] | 702 | .Case("is_union", LangOpts.CPlusPlus) |
Douglas Gregor | bd507c5 | 2012-01-04 21:16:09 +0000 | [diff] [blame] | 703 | .Case("modules", LangOpts.Modules) |
Eric Christopher | 1f84f8d | 2010-06-24 02:02:00 +0000 | [diff] [blame] | 704 | .Case("tls", PP.getTargetInfo().isTLSSupported()) |
Sean Hunt | 858a325 | 2011-07-18 17:08:00 +0000 | [diff] [blame] | 705 | .Case("underlying_type", LangOpts.CPlusPlus) |
Benjamin Kramer | 32592e8 | 2010-01-09 18:53:11 +0000 | [diff] [blame] | 706 | .Default(false); |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 709 | /// HasExtension - Return true if we recognize and implement the feature |
| 710 | /// specified by the identifier, either as an extension or a standard language |
| 711 | /// feature. |
| 712 | static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { |
| 713 | if (HasFeature(PP, II)) |
| 714 | return true; |
| 715 | |
| 716 | // If the use of an extension results in an error diagnostic, extensions are |
| 717 | // effectively unavailable, so just return false here. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 718 | if (PP.getDiagnostics().getExtensionHandlingBehavior() == |
| 719 | DiagnosticsEngine::Ext_Error) |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 720 | return false; |
| 721 | |
| 722 | const LangOptions &LangOpts = PP.getLangOptions(); |
| 723 | |
| 724 | // Because we inherit the feature list from HasFeature, this string switch |
| 725 | // must be less restrictive than HasFeature's. |
| 726 | return llvm::StringSwitch<bool>(II->getName()) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 727 | // C11 features supported by other languages as extensions. |
Peter Collingbourne | fd5f686 | 2011-10-14 23:44:46 +0000 | [diff] [blame] | 728 | .Case("c_alignas", true) |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame^] | 729 | .Case("c_atomic", true) |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 730 | .Case("c_generic_selections", true) |
| 731 | .Case("c_static_assert", true) |
| 732 | // C++0x features supported by other languages as extensions. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame^] | 733 | .Case("cxx_atomic", LangOpts.CPlusPlus) |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 734 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 735 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 736 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) |
Douglas Gregor | ece3894 | 2011-08-29 17:28:38 +0000 | [diff] [blame] | 737 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 738 | .Case("cxx_override_control", LangOpts.CPlusPlus) |
Richard Smith | 7640c00 | 2011-09-06 18:03:41 +0000 | [diff] [blame] | 739 | .Case("cxx_range_for", LangOpts.CPlusPlus) |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 740 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) |
| 741 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus) |
| 742 | .Default(false); |
| 743 | } |
| 744 | |
Anders Carlsson | cae5095 | 2010-10-20 02:31:43 +0000 | [diff] [blame] | 745 | /// HasAttribute - Return true if we recognize and implement the attribute |
| 746 | /// specified by the given identifier. |
| 747 | static bool HasAttribute(const IdentifierInfo *II) { |
| 748 | return llvm::StringSwitch<bool>(II->getName()) |
| 749 | #include "clang/Lex/AttrSpellings.inc" |
| 750 | .Default(false); |
| 751 | } |
| 752 | |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 753 | /// EvaluateHasIncludeCommon - Process a '__has_include("path")' |
| 754 | /// or '__has_include_next("path")' expression. |
| 755 | /// Returns true if successful. |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 756 | static bool EvaluateHasIncludeCommon(Token &Tok, |
| 757 | IdentifierInfo *II, Preprocessor &PP, |
| 758 | const DirectoryLookup *LookupFrom) { |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 759 | SourceLocation LParenLoc; |
| 760 | |
| 761 | // Get '('. |
| 762 | PP.LexNonComment(Tok); |
| 763 | |
| 764 | // Ensure we have a '('. |
| 765 | if (Tok.isNot(tok::l_paren)) { |
| 766 | PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName(); |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | // Save '(' location for possible missing ')' message. |
| 771 | LParenLoc = Tok.getLocation(); |
| 772 | |
| 773 | // Get the file name. |
| 774 | PP.getCurrentLexer()->LexIncludeFilename(Tok); |
| 775 | |
| 776 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 777 | llvm::SmallString<128> FilenameBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 778 | StringRef Filename; |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 779 | SourceLocation EndLoc; |
| 780 | |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 781 | switch (Tok.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 782 | case tok::eod: |
| 783 | // If the token kind is EOD, the error has already been diagnosed. |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 784 | return false; |
| 785 | |
| 786 | case tok::angle_string_literal: |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 787 | case tok::string_literal: { |
| 788 | bool Invalid = false; |
| 789 | Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); |
| 790 | if (Invalid) |
| 791 | return false; |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 792 | break; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 793 | } |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 794 | |
| 795 | case tok::less: |
| 796 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 797 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 798 | FilenameBuffer.push_back('<'); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 799 | if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 800 | return false; // Found <eod> but no ">"? Diagnostic already emitted. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 801 | Filename = FilenameBuffer.str(); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 802 | break; |
| 803 | default: |
| 804 | PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); |
| 805 | return false; |
| 806 | } |
| 807 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 808 | bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 809 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 810 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 811 | if (Filename.empty()) |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 812 | return false; |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 813 | |
| 814 | // Search include directories. |
| 815 | const DirectoryLookup *CurDir; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 816 | const FileEntry *File = |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 817 | PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 818 | |
| 819 | // Get the result value. Result = true means the file exists. |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 820 | bool Result = File != 0; |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 821 | |
| 822 | // Get ')'. |
| 823 | PP.LexNonComment(Tok); |
| 824 | |
| 825 | // Ensure we have a trailing ). |
| 826 | if (Tok.isNot(tok::r_paren)) { |
| 827 | PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName(); |
| 828 | PP.Diag(LParenLoc, diag::note_matching) << "("; |
| 829 | return false; |
| 830 | } |
| 831 | |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 832 | return Result; |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /// EvaluateHasInclude - Process a '__has_include("path")' expression. |
| 836 | /// Returns true if successful. |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 837 | static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 838 | Preprocessor &PP) { |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 839 | return EvaluateHasIncludeCommon(Tok, II, PP, NULL); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. |
| 843 | /// Returns true if successful. |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 844 | static bool EvaluateHasIncludeNext(Token &Tok, |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 845 | IdentifierInfo *II, Preprocessor &PP) { |
| 846 | // __has_include_next is like __has_include, except that we start |
| 847 | // searching after the current found directory. If we can't do this, |
| 848 | // issue a diagnostic. |
| 849 | const DirectoryLookup *Lookup = PP.GetCurDirLookup(); |
| 850 | if (PP.isInPrimaryFile()) { |
| 851 | Lookup = 0; |
| 852 | PP.Diag(Tok, diag::pp_include_next_in_primary); |
| 853 | } else if (Lookup == 0) { |
| 854 | PP.Diag(Tok, diag::pp_include_next_absolute_path); |
| 855 | } else { |
| 856 | // Start looking up in the next directory. |
| 857 | ++Lookup; |
| 858 | } |
| 859 | |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 860 | return EvaluateHasIncludeCommon(Tok, II, PP, Lookup); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 861 | } |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 862 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 863 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 864 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 865 | void Preprocessor::ExpandBuiltinMacro(Token &Tok) { |
| 866 | // Figure out which token this is. |
| 867 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 868 | assert(II && "Can't be a macro without id info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 870 | // If this is an _Pragma or Microsoft __pragma directive, expand it, |
| 871 | // invoke the pragma handler, then lex the token after it. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 872 | if (II == Ident_Pragma) |
| 873 | return Handle_Pragma(Tok); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 874 | else if (II == Ident__pragma) // in non-MS mode this is null |
| 875 | return HandleMicrosoft__pragma(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 877 | ++NumBuiltinMacroExpanded; |
| 878 | |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 879 | llvm::SmallString<128> TmpBuffer; |
| 880 | llvm::raw_svector_ostream OS(TmpBuffer); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 881 | |
| 882 | // Set up the return result. |
| 883 | Tok.setIdentifierInfo(0); |
| 884 | Tok.clearFlag(Token::NeedsCleaning); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 886 | if (II == Ident__LINE__) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 887 | // C99 6.10.8: "__LINE__: The presumed line number (within the current |
| 888 | // source file) of the current source line (an integer constant)". This can |
| 889 | // be affected by #line. |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 890 | SourceLocation Loc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Chris Lattner | dff070f | 2009-04-18 22:29:33 +0000 | [diff] [blame] | 892 | // Advance to the location of the first _, this might not be the first byte |
| 893 | // of the token if it starts with an escaped newline. |
| 894 | Loc = AdvanceToTokenCharacter(Loc, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 896 | // One wrinkle here is that GCC expands __LINE__ to location of the *end* of |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 897 | // a macro expansion. This doesn't matter for object-like macros, but |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 898 | // can matter for a function-like macro that expands to contain __LINE__. |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 899 | // Skip down through expansion points until we find a file loc for the |
| 900 | // end of the expansion history. |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 901 | Loc = SourceMgr.getExpansionRange(Loc).second; |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 902 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 904 | // __LINE__ expands to a simple numeric value. |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 905 | OS << (PLoc.isValid()? PLoc.getLine() : 1); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 906 | Tok.setKind(tok::numeric_constant); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 907 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 908 | // C99 6.10.8: "__FILE__: The presumed name of the current source file (a |
| 909 | // character string literal)". This can be affected by #line. |
| 910 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 911 | |
| 912 | // __BASE_FILE__ is a GNU extension that returns the top of the presumed |
| 913 | // #include stack instead of the current file. |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 914 | if (II == Ident__BASE_FILE__ && PLoc.isValid()) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 915 | SourceLocation NextLoc = PLoc.getIncludeLoc(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 916 | while (NextLoc.isValid()) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 917 | PLoc = SourceMgr.getPresumedLoc(NextLoc); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 918 | if (PLoc.isInvalid()) |
| 919 | break; |
| 920 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 921 | NextLoc = PLoc.getIncludeLoc(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 925 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 926 | llvm::SmallString<128> FN; |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 927 | if (PLoc.isValid()) { |
| 928 | FN += PLoc.getFilename(); |
| 929 | Lexer::Stringify(FN); |
| 930 | OS << '"' << FN.str() << '"'; |
| 931 | } |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 932 | Tok.setKind(tok::string_literal); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 933 | } else if (II == Ident__DATE__) { |
| 934 | if (!DATELoc.isValid()) |
| 935 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 936 | Tok.setKind(tok::string_literal); |
| 937 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 938 | Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), |
| 939 | Tok.getLocation(), |
| 940 | Tok.getLength())); |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 941 | return; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 942 | } else if (II == Ident__TIME__) { |
| 943 | if (!TIMELoc.isValid()) |
| 944 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 945 | Tok.setKind(tok::string_literal); |
| 946 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 947 | Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), |
| 948 | Tok.getLocation(), |
| 949 | Tok.getLength())); |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 950 | return; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 951 | } else if (II == Ident__INCLUDE_LEVEL__) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 952 | // Compute the presumed include depth of this token. This can be affected |
| 953 | // by GNU line markers. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 954 | unsigned Depth = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 956 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 957 | if (PLoc.isValid()) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 958 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 959 | for (; PLoc.isValid(); ++Depth) |
| 960 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 961 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 963 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 964 | OS << Depth; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 965 | Tok.setKind(tok::numeric_constant); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 966 | } else if (II == Ident__TIMESTAMP__) { |
| 967 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 968 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 969 | |
| 970 | // Get the file that we are lexing out of. If we're currently lexing from |
| 971 | // a macro, dig into the include stack. |
| 972 | const FileEntry *CurFile = 0; |
Ted Kremenek | a275a19 | 2008-11-20 01:35:24 +0000 | [diff] [blame] | 973 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 975 | if (TheLexer) |
Ted Kremenek | ac80c6e | 2008-11-19 22:55:25 +0000 | [diff] [blame] | 976 | CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 978 | const char *Result; |
| 979 | if (CurFile) { |
| 980 | time_t TT = CurFile->getModificationTime(); |
| 981 | struct tm *TM = localtime(&TT); |
| 982 | Result = asctime(TM); |
| 983 | } else { |
| 984 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 985 | } |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 986 | // Surround the string with " and strip the trailing newline. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 987 | OS << '"' << StringRef(Result, strlen(Result)-1) << '"'; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 988 | Tok.setKind(tok::string_literal); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 989 | } else if (II == Ident__COUNTER__) { |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 990 | // __COUNTER__ expands to a simple numeric value. |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 991 | OS << CounterValue++; |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 992 | Tok.setKind(tok::numeric_constant); |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 993 | } else if (II == Ident__has_feature || |
| 994 | II == Ident__has_extension || |
| 995 | II == Ident__has_builtin || |
Anders Carlsson | cae5095 | 2010-10-20 02:31:43 +0000 | [diff] [blame] | 996 | II == Ident__has_attribute) { |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 997 | // The argument to these builtins should be a parenthesized identifier. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 998 | SourceLocation StartLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1000 | bool IsValid = false; |
| 1001 | IdentifierInfo *FeatureII = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1003 | // Read the '('. |
| 1004 | Lex(Tok); |
| 1005 | if (Tok.is(tok::l_paren)) { |
| 1006 | // Read the identifier |
| 1007 | Lex(Tok); |
| 1008 | if (Tok.is(tok::identifier)) { |
| 1009 | FeatureII = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1011 | // Read the ')'. |
| 1012 | Lex(Tok); |
| 1013 | if (Tok.is(tok::r_paren)) |
| 1014 | IsValid = true; |
| 1015 | } |
| 1016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1018 | bool Value = false; |
| 1019 | if (!IsValid) |
| 1020 | Diag(StartLoc, diag::err_feature_check_malformed); |
| 1021 | else if (II == Ident__has_builtin) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | // Check for a builtin is trivial. |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1023 | Value = FeatureII->getBuiltinID() != 0; |
Anders Carlsson | cae5095 | 2010-10-20 02:31:43 +0000 | [diff] [blame] | 1024 | } else if (II == Ident__has_attribute) |
| 1025 | Value = HasAttribute(FeatureII); |
Peter Collingbourne | c1b5fa4 | 2011-05-13 20:54:45 +0000 | [diff] [blame] | 1026 | else if (II == Ident__has_extension) |
| 1027 | Value = HasExtension(*this, FeatureII); |
Anders Carlsson | cae5095 | 2010-10-20 02:31:43 +0000 | [diff] [blame] | 1028 | else { |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1029 | assert(II == Ident__has_feature && "Must be feature check"); |
| 1030 | Value = HasFeature(*this, FeatureII); |
| 1031 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 1033 | OS << (int)Value; |
Chris Lattner | 148772a | 2009-06-13 07:13:28 +0000 | [diff] [blame] | 1034 | Tok.setKind(tok::numeric_constant); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 1035 | } else if (II == Ident__has_include || |
| 1036 | II == Ident__has_include_next) { |
| 1037 | // The argument to these two builtins should be a parenthesized |
| 1038 | // file name string literal using angle brackets (<>) or |
| 1039 | // double-quotes (""). |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 1040 | bool Value; |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 1041 | if (II == Ident__has_include) |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 1042 | Value = EvaluateHasInclude(Tok, II, *this); |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 1043 | else |
Chris Lattner | 3ed572e | 2011-01-15 06:57:04 +0000 | [diff] [blame] | 1044 | Value = EvaluateHasIncludeNext(Tok, II, *this); |
Benjamin Kramer | b176591 | 2010-01-27 16:38:22 +0000 | [diff] [blame] | 1045 | OS << (int)Value; |
John Thompson | 92bd8c7 | 2009-11-02 22:28:12 +0000 | [diff] [blame] | 1046 | Tok.setKind(tok::numeric_constant); |
Ted Kremenek | d768150 | 2011-10-12 19:46:30 +0000 | [diff] [blame] | 1047 | } else if (II == Ident__has_warning) { |
| 1048 | // The argument should be a parenthesized string literal. |
| 1049 | // The argument to these builtins should be a parenthesized identifier. |
| 1050 | SourceLocation StartLoc = Tok.getLocation(); |
| 1051 | bool IsValid = false; |
| 1052 | bool Value = false; |
| 1053 | // Read the '('. |
| 1054 | Lex(Tok); |
| 1055 | do { |
| 1056 | if (Tok.is(tok::l_paren)) { |
| 1057 | // Read the string. |
| 1058 | Lex(Tok); |
| 1059 | |
| 1060 | // We need at least one string literal. |
| 1061 | if (!Tok.is(tok::string_literal)) { |
| 1062 | StartLoc = Tok.getLocation(); |
| 1063 | IsValid = false; |
| 1064 | // Eat tokens until ')'. |
| 1065 | do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod))); |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | // String concatenation allows multiple strings, which can even come |
| 1070 | // from macro expansion. |
| 1071 | SmallVector<Token, 4> StrToks; |
| 1072 | while (Tok.is(tok::string_literal)) { |
| 1073 | StrToks.push_back(Tok); |
| 1074 | LexUnexpandedToken(Tok); |
| 1075 | } |
| 1076 | |
| 1077 | // Is the end a ')'? |
| 1078 | if (!(IsValid = Tok.is(tok::r_paren))) |
| 1079 | break; |
| 1080 | |
| 1081 | // Concatenate and parse the strings. |
| 1082 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 1083 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
| 1084 | if (Literal.hadError) |
| 1085 | break; |
| 1086 | if (Literal.Pascal) { |
| 1087 | Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
| 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | StringRef WarningName(Literal.GetString()); |
| 1092 | |
| 1093 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 1094 | WarningName[1] != 'W') { |
| 1095 | Diag(StrToks[0].getLocation(), diag::warn_has_warning_invalid_option); |
| 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | // Finally, check if the warning flags maps to a diagnostic group. |
| 1100 | // We construct a SmallVector here to talk to getDiagnosticIDs(). |
| 1101 | // Although we don't use the result, this isn't a hot path, and not |
| 1102 | // worth special casing. |
| 1103 | llvm::SmallVector<diag::kind, 10> Diags; |
| 1104 | Value = !getDiagnostics().getDiagnosticIDs()-> |
| 1105 | getDiagnosticsInGroup(WarningName.substr(2), Diags); |
| 1106 | } |
| 1107 | } while (false); |
| 1108 | |
| 1109 | if (!IsValid) |
| 1110 | Diag(StartLoc, diag::err_warning_check_malformed); |
| 1111 | |
| 1112 | OS << (int)Value; |
| 1113 | Tok.setKind(tok::numeric_constant); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1114 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1115 | llvm_unreachable("Unknown identifier!"); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1116 | } |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 1117 | CreateString(OS.str().data(), OS.str().size(), Tok, |
| 1118 | Tok.getLocation(), Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1119 | } |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1120 | |
| 1121 | void Preprocessor::markMacroAsUsed(MacroInfo *MI) { |
| 1122 | // If the 'used' status changed, and the macro requires 'unused' warning, |
| 1123 | // remove its SourceLocation from the warn-for-unused-macro locations. |
| 1124 | if (MI->isWarnIfUnused() && !MI->isUsed()) |
| 1125 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 1126 | MI->setIsUsed(true); |
| 1127 | } |