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