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