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