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