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