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