Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 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 # directive processing for the Preprocessor. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/LiteralSupport.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 19 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Pragma.h" |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Utility Methods for Preprocessor Directive Handling. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 30 | MacroInfo *Preprocessor::AllocateMacroInfo() { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 31 | MacroInfoChain *MIChain; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 33 | if (MICache) { |
| 34 | MIChain = MICache; |
| 35 | MICache = MICache->Next; |
Ted Kremenek | af8fa25 | 2010-10-19 18:16:54 +0000 | [diff] [blame] | 36 | } |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 37 | else { |
| 38 | MIChain = BP.Allocate<MacroInfoChain>(); |
| 39 | } |
| 40 | |
| 41 | MIChain->Next = MIChainHead; |
| 42 | MIChain->Prev = 0; |
| 43 | if (MIChainHead) |
| 44 | MIChainHead->Prev = MIChain; |
| 45 | MIChainHead = MIChain; |
| 46 | |
| 47 | return &(MIChain->MI); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
| 51 | MacroInfo *MI = AllocateMacroInfo(); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 52 | new (MI) MacroInfo(L); |
| 53 | return MI; |
| 54 | } |
| 55 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 56 | MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) { |
| 57 | MacroInfo *MI = AllocateMacroInfo(); |
| 58 | new (MI) MacroInfo(MacroToClone, BP); |
| 59 | return MI; |
| 60 | } |
| 61 | |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 62 | /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will |
| 63 | /// be reused for allocating new MacroInfo objects. |
Chris Lattner | 2c1ab90 | 2010-08-18 16:08:51 +0000 | [diff] [blame] | 64 | void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 65 | MacroInfoChain *MIChain = (MacroInfoChain*) MI; |
| 66 | if (MacroInfoChain *Prev = MIChain->Prev) { |
| 67 | MacroInfoChain *Next = MIChain->Next; |
| 68 | Prev->Next = Next; |
| 69 | if (Next) |
| 70 | Next->Prev = Prev; |
| 71 | } |
| 72 | else { |
| 73 | assert(MIChainHead == MIChain); |
| 74 | MIChainHead = MIChain->Next; |
| 75 | MIChainHead->Prev = 0; |
| 76 | } |
| 77 | MIChain->Next = MICache; |
| 78 | MICache = MIChain; |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 80 | MI->Destroy(); |
| 81 | } |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 83 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 84 | /// current line until the tok::eod token is found. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 85 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 86 | Token Tmp; |
| 87 | do { |
| 88 | LexUnexpandedToken(Tmp); |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 89 | assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 90 | } while (Tmp.isNot(tok::eod)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 93 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 94 | /// #define or #undef. This sets the token kind to eod and discards the rest |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 95 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 96 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
| 97 | /// else (e.g. #ifdef). |
| 98 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { |
| 99 | // Read the token, don't allow macro expansion on it. |
| 100 | LexUnexpandedToken(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 102 | if (MacroNameTok.is(tok::code_completion)) { |
| 103 | if (CodeComplete) |
| 104 | CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); |
| 105 | LexUnexpandedToken(MacroNameTok); |
| 106 | return; |
| 107 | } |
| 108 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 109 | // Missing macro name? |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 110 | if (MacroNameTok.is(tok::eod)) { |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 111 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 112 | return; |
| 113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 115 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 116 | if (II == 0) { |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 117 | bool Invalid = false; |
| 118 | std::string Spelling = getSpelling(MacroNameTok, &Invalid); |
| 119 | if (Invalid) |
| 120 | return; |
| 121 | |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 122 | const IdentifierInfo &Info = Identifiers.get(Spelling); |
| 123 | if (Info.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 124 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 125 | // except for their spellings. |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 126 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 127 | else |
| 128 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 129 | // Fall through on error. |
| 130 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
| 131 | // Error if defining "defined": C99 6.10.8.4. |
| 132 | Diag(MacroNameTok, diag::err_defined_macro_name); |
| 133 | } else if (isDefineUndef && II->hasMacroDefinition() && |
| 134 | getMacroInfo(II)->isBuiltinMacro()) { |
| 135 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
| 136 | if (isDefineUndef == 1) |
| 137 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 138 | else |
| 139 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
| 140 | } else { |
| 141 | // Okay, we got a good identifier node. Return it. |
| 142 | return; |
| 143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 145 | // Invalid macro name, read and discard the rest of the line. Then set the |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 146 | // token kind to tok::eod. |
| 147 | MacroNameTok.setKind(tok::eod); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 148 | return DiscardUntilEndOfDirective(); |
| 149 | } |
| 150 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 151 | /// CheckEndOfDirective - Ensure that the next token is a tok::eod token. If |
| 152 | /// not, emit a diagnostic and consume up until the eod. If EnableMacros is |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 153 | /// true, then we consider macros that expand to zero tokens as being ok. |
| 154 | void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 155 | Token Tmp; |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 156 | // Lex unexpanded tokens for most directives: macros might expand to zero |
| 157 | // tokens, causing us to miss diagnosing invalid lines. Some directives (like |
| 158 | // #line) allow empty macros. |
| 159 | if (EnableMacros) |
| 160 | Lex(Tmp); |
| 161 | else |
| 162 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 164 | // There should be no tokens after the directive, but we allow them as an |
| 165 | // extension. |
| 166 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
| 167 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 169 | if (Tmp.isNot(tok::eod)) { |
Chris Lattner | 959875a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 170 | // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 171 | // or if this is a macro-style preprocessing directive, because it is more |
| 172 | // trouble than it is worth to insert /**/ and check that there is no /**/ |
| 173 | // in the range also. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 174 | FixItHint Hint; |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 175 | if ((Features.GNUMode || Features.C99 || Features.CPlusPlus) && |
| 176 | !CurTokenLexer) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 177 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); |
| 178 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 179 | DiscardUntilEndOfDirective(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 186 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 187 | /// file. Lex the rest of the file, until we see an #endif. If |
| 188 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 189 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 190 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 191 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 192 | /// the first valid token. |
| 193 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 194 | bool FoundNonSkipPortion, |
| 195 | bool FoundElse) { |
| 196 | ++NumSkipped; |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 197 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 199 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 200 | FoundNonSkipPortion, FoundElse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 202 | if (CurPTHLexer) { |
| 203 | PTHSkipExcludedConditionalBlock(); |
| 204 | return; |
| 205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 207 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 208 | // disabling warnings, etc. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 209 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 210 | Token Tok; |
| 211 | while (1) { |
Chris Lattner | 2c6b193 | 2010-01-18 22:33:01 +0000 | [diff] [blame] | 212 | CurLexer->Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 214 | if (Tok.is(tok::code_completion)) { |
| 215 | if (CodeComplete) |
| 216 | CodeComplete->CodeCompleteInConditionalExclusion(); |
| 217 | continue; |
| 218 | } |
| 219 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 220 | // If this is the end of the buffer, we have an error. |
| 221 | if (Tok.is(tok::eof)) { |
| 222 | // Emit errors for each unterminated conditional on the stack, including |
| 223 | // the current one. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 224 | while (!CurPPLexer->ConditionalStack.empty()) { |
Douglas Gregor | 2d474ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 225 | if (!isCodeCompletionFile(Tok.getLocation())) |
| 226 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, |
| 227 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 228 | CurPPLexer->ConditionalStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 231 | // Just return and let the caller lex after this #include. |
| 232 | break; |
| 233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 235 | // If this token is not a preprocessor directive, just skip it. |
| 236 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
| 237 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 239 | // We just parsed a # character at the start of a line, so we're in |
| 240 | // directive mode. Tell the lexer this so any newlines we see will be |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 241 | // converted into an EOD token (this terminates the macro). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 242 | CurPPLexer->ParsingPreprocessorDirective = true; |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 243 | if (CurLexer) CurLexer->SetCommentRetentionState(false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 244 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 246 | // Read the next token, the directive flavor. |
| 247 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 249 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 250 | // something bogus), skip it. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 251 | if (Tok.isNot(tok::raw_identifier)) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 252 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 253 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 254 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 255 | continue; |
| 256 | } |
| 257 | |
| 258 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 259 | // this is safe in the face of spelling differences, because there is no way |
| 260 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 261 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 262 | // other common directives. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 263 | const char *RawCharData = Tok.getRawIdentifierData(); |
| 264 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 265 | char FirstChar = RawCharData[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 267 | FirstChar != 'i' && FirstChar != 'e') { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 268 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 269 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 270 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 271 | continue; |
| 272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 274 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 275 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 276 | // when skipping. |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 277 | char DirectiveBuf[20]; |
| 278 | llvm::StringRef Directive; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 279 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 280 | Directive = llvm::StringRef(RawCharData, Tok.getLength()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 281 | } else { |
| 282 | std::string DirectiveStr = getSpelling(Tok); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 283 | unsigned IdLen = DirectiveStr.size(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 284 | if (IdLen >= 20) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 285 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 286 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 287 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 288 | continue; |
| 289 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 290 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
| 291 | Directive = llvm::StringRef(DirectiveBuf, IdLen); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 292 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 294 | if (Directive.startswith("if")) { |
| 295 | llvm::StringRef Sub = Directive.substr(2); |
| 296 | if (Sub.empty() || // "if" |
| 297 | Sub == "def" || // "ifdef" |
| 298 | Sub == "ndef") { // "ifndef" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 299 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 300 | // bother parsing the condition. |
| 301 | DiscardUntilEndOfDirective(); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 302 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 303 | /*foundnonskip*/false, |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 304 | /*foundelse*/false); |
| 305 | |
| 306 | if (Callbacks) |
| 307 | Callbacks->Endif(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 308 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 309 | } else if (Directive[0] == 'e') { |
| 310 | llvm::StringRef Sub = Directive.substr(1); |
| 311 | if (Sub == "ndif") { // "endif" |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 312 | CheckEndOfDirective("endif"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 313 | PPConditionalInfo CondInfo; |
| 314 | CondInfo.WasSkipping = true; // Silence bogus warning. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 315 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 316 | (void)InCond; // Silence warning in no-asserts mode. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 317 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 319 | // If we popped the outermost skipping block, we're done skipping! |
| 320 | if (!CondInfo.WasSkipping) |
| 321 | break; |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 322 | } else if (Sub == "lse") { // "else". |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 323 | // #else directive in a skipping conditional. If not in some other |
| 324 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 325 | // as a non-skipping conditional. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 326 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 328 | // If this is a #else with a #else before it, report the error. |
| 329 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 331 | // Note that we've seen a #else in this conditional. |
| 332 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 334 | if (Callbacks) |
| 335 | Callbacks->Else(); |
| 336 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 337 | // If the conditional is at the top level, and the #if block wasn't |
| 338 | // entered, enter the #else block now. |
| 339 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 340 | CondInfo.FoundNonSkip = true; |
Argyrios Kyrtzidis | e26224e | 2011-05-21 04:26:04 +0000 | [diff] [blame] | 341 | CheckEndOfDirective("else"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 342 | break; |
Argyrios Kyrtzidis | e26224e | 2011-05-21 04:26:04 +0000 | [diff] [blame] | 343 | } else { |
| 344 | DiscardUntilEndOfDirective(); // C99 6.10p4. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 345 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 346 | } else if (Sub == "lif") { // "elif". |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 347 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 348 | |
| 349 | bool ShouldEnter; |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 350 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 351 | // If this is in a skipping block or if we're already handled this #if |
| 352 | // block, don't bother parsing the condition. |
| 353 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 354 | DiscardUntilEndOfDirective(); |
| 355 | ShouldEnter = false; |
| 356 | } else { |
| 357 | // Restore the value of LexingRawMode so that identifiers are |
| 358 | // looked up, etc, inside the #elif expression. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 359 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
| 360 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 361 | IdentifierInfo *IfNDefMacro = 0; |
| 362 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 363 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 364 | } |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 365 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 367 | // If this is a #elif with a #else before it, report the error. |
| 368 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 370 | if (Callbacks) |
| 371 | Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd)); |
| 372 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 373 | // If this condition is true, enter it! |
| 374 | if (ShouldEnter) { |
| 375 | CondInfo.FoundNonSkip = true; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 381 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 382 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 383 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 387 | // of the file, just stop skipping and return to lexing whatever came after |
| 388 | // the #if block. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 389 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 392 | void Preprocessor::PTHSkipExcludedConditionalBlock() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
| 394 | while (1) { |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 395 | assert(CurPTHLexer); |
| 396 | assert(CurPTHLexer->LexingRawMode == false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 398 | // Skip to the next '#else', '#elif', or #endif. |
| 399 | if (CurPTHLexer->SkipBlock()) { |
| 400 | // We have reached an #endif. Both the '#' and 'endif' tokens |
| 401 | // have been consumed by the PTHLexer. Just pop off the condition level. |
| 402 | PPConditionalInfo CondInfo; |
| 403 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 404 | (void)InCond; // Silence warning in no-asserts mode. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 405 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 406 | break; |
| 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 409 | // We have reached a '#else' or '#elif'. Lex the next token to get |
| 410 | // the directive flavor. |
| 411 | Token Tok; |
| 412 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 414 | // We can actually look up the IdentifierInfo here since we aren't in |
| 415 | // raw mode. |
| 416 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); |
| 417 | |
| 418 | if (K == tok::pp_else) { |
| 419 | // #else: Enter the else condition. We aren't in a nested condition |
| 420 | // since we skip those. We're always in the one matching the last |
| 421 | // blocked we skipped. |
| 422 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 423 | // Note that we've seen a #else in this conditional. |
| 424 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 426 | // If the #if block wasn't entered then enter the #else block now. |
| 427 | if (!CondInfo.FoundNonSkip) { |
| 428 | CondInfo.FoundNonSkip = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 430 | // Scan until the eod token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 431 | CurPTHLexer->ParsingPreprocessorDirective = true; |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 432 | DiscardUntilEndOfDirective(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 433 | CurPTHLexer->ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 435 | break; |
| 436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 438 | // Otherwise skip this block. |
| 439 | continue; |
| 440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 442 | assert(K == tok::pp_elif); |
| 443 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 444 | |
| 445 | // If this is a #elif with a #else before it, report the error. |
| 446 | if (CondInfo.FoundElse) |
| 447 | Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 449 | // If this is in a skipping block or if we're already handled this #if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | // block, don't bother parsing the condition. We just skip this block. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 451 | if (CondInfo.FoundNonSkip) |
| 452 | continue; |
| 453 | |
| 454 | // Evaluate the condition of the #elif. |
| 455 | IdentifierInfo *IfNDefMacro = 0; |
| 456 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 457 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 458 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 459 | |
| 460 | // If this condition is true, enter it! |
| 461 | if (ShouldEnter) { |
| 462 | CondInfo.FoundNonSkip = true; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | // Otherwise, skip this block and go to the next one. |
| 467 | continue; |
| 468 | } |
| 469 | } |
| 470 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 471 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 472 | /// return null on failure. isAngled indicates whether the file reference is |
| 473 | /// for system #include's or not (i.e. using <> instead of ""). |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 474 | const FileEntry *Preprocessor::LookupFile( |
| 475 | llvm::StringRef Filename, |
| 476 | bool isAngled, |
| 477 | const DirectoryLookup *FromDir, |
| 478 | const DirectoryLookup *&CurDir, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 479 | llvm::SmallVectorImpl<char> *SearchPath, |
| 480 | llvm::SmallVectorImpl<char> *RelativePath) { |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 481 | // If the header lookup mechanism may be relative to the current file, pass in |
| 482 | // info about where the current file is. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 483 | const FileEntry *CurFileEnt = 0; |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 484 | if (!FromDir) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 485 | FileID FID = getCurrentFileLexer()->getFileID(); |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 486 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 488 | // If there is no file entry associated with this file, it must be the |
| 489 | // predefines buffer. Any other file is not lexed with a normal lexer, so |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 490 | // it won't be scanned for preprocessor directives. If we have the |
| 491 | // predefines buffer, resolve #include references (which come from the |
| 492 | // -include command line argument) as if they came from the main file, this |
| 493 | // affects file lookup etc. |
| 494 | if (CurFileEnt == 0) { |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 495 | FID = SourceMgr.getMainFileID(); |
| 496 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
| 497 | } |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 500 | // Do a standard file entry lookup. |
| 501 | CurDir = CurDirLookup; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 502 | const FileEntry *FE = HeaderInfo.LookupFile( |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 503 | Filename, isAngled, FromDir, CurDir, CurFileEnt, |
| 504 | SearchPath, RelativePath); |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 505 | if (FE) return FE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 507 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 508 | // to one of the headers on the #include stack. Walk the list of the current |
| 509 | // headers on the #include stack and pass them to HeaderInfo. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 510 | if (IsFileLexer()) { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 511 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 512 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 513 | SearchPath, RelativePath))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 514 | return FE; |
| 515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 517 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 518 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 519 | if (IsFileLexer(ISEntry)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | if ((CurFileEnt = |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 521 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 522 | if ((FE = HeaderInfo.LookupSubframeworkHeader( |
| 523 | Filename, CurFileEnt, SearchPath, RelativePath))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 524 | return FE; |
| 525 | } |
| 526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 528 | // Otherwise, we really couldn't find the file. |
| 529 | return 0; |
| 530 | } |
| 531 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 532 | |
| 533 | //===----------------------------------------------------------------------===// |
| 534 | // Preprocessor Directive Handling. |
| 535 | //===----------------------------------------------------------------------===// |
| 536 | |
| 537 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | /// at the start of a line. This consumes the directive, modifies the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 539 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 540 | /// read is the correct one. |
| 541 | void Preprocessor::HandleDirective(Token &Result) { |
| 542 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 544 | // We just parsed a # character at the start of a line, so we're in directive |
| 545 | // mode. Tell the lexer this so any newlines we see will be converted into an |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 546 | // EOD token (which terminates the directive). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 547 | CurPPLexer->ParsingPreprocessorDirective = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 549 | ++NumDirectives; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 551 | // We are about to read a token. For the multiple-include optimization FA to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | // work, we have to remember if we had read any tokens *before* this |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 553 | // pp-directive. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 554 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 556 | // Save the '#' token in case we need to return it later. |
| 557 | Token SavedHash = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 559 | // Read the next token, the directive flavor. This isn't expanded due to |
| 560 | // C99 6.10.3p8. |
| 561 | LexUnexpandedToken(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 563 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 564 | // #define A(x) #x |
| 565 | // A(abc |
| 566 | // #warning blah |
| 567 | // def) |
| 568 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 569 | if (InMacroArgs) |
| 570 | Diag(Result, diag::ext_embedded_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 572 | TryAgain: |
| 573 | switch (Result.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 574 | case tok::eod: |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 575 | return; // null directive. |
| 576 | case tok::comment: |
| 577 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 578 | LexUnexpandedToken(Result); |
| 579 | goto TryAgain; |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 580 | case tok::code_completion: |
| 581 | if (CodeComplete) |
| 582 | CodeComplete->CodeCompleteDirective( |
| 583 | CurPPLexer->getConditionalStackDepth() > 0); |
| 584 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 585 | case tok::numeric_constant: // # 7 GNU line marker directive. |
Chris Lattner | 5f607c4 | 2009-03-18 20:41:10 +0000 | [diff] [blame] | 586 | if (getLangOptions().AsmPreprocessor) |
| 587 | break; // # 4 is not a preprocessor directive in .S files. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 588 | return HandleDigitDirective(Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 589 | default: |
| 590 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 591 | if (II == 0) break; // Not an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 593 | // Ask what the preprocessor keyword ID is. |
| 594 | switch (II->getPPKeywordID()) { |
| 595 | default: break; |
| 596 | // C99 6.10.1 - Conditional Inclusion. |
| 597 | case tok::pp_if: |
| 598 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 599 | case tok::pp_ifdef: |
| 600 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 601 | case tok::pp_ifndef: |
| 602 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 603 | case tok::pp_elif: |
| 604 | return HandleElifDirective(Result); |
| 605 | case tok::pp_else: |
| 606 | return HandleElseDirective(Result); |
| 607 | case tok::pp_endif: |
| 608 | return HandleEndifDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 610 | // C99 6.10.2 - Source File Inclusion. |
| 611 | case tok::pp_include: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 612 | // Handle #include. |
| 613 | return HandleIncludeDirective(SavedHash.getLocation(), Result); |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 614 | case tok::pp___include_macros: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 615 | // Handle -imacros. |
| 616 | return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 618 | // C99 6.10.3 - Macro Replacement. |
| 619 | case tok::pp_define: |
| 620 | return HandleDefineDirective(Result); |
| 621 | case tok::pp_undef: |
| 622 | return HandleUndefDirective(Result); |
| 623 | |
| 624 | // C99 6.10.4 - Line Control. |
| 625 | case tok::pp_line: |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 626 | return HandleLineDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 628 | // C99 6.10.5 - Error Directive. |
| 629 | case tok::pp_error: |
| 630 | return HandleUserDiagnosticDirective(Result, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 632 | // C99 6.10.6 - Pragma Directive. |
| 633 | case tok::pp_pragma: |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 634 | return HandlePragmaDirective(PIK_HashPragma); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 636 | // GNU Extensions. |
| 637 | case tok::pp_import: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 638 | return HandleImportDirective(SavedHash.getLocation(), Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 639 | case tok::pp_include_next: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 640 | return HandleIncludeNextDirective(SavedHash.getLocation(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 642 | case tok::pp_warning: |
| 643 | Diag(Result, diag::ext_pp_warning_directive); |
| 644 | return HandleUserDiagnosticDirective(Result, true); |
| 645 | case tok::pp_ident: |
| 646 | return HandleIdentSCCSDirective(Result); |
| 647 | case tok::pp_sccs: |
| 648 | return HandleIdentSCCSDirective(Result); |
| 649 | case tok::pp_assert: |
| 650 | //isExtension = true; // FIXME: implement #assert |
| 651 | break; |
| 652 | case tok::pp_unassert: |
| 653 | //isExtension = true; // FIXME: implement #unassert |
| 654 | break; |
| 655 | } |
| 656 | break; |
| 657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 659 | // If this is a .S file, treat unknown # directives as non-preprocessor |
| 660 | // directives. This is important because # may be a comment or introduce |
| 661 | // various pseudo-ops. Just return the # token and push back the following |
| 662 | // token to be lexed next time. |
| 663 | if (getLangOptions().AsmPreprocessor) { |
Daniel Dunbar | 3d399a0 | 2009-07-13 21:48:50 +0000 | [diff] [blame] | 664 | Token *Toks = new Token[2]; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 665 | // Return the # and the token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | Toks[0] = SavedHash; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 667 | Toks[1] = Result; |
Chris Lattner | ba3ca52 | 2011-01-06 05:01:51 +0000 | [diff] [blame] | 668 | |
| 669 | // If the second token is a hashhash token, then we need to translate it to |
| 670 | // unknown so the token lexer doesn't try to perform token pasting. |
| 671 | if (Result.is(tok::hashhash)) |
| 672 | Toks[1].setKind(tok::unknown); |
| 673 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 674 | // Enter this token stream so that we re-lex the tokens. Make sure to |
| 675 | // enable macro expansion, in case the token after the # is an identifier |
| 676 | // that is expanded. |
| 677 | EnterTokenStream(Toks, 2, false, true); |
| 678 | return; |
| 679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 681 | // If we reached here, the preprocessing token is not valid! |
| 682 | Diag(Result, diag::err_pp_invalid_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 684 | // Read the rest of the PP line. |
| 685 | DiscardUntilEndOfDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 687 | // Okay, we're done parsing the directive. |
| 688 | } |
| 689 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 690 | /// GetLineValue - Convert a numeric token into an unsigned value, emitting |
| 691 | /// Diagnostic DiagID if it is invalid, and returning the value in Val. |
| 692 | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
| 693 | unsigned DiagID, Preprocessor &PP) { |
| 694 | if (DigitTok.isNot(tok::numeric_constant)) { |
| 695 | PP.Diag(DigitTok, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 697 | if (DigitTok.isNot(tok::eod)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 698 | PP.DiscardUntilEndOfDirective(); |
| 699 | return true; |
| 700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 702 | llvm::SmallString<64> IntegerBuffer; |
| 703 | IntegerBuffer.resize(DigitTok.getLength()); |
| 704 | const char *DigitTokBegin = &IntegerBuffer[0]; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 705 | bool Invalid = false; |
| 706 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); |
| 707 | if (Invalid) |
| 708 | return true; |
| 709 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 710 | // Verify that we have a simple digit-sequence, and compute the value. This |
| 711 | // is always a simple digit string computed in decimal, so we do this manually |
| 712 | // here. |
| 713 | Val = 0; |
| 714 | for (unsigned i = 0; i != ActualLength; ++i) { |
| 715 | if (!isdigit(DigitTokBegin[i])) { |
| 716 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
| 717 | diag::err_pp_line_digit_sequence); |
| 718 | PP.DiscardUntilEndOfDirective(); |
| 719 | return true; |
| 720 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 722 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
| 723 | if (NextVal < Val) { // overflow. |
| 724 | PP.Diag(DigitTok, DiagID); |
| 725 | PP.DiscardUntilEndOfDirective(); |
| 726 | return true; |
| 727 | } |
| 728 | Val = NextVal; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
| 731 | // Reject 0, this is needed both by #line numbers and flags. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 732 | if (Val == 0) { |
| 733 | PP.Diag(DigitTok, DiagID); |
| 734 | PP.DiscardUntilEndOfDirective(); |
| 735 | return true; |
| 736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 738 | if (DigitTokBegin[0] == '0') |
| 739 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | /// HandleLineDirective - Handle #line directive: C99 6.10.4. The two |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 745 | /// acceptable forms are: |
| 746 | /// # line digit-sequence |
| 747 | /// # line digit-sequence "s-char-sequence" |
| 748 | void Preprocessor::HandleLineDirective(Token &Tok) { |
| 749 | // Read the line # and string argument. Per C99 6.10.4p5, these tokens are |
| 750 | // expanded. |
| 751 | Token DigitTok; |
| 752 | Lex(DigitTok); |
| 753 | |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 754 | // Validate the number and convert it to an unsigned. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 755 | unsigned LineNo; |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 756 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 757 | return; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 759 | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a |
| 760 | // number greater than 2147483647". C90 requires that the line # be <= 32767. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 761 | unsigned LineLimit = Features.C99 ? 2147483648U : 32768U; |
| 762 | if (LineNo >= LineLimit) |
| 763 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 765 | int FilenameID = -1; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 766 | Token StrTok; |
| 767 | Lex(StrTok); |
| 768 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 769 | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
| 770 | // string followed by eod. |
| 771 | if (StrTok.is(tok::eod)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 772 | ; // ok |
| 773 | else if (StrTok.isNot(tok::string_literal)) { |
| 774 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
| 775 | DiscardUntilEndOfDirective(); |
| 776 | return; |
| 777 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 778 | // Parse and validate the string, converting it into a unique ID. |
| 779 | StringLiteralParser Literal(&StrTok, 1, *this); |
| 780 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 781 | if (Literal.hadError) |
| 782 | return DiscardUntilEndOfDirective(); |
| 783 | if (Literal.Pascal) { |
| 784 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 785 | return DiscardUntilEndOfDirective(); |
| 786 | } |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 787 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 789 | // Verify that there is nothing after the string, other than EOD. Because |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 790 | // of C99 6.10.4p5, macros that expand to empty tokens are ok. |
| 791 | CheckEndOfDirective("line", true); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 794 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 796 | if (Callbacks) |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 797 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
| 798 | PPCallbacks::RenameFile, |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 799 | SrcMgr::C_User); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 802 | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line |
| 803 | /// marker directive. |
| 804 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
| 805 | bool &IsSystemHeader, bool &IsExternCHeader, |
| 806 | Preprocessor &PP) { |
| 807 | unsigned FlagVal; |
| 808 | Token FlagTok; |
| 809 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 810 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 811 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
| 812 | return true; |
| 813 | |
| 814 | if (FlagVal == 1) { |
| 815 | IsFileEntry = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 817 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 818 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 819 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 820 | return true; |
| 821 | } else if (FlagVal == 2) { |
| 822 | IsFileExit = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 824 | SourceManager &SM = PP.getSourceManager(); |
| 825 | // If we are leaving the current presumed file, check to make sure the |
| 826 | // presumed include stack isn't empty! |
| 827 | FileID CurFileID = |
| 828 | SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first; |
| 829 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 830 | if (PLoc.isInvalid()) |
| 831 | return true; |
| 832 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 833 | // If there is no include loc (main file) or if the include loc is in a |
| 834 | // different physical file, then we aren't in a "1" line marker flag region. |
| 835 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
| 836 | if (IncLoc.isInvalid() || |
| 837 | SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) { |
| 838 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
| 839 | PP.DiscardUntilEndOfDirective(); |
| 840 | return true; |
| 841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 843 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 844 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 845 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | // We must have 3 if there are still flags. |
| 850 | if (FlagVal != 3) { |
| 851 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 852 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 853 | return true; |
| 854 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 856 | IsSystemHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 858 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 859 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 860 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 861 | return true; |
| 862 | |
| 863 | // We must have 4 if there is yet another flag. |
| 864 | if (FlagVal != 4) { |
| 865 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 866 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 867 | return true; |
| 868 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 870 | IsExternCHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 872 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 873 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 874 | |
| 875 | // There are no more valid flags here. |
| 876 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 877 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 878 | return true; |
| 879 | } |
| 880 | |
| 881 | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is |
| 882 | /// one of the following forms: |
| 883 | /// |
| 884 | /// # 42 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | /// # 42 "file" ('1' | '2')? |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 886 | /// # 42 "file" ('1' | '2')? '3' '4'? |
| 887 | /// |
| 888 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
| 889 | // Validate the number and convert it to an unsigned. GNU does not have a |
| 890 | // line # limit other than it fit in 32-bits. |
| 891 | unsigned LineNo; |
| 892 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
| 893 | *this)) |
| 894 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 896 | Token StrTok; |
| 897 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 899 | bool IsFileEntry = false, IsFileExit = false; |
| 900 | bool IsSystemHeader = false, IsExternCHeader = false; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 901 | int FilenameID = -1; |
| 902 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 903 | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
| 904 | // string followed by eod. |
| 905 | if (StrTok.is(tok::eod)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 906 | ; // ok |
| 907 | else if (StrTok.isNot(tok::string_literal)) { |
| 908 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 909 | return DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 910 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 911 | // Parse and validate the string, converting it into a unique ID. |
| 912 | StringLiteralParser Literal(&StrTok, 1, *this); |
| 913 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 914 | if (Literal.hadError) |
| 915 | return DiscardUntilEndOfDirective(); |
| 916 | if (Literal.Pascal) { |
| 917 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 918 | return DiscardUntilEndOfDirective(); |
| 919 | } |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 920 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 922 | // If a filename was present, read any flags that are present. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 924 | IsSystemHeader, IsExternCHeader, *this)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 925 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 928 | // Create a line note with this information. |
| 929 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | IsFileEntry, IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 931 | IsSystemHeader, IsExternCHeader); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 933 | // If the preprocessor has callbacks installed, notify them of the #line |
| 934 | // change. This is used so that the line marker comes out in -E mode for |
| 935 | // example. |
| 936 | if (Callbacks) { |
| 937 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
| 938 | if (IsFileEntry) |
| 939 | Reason = PPCallbacks::EnterFile; |
| 940 | else if (IsFileExit) |
| 941 | Reason = PPCallbacks::ExitFile; |
| 942 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
| 943 | if (IsExternCHeader) |
| 944 | FileKind = SrcMgr::C_ExternCSystem; |
| 945 | else if (IsSystemHeader) |
| 946 | FileKind = SrcMgr::C_System; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 948 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 953 | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. |
| 954 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 956 | bool isWarning) { |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 957 | // PTH doesn't emit #warning or #error directives. |
| 958 | if (CurPTHLexer) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 959 | return CurPTHLexer->DiscardToEndOfLine(); |
| 960 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 961 | // Read the rest of the line raw. We do this because we don't want macros |
| 962 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 963 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 964 | // collapse multiple consequtive white space between tokens, but this isn't |
| 965 | // specified by the standard. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 966 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 967 | if (isWarning) |
| 968 | Diag(Tok, diag::pp_hash_warning) << Message; |
| 969 | else |
| 970 | Diag(Tok, diag::err_pp_hash_error) << Message; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 974 | /// |
| 975 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
| 976 | // Yes, this directive is an extension. |
| 977 | Diag(Tok, diag::ext_pp_ident_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 979 | // Read the string argument. |
| 980 | Token StrTok; |
| 981 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 983 | // If the token kind isn't a string, it's a malformed directive. |
| 984 | if (StrTok.isNot(tok::string_literal) && |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 985 | StrTok.isNot(tok::wide_string_literal)) { |
| 986 | Diag(StrTok, diag::err_pp_malformed_ident); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 987 | if (StrTok.isNot(tok::eod)) |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 988 | DiscardUntilEndOfDirective(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 992 | // Verify that there is nothing after the string, other than EOD. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 993 | CheckEndOfDirective("ident"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 994 | |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 995 | if (Callbacks) { |
| 996 | bool Invalid = false; |
| 997 | std::string Str = getSpelling(StrTok, &Invalid); |
| 998 | if (!Invalid) |
| 999 | Callbacks->Ident(Tok.getLocation(), Str); |
| 1000 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | //===----------------------------------------------------------------------===// |
| 1004 | // Preprocessor Include Directive Handling. |
| 1005 | //===----------------------------------------------------------------------===// |
| 1006 | |
| 1007 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 1008 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 1009 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 1010 | /// caller is expected to provide a buffer that is large enough to hold the |
| 1011 | /// spelling of the filename, but is also expected to handle the case when |
| 1012 | /// this method decides to use a different buffer. |
| 1013 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1014 | llvm::StringRef &Buffer) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1015 | // Get the text form of the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1016 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1018 | // Make sure the filename is <x> or "x". |
| 1019 | bool isAngled; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1020 | if (Buffer[0] == '<') { |
| 1021 | if (Buffer.back() != '>') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1022 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1023 | Buffer = llvm::StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1024 | return true; |
| 1025 | } |
| 1026 | isAngled = true; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1027 | } else if (Buffer[0] == '"') { |
| 1028 | if (Buffer.back() != '"') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1029 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1030 | Buffer = llvm::StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1031 | return true; |
| 1032 | } |
| 1033 | isAngled = false; |
| 1034 | } else { |
| 1035 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1036 | Buffer = llvm::StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1037 | return true; |
| 1038 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1040 | // Diagnose #include "" as invalid. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1041 | if (Buffer.size() <= 2) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1042 | Diag(Loc, diag::err_pp_empty_filename); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1043 | Buffer = llvm::StringRef(); |
| 1044 | return true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1045 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1047 | // Skip the brackets. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1048 | Buffer = Buffer.substr(1, Buffer.size()-2); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1049 | return isAngled; |
| 1050 | } |
| 1051 | |
| 1052 | /// ConcatenateIncludeName - Handle cases where the #include name is expanded |
| 1053 | /// from a macro as multiple tokens, which need to be glued together. This |
| 1054 | /// occurs for code like: |
| 1055 | /// #define FOO <a/b.h> |
| 1056 | /// #include FOO |
| 1057 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. |
| 1058 | /// |
| 1059 | /// This code concatenates and consumes tokens up to the '>' token. It returns |
| 1060 | /// false if the > was found, otherwise it returns true if it finds and consumes |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1061 | /// the EOD marker. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1062 | bool Preprocessor::ConcatenateIncludeName( |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1063 | llvm::SmallString<128> &FilenameBuffer, |
| 1064 | SourceLocation &End) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1065 | Token CurTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1067 | Lex(CurTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1068 | while (CurTok.isNot(tok::eod)) { |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1069 | End = CurTok.getLocation(); |
| 1070 | |
Douglas Gregor | 25bb03b | 2010-12-09 23:35:36 +0000 | [diff] [blame] | 1071 | // FIXME: Provide code completion for #includes. |
| 1072 | if (CurTok.is(tok::code_completion)) { |
| 1073 | Lex(CurTok); |
| 1074 | continue; |
| 1075 | } |
| 1076 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1077 | // Append the spelling of this token to the buffer. If there was a space |
| 1078 | // before it, add it now. |
| 1079 | if (CurTok.hasLeadingSpace()) |
| 1080 | FilenameBuffer.push_back(' '); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1082 | // Get the spelling of the token, directly into FilenameBuffer if possible. |
| 1083 | unsigned PreAppendSize = FilenameBuffer.size(); |
| 1084 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1086 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1087 | unsigned ActualLen = getSpelling(CurTok, BufPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1089 | // If the token was spelled somewhere else, copy it into FilenameBuffer. |
| 1090 | if (BufPtr != &FilenameBuffer[PreAppendSize]) |
| 1091 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1093 | // Resize FilenameBuffer to the correct size. |
| 1094 | if (CurTok.getLength() != ActualLen) |
| 1095 | FilenameBuffer.resize(PreAppendSize+ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1097 | // If we found the '>' marker, return success. |
| 1098 | if (CurTok.is(tok::greater)) |
| 1099 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1101 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1104 | // If we hit the eod marker, emit an error and return true so that the caller |
| 1105 | // knows the EOD has been read. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1106 | Diag(CurTok.getLocation(), diag::err_pp_expects_filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1107 | return true; |
| 1108 | } |
| 1109 | |
| 1110 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1111 | /// file to be included from the lexer, then include it! This is a common |
| 1112 | /// routine with functionality shared between #include, #include_next and |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1113 | /// #import. LookupFrom is set when this is a #include_next directive, it |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | /// specifies the file to start searching from. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1115 | void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, |
| 1116 | Token &IncludeTok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1117 | const DirectoryLookup *LookupFrom, |
| 1118 | bool isImport) { |
| 1119 | |
| 1120 | Token FilenameTok; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1121 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1123 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1124 | llvm::SmallString<128> FilenameBuffer; |
| 1125 | llvm::StringRef Filename; |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1126 | SourceLocation End; |
| 1127 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1128 | switch (FilenameTok.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1129 | case tok::eod: |
| 1130 | // If the token kind is EOD, the error has already been diagnosed. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1131 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1133 | case tok::angle_string_literal: |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 1134 | case tok::string_literal: |
| 1135 | Filename = getSpelling(FilenameTok, FilenameBuffer); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1136 | End = FilenameTok.getLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1137 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1139 | case tok::less: |
| 1140 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1141 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1142 | FilenameBuffer.push_back('<'); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1143 | if (ConcatenateIncludeName(FilenameBuffer, End)) |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1144 | return; // Found <eod> but no ">"? Diagnostic already emitted. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1145 | Filename = FilenameBuffer.str(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1146 | break; |
| 1147 | default: |
| 1148 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1149 | DiscardUntilEndOfDirective(); |
| 1150 | return; |
| 1151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1153 | bool isAngled = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1154 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1155 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1156 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1157 | if (Filename.empty()) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1158 | DiscardUntilEndOfDirective(); |
| 1159 | return; |
| 1160 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1162 | // Verify that there is nothing after the filename, other than EOD. Note that |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1163 | // we allow macros that expand to nothing after the filename, because this |
| 1164 | // falls into the category of "#include pp-tokens new-line" specified in |
| 1165 | // C99 6.10.2p4. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1166 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1169 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
| 1170 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1171 | return; |
| 1172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1174 | // Search include directories. |
| 1175 | const DirectoryLookup *CurDir; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 1176 | llvm::SmallString<1024> SearchPath; |
| 1177 | llvm::SmallString<1024> RelativePath; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1178 | // We get the raw path only if we have 'Callbacks' to which we later pass |
| 1179 | // the path. |
| 1180 | const FileEntry *File = LookupFile( |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 1181 | Filename, isAngled, LookupFrom, CurDir, |
| 1182 | Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1183 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1184 | // Notify the callback object that we've seen an inclusion directive. |
| 1185 | if (Callbacks) |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1186 | Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 1187 | End, SearchPath, RelativePath); |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1188 | |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame^] | 1189 | if (File == 0) { |
| 1190 | Diag(FilenameTok, diag::warn_pp_file_not_found) << Filename; |
| 1191 | return; |
| 1192 | } |
| 1193 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1194 | // The #included file will be considered to be a system header if either it is |
| 1195 | // in a system include directory, or if the #includer is a system include |
| 1196 | // header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | SrcMgr::CharacteristicKind FileCharacter = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1198 | std::max(HeaderInfo.getFileDirFlavor(File), |
Chris Lattner | 693faa6 | 2009-01-19 07:59:15 +0000 | [diff] [blame] | 1199 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1201 | // Ask HeaderInfo if we should enter this #include file. If not, #including |
| 1202 | // this file will have no effect. |
| 1203 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1204 | if (Callbacks) |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1205 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1206 | return; |
| 1207 | } |
| 1208 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1209 | // Look up the file, create a File ID for it. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1210 | FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), |
| 1211 | FileCharacter); |
Peter Collingbourne | d57b7ff | 2011-06-30 16:41:03 +0000 | [diff] [blame] | 1212 | assert(!FID.isInvalid() && "Expected valid file ID"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Finally, if all is good, enter the new file! |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1215 | EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1219 | /// |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1220 | void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, |
| 1221 | Token &IncludeNextTok) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1222 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1224 | // #include_next is like #include, except that we start searching after |
| 1225 | // the current found directory. If we can't do this, issue a |
| 1226 | // diagnostic. |
| 1227 | const DirectoryLookup *Lookup = CurDirLookup; |
| 1228 | if (isInPrimaryFile()) { |
| 1229 | Lookup = 0; |
| 1230 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 1231 | } else if (Lookup == 0) { |
| 1232 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 1233 | } else { |
| 1234 | // Start looking up in the next directory. |
| 1235 | ++Lookup; |
| 1236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1238 | return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /// HandleImportDirective - Implements #import. |
| 1242 | /// |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1243 | void Preprocessor::HandleImportDirective(SourceLocation HashLoc, |
| 1244 | Token &ImportTok) { |
Chris Lattner | b627c8d | 2009-03-06 04:28:03 +0000 | [diff] [blame] | 1245 | if (!Features.ObjC1) // #import is standard for ObjC. |
| 1246 | Diag(ImportTok, diag::ext_pp_import_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1248 | return HandleIncludeDirective(HashLoc, ImportTok, 0, true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1251 | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a |
| 1252 | /// pseudo directive in the predefines buffer. This handles it by sucking all |
| 1253 | /// tokens through the preprocessor and discarding them (only keeping the side |
| 1254 | /// effects on the preprocessor). |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1255 | void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, |
| 1256 | Token &IncludeMacrosTok) { |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1257 | // This directive should only occur in the predefines buffer. If not, emit an |
| 1258 | // error and reject it. |
| 1259 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
| 1260 | if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { |
| 1261 | Diag(IncludeMacrosTok.getLocation(), |
| 1262 | diag::pp_include_macros_out_of_predefines); |
| 1263 | DiscardUntilEndOfDirective(); |
| 1264 | return; |
| 1265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1267 | // Treat this as a normal #include for checking purposes. If this is |
| 1268 | // successful, it will push a new lexer onto the include stack. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1269 | HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1271 | Token TmpTok; |
| 1272 | do { |
| 1273 | Lex(TmpTok); |
| 1274 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
| 1275 | } while (TmpTok.isNot(tok::hashhash)); |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1278 | //===----------------------------------------------------------------------===// |
| 1279 | // Preprocessor Macro Directive Handling. |
| 1280 | //===----------------------------------------------------------------------===// |
| 1281 | |
| 1282 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1283 | /// definition has just been read. Lex the rest of the arguments and the |
| 1284 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1285 | /// parsing the arg list. |
| 1286 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
| 1287 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1289 | Token Tok; |
| 1290 | while (1) { |
| 1291 | LexUnexpandedToken(Tok); |
| 1292 | switch (Tok.getKind()) { |
| 1293 | case tok::r_paren: |
| 1294 | // Found the end of the argument list. |
Chris Lattner | cf29e07 | 2009-02-20 22:31:31 +0000 | [diff] [blame] | 1295 | if (Arguments.empty()) // #define FOO() |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1296 | return false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1297 | // Otherwise we have #define FOO(A,) |
| 1298 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1299 | return true; |
| 1300 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1301 | // Warn if use of C99 feature in non-C99 mode. |
| 1302 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1303 | |
| 1304 | // Lex the token after the identifier. |
| 1305 | LexUnexpandedToken(Tok); |
| 1306 | if (Tok.isNot(tok::r_paren)) { |
| 1307 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1308 | return true; |
| 1309 | } |
| 1310 | // Add the __VA_ARGS__ identifier as an argument. |
| 1311 | Arguments.push_back(Ident__VA_ARGS__); |
| 1312 | MI->setIsC99Varargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1313 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1314 | return false; |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1315 | case tok::eod: // #define X( |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1316 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1317 | return true; |
| 1318 | default: |
| 1319 | // Handle keywords and identifiers here to accept things like |
| 1320 | // #define Foo(for) for. |
| 1321 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1322 | if (II == 0) { |
| 1323 | // #define X(1 |
| 1324 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1325 | return true; |
| 1326 | } |
| 1327 | |
| 1328 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1329 | // #define X(A,A. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1331 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6cf3ed7 | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 1332 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1333 | return true; |
| 1334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1336 | // Add the argument to the macro info. |
| 1337 | Arguments.push_back(II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1339 | // Lex the token after the identifier. |
| 1340 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1342 | switch (Tok.getKind()) { |
| 1343 | default: // #define X(A B |
| 1344 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1345 | return true; |
| 1346 | case tok::r_paren: // #define X(A) |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1347 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1348 | return false; |
| 1349 | case tok::comma: // #define X(A, |
| 1350 | break; |
| 1351 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1352 | // Diagnose extension. |
| 1353 | Diag(Tok, diag::ext_named_variadic_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1355 | // Lex the token after the identifier. |
| 1356 | LexUnexpandedToken(Tok); |
| 1357 | if (Tok.isNot(tok::r_paren)) { |
| 1358 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1359 | return true; |
| 1360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1362 | MI->setIsGNUVarargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1363 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1364 | return false; |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1371 | /// line then lets the caller lex the next real token. |
| 1372 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { |
| 1373 | ++NumDefined; |
| 1374 | |
| 1375 | Token MacroNameTok; |
| 1376 | ReadMacroName(MacroNameTok, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1378 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1379 | if (MacroNameTok.is(tok::eod)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1380 | return; |
| 1381 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1382 | Token LastTok = MacroNameTok; |
| 1383 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1384 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1385 | // mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 1386 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1388 | // Create the new macro. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1389 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1391 | Token Tok; |
| 1392 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1394 | // If this is a function-like macro definition, parse the argument list, |
| 1395 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1396 | // check other constraints on the first token of the macro body. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1397 | if (Tok.is(tok::eod)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1398 | // If there is no body to this macro, we have no special handling here. |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1399 | } else if (Tok.hasLeadingSpace()) { |
| 1400 | // This is a normal token with leading space. Clear the leading space |
| 1401 | // marker on the first token to get proper expansion. |
| 1402 | Tok.clearFlag(Token::LeadingSpace); |
| 1403 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1404 | // This is a function-like macro definition. Read the argument list. |
| 1405 | MI->setIsFunctionLike(); |
| 1406 | if (ReadMacroDefinitionArgList(MI)) { |
| 1407 | // Forget about MI. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1408 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1409 | // Throw away the rest of the line. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1410 | if (CurPPLexer->ParsingPreprocessorDirective) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1411 | DiscardUntilEndOfDirective(); |
| 1412 | return; |
| 1413 | } |
| 1414 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1415 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1416 | // the GNU named varargs extension, enabled __VA_ARGS__. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1418 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1419 | // This gets unpoisoned where it is allowed. |
| 1420 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1421 | if (MI->isC99Varargs()) |
| 1422 | Ident__VA_ARGS__->setIsPoisoned(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1424 | // Read the first token after the arg list for down below. |
| 1425 | LexUnexpandedToken(Tok); |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1426 | } else if (Features.C99) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1427 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1428 | // a diagnostic for something like "#define X+". |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1429 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1430 | } else { |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1431 | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the |
| 1432 | // first character of a replacement list is not a character required by |
| 1433 | // subclause 5.2.1, then there shall be white-space separation between the |
| 1434 | // identifier and the replacement list.". 5.2.1 lists this set: |
| 1435 | // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which |
| 1436 | // is irrelevant here. |
| 1437 | bool isInvalid = false; |
| 1438 | if (Tok.is(tok::at)) // @ is not in the list above. |
| 1439 | isInvalid = true; |
| 1440 | else if (Tok.is(tok::unknown)) { |
| 1441 | // If we have an unknown token, it is something strange like "`". Since |
| 1442 | // all of valid characters would have lexed into a single character |
| 1443 | // token of some sort, we know this is not a valid case. |
| 1444 | isInvalid = true; |
| 1445 | } |
| 1446 | if (isInvalid) |
| 1447 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
| 1448 | else |
| 1449 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1450 | } |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1451 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1452 | if (!Tok.is(tok::eod)) |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1453 | LastTok = Tok; |
| 1454 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1455 | // Read the rest of the macro body. |
| 1456 | if (MI->isObjectLike()) { |
| 1457 | // Object-like macros are very simple, just read their body. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1458 | while (Tok.isNot(tok::eod)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1459 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1460 | MI->AddTokenToBody(Tok); |
| 1461 | // Get the next token of the macro. |
| 1462 | LexUnexpandedToken(Tok); |
| 1463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1465 | } else { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1466 | // Otherwise, read the body of a function-like macro. While we are at it, |
| 1467 | // check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1468 | // parameters in function-like macro expansions. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1469 | while (Tok.isNot(tok::eod)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1470 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1472 | if (Tok.isNot(tok::hash)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1473 | MI->AddTokenToBody(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1475 | // Get the next token of the macro. |
| 1476 | LexUnexpandedToken(Tok); |
| 1477 | continue; |
| 1478 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1480 | // Get the next token of the macro. |
| 1481 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1483 | // Check for a valid macro arg identifier. |
| 1484 | if (Tok.getIdentifierInfo() == 0 || |
| 1485 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1486 | |
| 1487 | // If this is assembler-with-cpp mode, we accept random gibberish after |
| 1488 | // the '#' because '#' is often a comment character. However, change |
| 1489 | // the kind of the token to tok::unknown so that the preprocessor isn't |
| 1490 | // confused. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1491 | if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eod)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1492 | LastTok.setKind(tok::unknown); |
| 1493 | } else { |
| 1494 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1495 | ReleaseMacroInfo(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1497 | // Disable __VA_ARGS__ again. |
| 1498 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1499 | return; |
| 1500 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1503 | // Things look ok, add the '#' and param name tokens to the macro. |
| 1504 | MI->AddTokenToBody(LastTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1505 | MI->AddTokenToBody(Tok); |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1506 | LastTok = Tok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1508 | // Get the next token of the macro. |
| 1509 | LexUnexpandedToken(Tok); |
| 1510 | } |
| 1511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
| 1513 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1514 | // Disable __VA_ARGS__ again. |
| 1515 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1516 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1517 | // Check that there is no paste (##) operator at the beginning or end of the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1518 | // replacement list. |
| 1519 | unsigned NumTokens = MI->getNumTokens(); |
| 1520 | if (NumTokens != 0) { |
| 1521 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
| 1522 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1523 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1524 | return; |
| 1525 | } |
| 1526 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
| 1527 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1528 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1529 | return; |
| 1530 | } |
| 1531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1533 | MI->setDefinitionEndLoc(LastTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1535 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1536 | // the macro bodies are identical and free the old definition. |
| 1537 | if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1538 | // It is very common for system headers to have tons of macro redefinitions |
| 1539 | // and for warnings to be disabled in system headers. If this is the case, |
| 1540 | // then don't bother calling MacroInfo::isIdenticalTo. |
Chris Lattner | 7f549df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 1541 | if (!getDiagnostics().getSuppressSystemWarnings() || |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1542 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
Argyrios Kyrtzidis | a33e050 | 2011-01-18 19:50:15 +0000 | [diff] [blame] | 1543 | if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1544 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1546 | // Macros must be identical. This means all tokens and whitespace |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1547 | // separation must be the same. C99 6.10.3.2. |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1548 | if (!OtherMI->isAllowRedefinitionsWithoutWarning() && |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1549 | !MI->isIdenticalTo(*OtherMI, *this)) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1550 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
| 1551 | << MacroNameTok.getIdentifierInfo(); |
| 1552 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
| 1553 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1554 | } |
Argyrios Kyrtzidis | a33e050 | 2011-01-18 19:50:15 +0000 | [diff] [blame] | 1555 | if (OtherMI->isWarnIfUnused()) |
| 1556 | WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1557 | ReleaseMacroInfo(OtherMI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1560 | setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1562 | assert(!MI->isUsed()); |
| 1563 | // If we need warning for not using the macro, add its location in the |
| 1564 | // warn-because-unused-macro set. If it gets used it will be removed from set. |
| 1565 | if (isInPrimaryFile() && // don't warn for include'd macros. |
| 1566 | Diags->getDiagnosticLevel(diag::pp_macro_not_used, |
| 1567 | MI->getDefinitionLoc()) != Diagnostic::Ignored) { |
| 1568 | MI->setIsWarnIfUnused(true); |
| 1569 | WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); |
| 1570 | } |
| 1571 | |
Chris Lattner | f4a72b0 | 2009-04-12 01:39:54 +0000 | [diff] [blame] | 1572 | // If the callbacks want to know, tell them about the macro definition. |
| 1573 | if (Callbacks) |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1574 | Callbacks->MacroDefined(MacroNameTok, MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /// HandleUndefDirective - Implements #undef. |
| 1578 | /// |
| 1579 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { |
| 1580 | ++NumUndefined; |
| 1581 | |
| 1582 | Token MacroNameTok; |
| 1583 | ReadMacroName(MacroNameTok, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1585 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1586 | if (MacroNameTok.is(tok::eod)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1587 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1589 | // Check to see if this is the last token on the #undef line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1590 | CheckEndOfDirective("undef"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1592 | // Okay, we finally have a valid identifier to undef. |
| 1593 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1595 | // If the macro is not defined, this is a noop undef, just return. |
| 1596 | if (MI == 0) return; |
| 1597 | |
Argyrios Kyrtzidis | 1f8dcfc | 2011-07-11 20:39:47 +0000 | [diff] [blame] | 1598 | if (!MI->isUsed() && MI->isWarnIfUnused()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1599 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1600 | |
| 1601 | // If the callbacks want to know, tell them about the macro #undef. |
| 1602 | if (Callbacks) |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1603 | Callbacks->MacroUndefined(MacroNameTok, MI); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1604 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1605 | if (MI->isWarnIfUnused()) |
| 1606 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 1607 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1608 | // Free macro definition. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1609 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1610 | setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | //===----------------------------------------------------------------------===// |
| 1615 | // Preprocessor Conditional Directive Handling. |
| 1616 | //===----------------------------------------------------------------------===// |
| 1617 | |
| 1618 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 1619 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1620 | /// if any tokens have been returned or pp-directives activated before this |
| 1621 | /// #ifndef has been lexed. |
| 1622 | /// |
| 1623 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, |
| 1624 | bool ReadAnyTokensBeforeDirective) { |
| 1625 | ++NumIf; |
| 1626 | Token DirectiveTok = Result; |
| 1627 | |
| 1628 | Token MacroNameTok; |
| 1629 | ReadMacroName(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1631 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1632 | if (MacroNameTok.is(tok::eod)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1633 | // Skip code until we get to #endif. This helps with recovery by not |
| 1634 | // emitting an error when the #endif is reached. |
| 1635 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 1636 | /*Foundnonskip*/false, /*FoundElse*/false); |
| 1637 | return; |
| 1638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1640 | // Check to see if this is the last token on the #if[n]def line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1641 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1642 | |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1643 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 1644 | MacroInfo *MI = getMacroInfo(MII); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1645 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1646 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1647 | // If the start of a top-level #ifdef and if the macro is not defined, |
| 1648 | // inform MIOpt that this might be the start of a proper include guard. |
| 1649 | // Otherwise it is some other form of unknown conditional which we can't |
| 1650 | // handle. |
| 1651 | if (!ReadAnyTokensBeforeDirective && MI == 0) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1652 | assert(isIfndef && "#ifdef shouldn't reach here"); |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1653 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1654 | } else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1655 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1658 | // If there is a macro, process it. |
| 1659 | if (MI) // Mark it used. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1660 | markMacroAsUsed(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1662 | // Should we include the stuff contained by this directive? |
| 1663 | if (!MI == isIfndef) { |
| 1664 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 1665 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
| 1666 | /*wasskip*/false, /*foundnonskip*/true, |
| 1667 | /*foundelse*/false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1668 | } else { |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1669 | // No, skip the contents of this block. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1670 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1672 | /*FoundElse*/false); |
| 1673 | } |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (Callbacks) { |
| 1676 | if (isIfndef) |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1677 | Callbacks->Ifndef(MacroNameTok); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1678 | else |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1679 | Callbacks->Ifdef(MacroNameTok); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1680 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /// HandleIfDirective - Implements the #if directive. |
| 1684 | /// |
| 1685 | void Preprocessor::HandleIfDirective(Token &IfToken, |
| 1686 | bool ReadAnyTokensBeforeDirective) { |
| 1687 | ++NumIf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1689 | // Parse and evaluate the conditional expression. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1690 | IdentifierInfo *IfNDefMacro = 0; |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1691 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
| 1692 | const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
| 1693 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1694 | |
| 1695 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1696 | // directive seen, handle it for the multiple-include optimization. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1697 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1698 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1699 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1700 | else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1701 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1704 | // Should we include the stuff contained by this directive? |
| 1705 | if (ConditionalTrue) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1706 | // Yes, remember that we are inside a conditional, then lex the next token. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1707 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1708 | /*foundnonskip*/true, /*foundelse*/false); |
| 1709 | } else { |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1710 | // No, skip the contents of this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1712 | /*FoundElse*/false); |
| 1713 | } |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1714 | |
| 1715 | if (Callbacks) |
| 1716 | Callbacks->If(SourceRange(ConditionalBegin, ConditionalEnd)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | /// HandleEndifDirective - Implements the #endif directive. |
| 1720 | /// |
| 1721 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
| 1722 | ++NumEndif; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1724 | // Check that this is the whole directive. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1725 | CheckEndOfDirective("endif"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1727 | PPConditionalInfo CondInfo; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1728 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1729 | // No conditionals on the stack: this is an #endif without an #if. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1730 | Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1731 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1734 | // If this the end of a top-level #endif, inform MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1735 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1736 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1738 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1739 | "This code should only be reachable in the non-skipping case!"); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1740 | |
| 1741 | if (Callbacks) |
| 1742 | Callbacks->Endif(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1745 | /// HandleElseDirective - Implements the #else directive. |
| 1746 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1747 | void Preprocessor::HandleElseDirective(Token &Result) { |
| 1748 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1750 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1751 | CheckEndOfDirective("else"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1753 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1754 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1755 | Diag(Result, diag::pp_err_else_without_if); |
| 1756 | return; |
| 1757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1759 | // If this is a top-level #else, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1760 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1761 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1762 | |
| 1763 | // If this is a #else with a #else before it, report the error. |
| 1764 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1766 | // Finally, skip the rest of the contents of this block. |
| 1767 | SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1768 | /*FoundElse*/true); |
| 1769 | |
| 1770 | if (Callbacks) |
| 1771 | Callbacks->Else(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1774 | /// HandleElifDirective - Implements the #elif directive. |
| 1775 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1776 | void Preprocessor::HandleElifDirective(Token &ElifToken) { |
| 1777 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1779 | // #elif directive in a non-skipping conditional... start skipping. |
| 1780 | // We don't care what the condition is, because we will always skip it (since |
| 1781 | // the block immediately before it was included). |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1782 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1783 | DiscardUntilEndOfDirective(); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1784 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1785 | |
| 1786 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1787 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1788 | Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1789 | return; |
| 1790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1792 | // If this is a top-level #elif, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1793 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1794 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1796 | // If this is a #elif with a #else before it, report the error. |
| 1797 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
| 1798 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1799 | // Finally, skip the rest of the contents of this block. |
| 1800 | SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1801 | /*FoundElse*/CI.FoundElse); |
| 1802 | |
| 1803 | if (Callbacks) |
| 1804 | Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1805 | } |