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