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