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