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