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