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