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