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