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