blob: 59ea27c586888cd01d9071014b313561228da957 [file] [log] [blame]
Chris Lattner89620152008-03-09 03:13:06 +00001//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
Chris Lattnerf64b3522008-03-09 01:54:53 +00002//
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 Dennettf6333ac2012-06-22 05:46:07 +00009///
10/// \file
11/// \brief Implements # directive processing for the Preprocessor.
12///
Chris Lattnerf64b3522008-03-09 01:54:53 +000013//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Preprocessor.h"
Chris Lattner710bb872009-11-30 04:18:44 +000016#include "clang/Basic/FileManager.h"
Chris Lattnerf64b3522008-03-09 01:54:53 +000017#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/CodeCompletionHandler.h"
19#include "clang/Lex/HeaderSearch.h"
Daniel Jasper07e6c402013-08-05 20:26:17 +000020#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#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 Lattner100c65e2009-01-26 05:29:08 +000026#include "llvm/ADT/APInt.h"
Douglas Gregor41e115a2011-11-30 18:02:36 +000027#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaf6002232014-08-08 21:31:04 +000028#include "llvm/Support/Path.h"
Aaron Ballman6ce00002013-01-16 19:32:21 +000029#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000030
Chris Lattnerf64b3522008-03-09 01:54:53 +000031using namespace clang;
32
33//===----------------------------------------------------------------------===//
34// Utility Methods for Preprocessor Directive Handling.
35//===----------------------------------------------------------------------===//
36
Chris Lattnerc0a585d2010-08-17 15:55:45 +000037MacroInfo *Preprocessor::AllocateMacroInfo() {
Richard Smithee0c4c12014-07-24 01:13:23 +000038 MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
Ted Kremenekc8456f82010-10-19 22:15:20 +000039 MIChain->Next = MIChainHead;
Ted Kremenekc8456f82010-10-19 22:15:20 +000040 MIChainHead = MIChain;
Richard Smithee0c4c12014-07-24 01:13:23 +000041 return &MIChain->MI;
Chris Lattnerc0a585d2010-08-17 15:55:45 +000042}
43
44MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
45 MacroInfo *MI = AllocateMacroInfo();
Ted Kremenek6c7ea112008-12-15 19:56:42 +000046 new (MI) MacroInfo(L);
47 return MI;
48}
49
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000050MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
51 unsigned SubModuleID) {
Chandler Carruth06dde922014-03-02 13:02:01 +000052 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
53 "alignment for MacroInfo is less than the ID");
Argyrios Kyrtzidisd48b91d2013-04-30 05:05:35 +000054 DeserializedMacroInfoChain *MIChain =
55 BP.Allocate<DeserializedMacroInfoChain>();
56 MIChain->Next = DeserialMIChainHead;
57 DeserialMIChainHead = MIChain;
58
59 MacroInfo *MI = &MIChain->MI;
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000060 new (MI) MacroInfo(L);
61 MI->FromASTFile = true;
62 MI->setOwningModuleID(SubModuleID);
63 return MI;
64}
65
Richard Smith50474bf2015-04-23 23:29:05 +000066DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
67 SourceLocation Loc) {
Richard Smith713369b2015-04-23 20:40:50 +000068 return new (BP) DefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000069}
70
71UndefMacroDirective *
Richard Smith50474bf2015-04-23 23:29:05 +000072Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
Richard Smith713369b2015-04-23 20:40:50 +000073 return new (BP) UndefMacroDirective(UndefLoc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000074}
75
76VisibilityMacroDirective *
77Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
78 bool isPublic) {
Richard Smithdaa69e02014-07-25 04:40:03 +000079 return new (BP) VisibilityMacroDirective(Loc, isPublic);
Chris Lattnerc0a585d2010-08-17 15:55:45 +000080}
81
James Dennettf6333ac2012-06-22 05:46:07 +000082/// \brief Read and discard all tokens remaining on the current line until
83/// the tok::eod token is found.
Chris Lattnerf64b3522008-03-09 01:54:53 +000084void Preprocessor::DiscardUntilEndOfDirective() {
85 Token Tmp;
86 do {
87 LexUnexpandedToken(Tmp);
Peter Collingbournef29ce972011-02-22 13:49:06 +000088 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +000089 } while (Tmp.isNot(tok::eod));
Chris Lattnerf64b3522008-03-09 01:54:53 +000090}
91
Serge Pavlov07c0f042014-12-18 11:14:21 +000092/// \brief Enumerates possible cases of #define/#undef a reserved identifier.
93enum 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.
102static 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 Weber92c14bb2014-12-16 21:16:10 +0000116 return false;
Serge Pavlov83cf0782014-12-11 12:18:08 +0000117}
118
Serge Pavlov07c0f042014-12-18 11:14:21 +0000119static 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
131static 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
140bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
141 bool *ShadowFlag) {
Alp Tokerb05e0b52014-05-21 06:13:51 +0000142 // 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 Tokerf33619c2014-05-31 03:38:08 +0000152 II = getIdentifierInfo(Spelling);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000153
Alp Tokerf33619c2014-05-31 03:38:08 +0000154 if (!II->isCPlusPlusOperatorKeyword())
155 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000156
Alp Tokere03e9e12014-05-31 16:32:22 +0000157 // 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 Tokerb05e0b52014-05-21 06:13:51 +0000163
Alp Tokerc5d194fc2014-05-31 03:38:17 +0000164 // Allow #defining |and| and friends for Microsoft compatibility or
165 // recovery when legacy C headers are included in C++.
Alp Tokerf33619c2014-05-31 03:38:08 +0000166 MacroNameTok.setIdentifierInfo(II);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000167 }
168
Serge Pavlovd024f522014-10-24 17:31:32 +0000169 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
Alp Tokerb05e0b52014-05-21 06:13:51 +0000170 // 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 Smith20e883e2015-04-29 23:20:19 +0000174 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 Tokerb05e0b52014-05-21 06:13:51 +0000181 }
182
Serge Pavlov07c0f042014-12-18 11:14:21 +0000183 // If defining/undefining reserved identifier or a keyword, we need to issue
184 // a warning.
Serge Pavlov83cf0782014-12-11 12:18:08 +0000185 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
Serge Pavlov07c0f042014-12-18 11:14:21 +0000186 if (ShadowFlag)
187 *ShadowFlag = false;
Serge Pavlov83cf0782014-12-11 12:18:08 +0000188 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
189 (strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
Serge Pavlov07c0f042014-12-18 11:14:21 +0000190 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 Pavlov83cf0782014-12-11 12:18:08 +0000205 }
206
Alp Tokerb05e0b52014-05-21 06:13:51 +0000207 // Okay, we got a good identifier.
208 return false;
209}
210
James Dennettf6333ac2012-06-22 05:46:07 +0000211/// \brief Lex and validate a macro name, which occurs after a
212/// \#define or \#undef.
213///
Serge Pavlovd024f522014-10-24 17:31:32 +0000214/// 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 Pavlov07c0f042014-12-18 11:14:21 +0000218/// \param isDefineUndef Context in which macro is used.
219/// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
220void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
221 bool *ShadowFlag) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000222 // Read the token, don't allow macro expansion on it.
223 LexUnexpandedToken(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +0000224
Douglas Gregor12785102010-08-24 20:21:13 +0000225 if (MacroNameTok.is(tok::code_completion)) {
226 if (CodeComplete)
Serge Pavlovd024f522014-10-24 17:31:32 +0000227 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000228 setCodeCompletionReached();
Douglas Gregor12785102010-08-24 20:21:13 +0000229 LexUnexpandedToken(MacroNameTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000230 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000231
Serge Pavlov07c0f042014-12-18 11:14:21 +0000232 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
Chris Lattner907dfe92008-11-18 07:59:24 +0000233 return;
Alp Tokerb05e0b52014-05-21 06:13:51 +0000234
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 Lattner907dfe92008-11-18 07:59:24 +0000240 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000241}
242
James Dennettf6333ac2012-06-22 05:46:07 +0000243/// \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 Lattner0003c272009-04-17 23:30:53 +0000246/// true, then we consider macros that expand to zero tokens as being ok.
247void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000248 Token Tmp;
Chris Lattner0003c272009-04-17 23:30:53 +0000249 // 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 Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattnerf64b3522008-03-09 01:54:53 +0000257 // 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 Stump11289f42009-09-09 15:08:12 +0000261
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000262 if (Tmp.isNot(tok::eod)) {
Chris Lattner825676a2009-04-14 05:15:20 +0000263 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000264 // 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 Gregora771f462010-03-31 17:46:05 +0000267 FixItHint Hint;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000268 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000269 !CurTokenLexer)
Douglas Gregora771f462010-03-31 17:46:05 +0000270 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
271 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000272 DiscardUntilEndOfDirective();
273 }
274}
275
James Dennettf6333ac2012-06-22 05:46:07 +0000276/// 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 Lattnerf64b3522008-03-09 01:54:53 +0000279/// FoundNonSkipPortion is true, then we have already emitted code for part of
James Dennettf6333ac2012-06-22 05:46:07 +0000280/// 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 Lattnerf64b3522008-03-09 01:54:53 +0000284void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
285 bool FoundNonSkipPortion,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000286 bool FoundElse,
287 SourceLocation ElseLoc) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000288 ++NumSkipped;
David Blaikie7d170102013-05-15 07:37:26 +0000289 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000290
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000291 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000292 FoundNonSkipPortion, FoundElse);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremenek56572ab2008-12-12 18:34:08 +0000294 if (CurPTHLexer) {
295 PTHSkipExcludedConditionalBlock();
296 return;
297 }
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerf64b3522008-03-09 01:54:53 +0000299 // Enter raw mode to disable identifier lookup (and thus macro expansion),
300 // disabling warnings, etc.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000301 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000302 Token Tok;
303 while (1) {
Chris Lattnerf406b242010-01-18 22:33:01 +0000304 CurLexer->Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000305
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000306 if (Tok.is(tok::code_completion)) {
307 if (CodeComplete)
308 CodeComplete->CodeCompleteInConditionalExclusion();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000309 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000310 continue;
311 }
312
Chris Lattnerf64b3522008-03-09 01:54:53 +0000313 // 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 Kremenek30cd88c2008-11-18 00:34:22 +0000317 while (!CurPPLexer->ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000318 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000319 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
320 diag::err_pp_unterminated_conditional);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000321 CurPPLexer->ConditionalStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000322 }
323
Chris Lattnerf64b3522008-03-09 01:54:53 +0000324 // Just return and let the caller lex after this #include.
325 break;
326 }
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattnerf64b3522008-03-09 01:54:53 +0000328 // If this token is not a preprocessor directive, just skip it.
329 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
330 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattnerf64b3522008-03-09 01:54:53 +0000332 // 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 Collingbourne2f1e36b2011-02-28 02:37:51 +0000334 // converted into an EOD token (this terminates the macro).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000335 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rose176057b2013-02-22 00:32:00 +0000336 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000337
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattnerf64b3522008-03-09 01:54:53 +0000339 // Read the next token, the directive flavor.
340 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000341
Chris Lattnerf64b3522008-03-09 01:54:53 +0000342 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
343 // something bogus), skip it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000344 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000345 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000346 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000347 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000348 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 Toker2d57cea2014-05-17 04:53:25 +0000356 StringRef RI = Tok.getRawIdentifier();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000357
Alp Toker2d57cea2014-05-17 04:53:25 +0000358 char FirstChar = RI[0];
Mike Stump11289f42009-09-09 15:08:12 +0000359 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000360 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000361 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000362 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000363 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000364 continue;
365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattnerf64b3522008-03-09 01:54:53 +0000367 // 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 Kramer144884642009-12-31 13:32:38 +0000370 char DirectiveBuf[20];
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000371 StringRef Directive;
Alp Toker2d57cea2014-05-17 04:53:25 +0000372 if (!Tok.needsCleaning() && RI.size() < 20) {
373 Directive = RI;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000374 } else {
375 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramer144884642009-12-31 13:32:38 +0000376 unsigned IdLen = DirectiveStr.size();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000377 if (IdLen >= 20) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000378 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000379 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000380 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000381 continue;
382 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000383 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000384 Directive = StringRef(DirectiveBuf, IdLen);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Benjamin Kramer144884642009-12-31 13:32:38 +0000387 if (Directive.startswith("if")) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000388 StringRef Sub = Directive.substr(2);
Benjamin Kramer144884642009-12-31 13:32:38 +0000389 if (Sub.empty() || // "if"
390 Sub == "def" || // "ifdef"
391 Sub == "ndef") { // "ifndef"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000392 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
393 // bother parsing the condition.
394 DiscardUntilEndOfDirective();
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000395 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000396 /*foundnonskip*/false,
Chandler Carruth540960f2011-01-03 17:40:17 +0000397 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000398 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000399 } else if (Directive[0] == 'e') {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000400 StringRef Sub = Directive.substr(1);
Benjamin Kramer144884642009-12-31 13:32:38 +0000401 if (Sub == "ndif") { // "endif"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000402 PPConditionalInfo CondInfo;
403 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000404 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000405 (void)InCond; // Silence warning in no-asserts mode.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000406 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattnerf64b3522008-03-09 01:54:53 +0000408 // If we popped the outermost skipping block, we're done skipping!
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000409 if (!CondInfo.WasSkipping) {
Richard Smith87d8fb92012-06-24 23:56:26 +0000410 // 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 Smithd0124572012-06-21 00:35:03 +0000413 CheckEndOfDirective("endif");
Richard Smith87d8fb92012-06-24 23:56:26 +0000414 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000415 if (Callbacks)
416 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000417 break;
Richard Smithd0124572012-06-21 00:35:03 +0000418 } else {
419 DiscardUntilEndOfDirective();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000420 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000421 } else if (Sub == "lse") { // "else".
Chris Lattnerf64b3522008-03-09 01:54:53 +0000422 // #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 Kremenek30cd88c2008-11-18 00:34:22 +0000425 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerf64b3522008-03-09 01:54:53 +0000427 // 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 Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattnerf64b3522008-03-09 01:54:53 +0000430 // Note that we've seen a #else in this conditional.
431 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnerf64b3522008-03-09 01:54:53 +0000433 // 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 Smith87d8fb92012-06-24 23:56:26 +0000437 // Restore the value of LexingRawMode so that trailing comments
438 // are handled correctly.
439 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000440 CheckEndOfDirective("else");
Richard Smith87d8fb92012-06-24 23:56:26 +0000441 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000442 if (Callbacks)
443 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000444 break;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000445 } else {
446 DiscardUntilEndOfDirective(); // C99 6.10p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000447 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000448 } else if (Sub == "lif") { // "elif".
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000449 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000450
John Thompson17c35732013-12-04 20:19:30 +0000451 // 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 Lattnerf64b3522008-03-09 01:54:53 +0000454 // 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 Lattnerf64b3522008-03-09 01:54:53 +0000458 } else {
John Thompson17c35732013-12-04 20:19:30 +0000459 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000460 // Restore the value of LexingRawMode so that identifiers are
461 // looked up, etc, inside the #elif expression.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000462 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
463 CurPPLexer->LexingRawMode = false;
Craig Topperd2d442c2014-05-17 23:10:59 +0000464 IdentifierInfo *IfNDefMacro = nullptr;
John Thompson17c35732013-12-04 20:19:30 +0000465 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000466 CurPPLexer->LexingRawMode = true;
John Thompson17c35732013-12-04 20:19:30 +0000467 if (Callbacks) {
468 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000469 Callbacks->Elif(Tok.getLocation(),
John Thompson17c35732013-12-04 20:19:30 +0000470 SourceRange(CondBegin, CondEnd),
John Thompson87f9fef2013-12-07 08:41:15 +0000471 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
John Thompson17c35732013-12-04 20:19:30 +0000472 }
473 // If this condition is true, enter it!
474 if (CondValue) {
475 CondInfo.FoundNonSkip = true;
476 break;
477 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000478 }
479 }
480 }
Mike Stump11289f42009-09-09 15:08:12 +0000481
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000482 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000483 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000484 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000485 }
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 Kremenek30cd88c2008-11-18 00:34:22 +0000490 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000491
492 if (Callbacks) {
493 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
494 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
495 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000496}
497
Ted Kremenek56572ab2008-12-12 18:34:08 +0000498void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump11289f42009-09-09 15:08:12 +0000499 while (1) {
Ted Kremenek56572ab2008-12-12 18:34:08 +0000500 assert(CurPTHLexer);
501 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Ted Kremenek56572ab2008-12-12 18:34:08 +0000503 // 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 Yasskinb3321532010-12-23 01:01:28 +0000509 (void)InCond; // Silence warning in no-asserts mode.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000510 assert(!InCond && "Can't be skipping if not in a conditional!");
511 break;
512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Ted Kremenek56572ab2008-12-12 18:34:08 +0000514 // We have reached a '#else' or '#elif'. Lex the next token to get
515 // the directive flavor.
516 Token Tok;
517 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000518
Ted Kremenek56572ab2008-12-12 18:34:08 +0000519 // 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 Stump11289f42009-09-09 15:08:12 +0000530
Ted Kremenek56572ab2008-12-12 18:34:08 +0000531 // If the #if block wasn't entered then enter the #else block now.
532 if (!CondInfo.FoundNonSkip) {
533 CondInfo.FoundNonSkip = true;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000535 // Scan until the eod token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000536 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar2cba6be2009-04-13 17:57:49 +0000537 DiscardUntilEndOfDirective();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000538 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000539
Ted Kremenek56572ab2008-12-12 18:34:08 +0000540 break;
541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Ted Kremenek56572ab2008-12-12 18:34:08 +0000543 // Otherwise skip this block.
544 continue;
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenek56572ab2008-12-12 18:34:08 +0000547 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 Stump11289f42009-09-09 15:08:12 +0000553
Ted Kremenek56572ab2008-12-12 18:34:08 +0000554 // If this is in a skipping block or if we're already handled this #if
Mike Stump11289f42009-09-09 15:08:12 +0000555 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000556 if (CondInfo.FoundNonSkip)
557 continue;
558
559 // Evaluate the condition of the #elif.
Craig Topperd2d442c2014-05-17 23:10:59 +0000560 IdentifierInfo *IfNDefMacro = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000561 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 Kremenek56572ab2008-12-12 18:34:08 +0000572 }
573}
574
Richard Smith2a553082015-04-23 22:58:06 +0000575Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
Richard Smith7e82e012016-02-19 22:25:36 +0000576 if (!SourceMgr.isInMainFile(Loc)) {
577 // Try to determine the module of the include directive.
578 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
579 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
580 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
581 // The include comes from an included file.
582 return HeaderInfo.getModuleMap()
583 .findModuleForHeader(EntryOfIncl)
584 .getModule();
585 }
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000586 }
Richard Smith7e82e012016-02-19 22:25:36 +0000587
588 // This is either in the main file or not in a file at all. It belongs
589 // to the current module, if there is one.
590 return getLangOpts().CurrentModule.empty()
591 ? nullptr
592 : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000593}
594
Richard Smith2a553082015-04-23 22:58:06 +0000595Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) {
596 return HeaderInfo.getModuleMap().inferModuleFromLocation(
597 FullSourceLoc(Loc, SourceMgr));
598}
599
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000600const FileEntry *Preprocessor::LookupFile(
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000601 SourceLocation FilenameLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000602 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000603 bool isAngled,
604 const DirectoryLookup *FromDir,
Richard Smith25d50752014-10-20 00:15:49 +0000605 const FileEntry *FromFile,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000606 const DirectoryLookup *&CurDir,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000607 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000608 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000609 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000610 bool SkipCache) {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000611 Module *RequestingModule = getModuleForLocation(FilenameLoc);
Richard Smith8d4e90b2016-03-14 17:52:37 +0000612 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
Richard Smith3d5b48c2015-10-16 21:42:56 +0000613
Will Wilson0fafd342013-12-27 19:46:16 +0000614 // If the header lookup mechanism may be relative to the current inclusion
615 // stack, record the parent #includes.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000616 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
617 Includers;
Richard Smith25d50752014-10-20 00:15:49 +0000618 if (!FromDir && !FromFile) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000619 FileID FID = getCurrentFileLexer()->getFileID();
Will Wilson0fafd342013-12-27 19:46:16 +0000620 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Chris Lattner022923a2009-02-04 19:45:07 +0000622 // If there is no file entry associated with this file, it must be the
Richard Smith3c1a41a2014-12-02 00:08:08 +0000623 // predefines buffer or the module includes buffer. Any other file is not
624 // lexed with a normal lexer, so it won't be scanned for preprocessor
625 // directives.
626 //
627 // If we have the predefines buffer, resolve #include references (which come
628 // from the -include command line argument) from the current working
629 // directory instead of relative to the main file.
630 //
631 // If we have the module includes buffer, resolve #include references (which
632 // come from header declarations in the module map) relative to the module
633 // map file.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000634 if (!FileEnt) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000635 if (FID == SourceMgr.getMainFileID() && MainFileDir)
636 Includers.push_back(std::make_pair(nullptr, MainFileDir));
637 else if ((FileEnt =
638 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000639 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
640 } else {
641 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
642 }
Will Wilson0fafd342013-12-27 19:46:16 +0000643
644 // MSVC searches the current include stack from top to bottom for
645 // headers included by quoted include directives.
646 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
Alp Tokerbfa39342014-01-14 12:51:41 +0000647 if (LangOpts.MSVCCompat && !isAngled) {
Will Wilson0fafd342013-12-27 19:46:16 +0000648 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
649 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
650 if (IsFileLexer(ISEntry))
Yaron Keren65224612015-12-18 10:30:12 +0000651 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000652 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
Will Wilson0fafd342013-12-27 19:46:16 +0000653 }
Chris Lattner022923a2009-02-04 19:45:07 +0000654 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000657 CurDir = CurDirLookup;
Richard Smith25d50752014-10-20 00:15:49 +0000658
659 if (FromFile) {
660 // We're supposed to start looking from after a particular file. Search
661 // the include path until we find that file or run out of files.
662 const DirectoryLookup *TmpCurDir = CurDir;
663 const DirectoryLookup *TmpFromDir = nullptr;
664 while (const FileEntry *FE = HeaderInfo.LookupFile(
665 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000666 Includers, SearchPath, RelativePath, RequestingModule,
667 SuggestedModule, SkipCache)) {
Richard Smith25d50752014-10-20 00:15:49 +0000668 // Keep looking as if this file did a #include_next.
669 TmpFromDir = TmpCurDir;
670 ++TmpFromDir;
671 if (FE == FromFile) {
672 // Found it.
673 FromDir = TmpFromDir;
674 CurDir = TmpCurDir;
675 break;
676 }
677 }
678 }
679
680 // Do a standard file entry lookup.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000681 const FileEntry *FE = HeaderInfo.LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000682 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000683 RelativePath, RequestingModule, SuggestedModule, SkipCache);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000684 if (FE) {
Daniel Jasper5c77e392014-03-14 14:53:17 +0000685 if (SuggestedModule && !LangOpts.AsmPreprocessor)
Daniel Jasper92669ee2013-12-20 12:09:36 +0000686 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
Richard Smith8d4e90b2016-03-14 17:52:37 +0000687 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
688 Filename, FE);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000689 return FE;
690 }
Mike Stump11289f42009-09-09 15:08:12 +0000691
Will Wilson0fafd342013-12-27 19:46:16 +0000692 const FileEntry *CurFileEnt;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000693 // Otherwise, see if this is a subframework header. If so, this is relative
694 // to one of the headers on the #include stack. Walk the list of the current
695 // headers on the #include stack and pass them to HeaderInfo.
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000696 if (IsFileLexer()) {
Yaron Keren65224612015-12-18 10:30:12 +0000697 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000698 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000699 SearchPath, RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000700 RequestingModule,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000701 SuggestedModule))) {
702 if (SuggestedModule && !LangOpts.AsmPreprocessor)
703 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
Richard Smith8d4e90b2016-03-14 17:52:37 +0000704 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
705 Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000706 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000707 }
708 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000709 }
Mike Stump11289f42009-09-09 15:08:12 +0000710
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000711 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
712 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000713 if (IsFileLexer(ISEntry)) {
Yaron Keren65224612015-12-18 10:30:12 +0000714 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000715 if ((FE = HeaderInfo.LookupSubframeworkHeader(
Douglas Gregorf5f94522013-02-08 00:10:48 +0000716 Filename, CurFileEnt, SearchPath, RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000717 RequestingModule, SuggestedModule))) {
Ben Langmuir71e1a642014-05-05 21:44:13 +0000718 if (SuggestedModule && !LangOpts.AsmPreprocessor)
719 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
Richard Smith8d4e90b2016-03-14 17:52:37 +0000720 RequestingModule, RequestingModuleIsModuleInterface,
721 FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000722 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000723 }
724 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000725 }
726 }
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000728 // Otherwise, we really couldn't find the file.
Craig Topperd2d442c2014-05-17 23:10:59 +0000729 return nullptr;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000730}
731
Chris Lattnerf64b3522008-03-09 01:54:53 +0000732//===----------------------------------------------------------------------===//
733// Preprocessor Directive Handling.
734//===----------------------------------------------------------------------===//
735
David Blaikied5321242012-06-06 18:52:13 +0000736class Preprocessor::ResetMacroExpansionHelper {
737public:
738 ResetMacroExpansionHelper(Preprocessor *pp)
739 : PP(pp), save(pp->DisableMacroExpansion) {
740 if (pp->MacroExpansionInDirectivesOverride)
741 pp->DisableMacroExpansion = false;
742 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000743
David Blaikied5321242012-06-06 18:52:13 +0000744 ~ResetMacroExpansionHelper() {
745 PP->DisableMacroExpansion = save;
746 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000747
David Blaikied5321242012-06-06 18:52:13 +0000748private:
749 Preprocessor *PP;
750 bool save;
751};
752
Chris Lattnerf64b3522008-03-09 01:54:53 +0000753/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump11289f42009-09-09 15:08:12 +0000754/// at the start of a line. This consumes the directive, modifies the
Chris Lattnerf64b3522008-03-09 01:54:53 +0000755/// lexer/preprocessor state, and advances the lexer(s) so that the next token
756/// read is the correct one.
757void Preprocessor::HandleDirective(Token &Result) {
758 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattnerf64b3522008-03-09 01:54:53 +0000760 // We just parsed a # character at the start of a line, so we're in directive
761 // mode. Tell the lexer this so any newlines we see will be converted into an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000762 // EOD token (which terminates the directive).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000763 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000764 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Mike Stump11289f42009-09-09 15:08:12 +0000765
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000766 bool ImmediatelyAfterTopLevelIfndef =
767 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
768 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
769
Chris Lattnerf64b3522008-03-09 01:54:53 +0000770 ++NumDirectives;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000771
Chris Lattnerf64b3522008-03-09 01:54:53 +0000772 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump11289f42009-09-09 15:08:12 +0000773 // work, we have to remember if we had read any tokens *before* this
Chris Lattnerf64b3522008-03-09 01:54:53 +0000774 // pp-directive.
Chris Lattner8cf1f932009-12-14 04:54:40 +0000775 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattner2d17ab72009-03-18 21:00:25 +0000777 // Save the '#' token in case we need to return it later.
778 Token SavedHash = Result;
Mike Stump11289f42009-09-09 15:08:12 +0000779
Chris Lattnerf64b3522008-03-09 01:54:53 +0000780 // Read the next token, the directive flavor. This isn't expanded due to
781 // C99 6.10.3p8.
782 LexUnexpandedToken(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattnerf64b3522008-03-09 01:54:53 +0000784 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
785 // #define A(x) #x
786 // A(abc
787 // #warning blah
788 // def)
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000789 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
790 // not support this for #include-like directives, since that can result in
791 // terrible diagnostics, and does not work in GCC.
792 if (InMacroArgs) {
793 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
794 switch (II->getPPKeywordID()) {
795 case tok::pp_include:
796 case tok::pp_import:
797 case tok::pp_include_next:
798 case tok::pp___include_macros:
David Majnemerf2d3bc02014-12-28 07:42:49 +0000799 case tok::pp_pragma:
800 Diag(Result, diag::err_embedded_directive) << II->getName();
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000801 DiscardUntilEndOfDirective();
802 return;
803 default:
804 break;
805 }
806 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000807 Diag(Result, diag::ext_embedded_directive);
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
David Blaikied5321242012-06-06 18:52:13 +0000810 // Temporarily enable macro expansion if set so
811 // and reset to previous state when returning from this function.
812 ResetMacroExpansionHelper helper(this);
813
Chris Lattnerf64b3522008-03-09 01:54:53 +0000814 switch (Result.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000815 case tok::eod:
Chris Lattnerf64b3522008-03-09 01:54:53 +0000816 return; // null directive.
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000817 case tok::code_completion:
818 if (CodeComplete)
819 CodeComplete->CodeCompleteDirective(
820 CurPPLexer->getConditionalStackDepth() > 0);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000821 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000822 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000823 case tok::numeric_constant: // # 7 GNU line marker directive.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000824 if (getLangOpts().AsmPreprocessor)
Chris Lattner5eb8ae22009-03-18 20:41:10 +0000825 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner76e68962009-01-26 06:19:46 +0000826 return HandleDigitDirective(Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000827 default:
828 IdentifierInfo *II = Result.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +0000829 if (!II) break; // Not an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattnerf64b3522008-03-09 01:54:53 +0000831 // Ask what the preprocessor keyword ID is.
832 switch (II->getPPKeywordID()) {
833 default: break;
834 // C99 6.10.1 - Conditional Inclusion.
835 case tok::pp_if:
836 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
837 case tok::pp_ifdef:
838 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
839 case tok::pp_ifndef:
840 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
841 case tok::pp_elif:
842 return HandleElifDirective(Result);
843 case tok::pp_else:
844 return HandleElseDirective(Result);
845 case tok::pp_endif:
846 return HandleEndifDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000847
Chris Lattnerf64b3522008-03-09 01:54:53 +0000848 // C99 6.10.2 - Source File Inclusion.
849 case tok::pp_include:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000850 // Handle #include.
851 return HandleIncludeDirective(SavedHash.getLocation(), Result);
Chris Lattner14a7f392009-04-08 18:24:34 +0000852 case tok::pp___include_macros:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000853 // Handle -imacros.
854 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000855
Chris Lattnerf64b3522008-03-09 01:54:53 +0000856 // C99 6.10.3 - Macro Replacement.
857 case tok::pp_define:
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000858 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000859 case tok::pp_undef:
860 return HandleUndefDirective(Result);
861
862 // C99 6.10.4 - Line Control.
863 case tok::pp_line:
Chris Lattner100c65e2009-01-26 05:29:08 +0000864 return HandleLineDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattnerf64b3522008-03-09 01:54:53 +0000866 // C99 6.10.5 - Error Directive.
867 case tok::pp_error:
868 return HandleUserDiagnosticDirective(Result, false);
Mike Stump11289f42009-09-09 15:08:12 +0000869
Chris Lattnerf64b3522008-03-09 01:54:53 +0000870 // C99 6.10.6 - Pragma Directive.
871 case tok::pp_pragma:
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000872 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
Mike Stump11289f42009-09-09 15:08:12 +0000873
Chris Lattnerf64b3522008-03-09 01:54:53 +0000874 // GNU Extensions.
875 case tok::pp_import:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000876 return HandleImportDirective(SavedHash.getLocation(), Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000877 case tok::pp_include_next:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000878 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000879
Chris Lattnerf64b3522008-03-09 01:54:53 +0000880 case tok::pp_warning:
881 Diag(Result, diag::ext_pp_warning_directive);
882 return HandleUserDiagnosticDirective(Result, true);
883 case tok::pp_ident:
884 return HandleIdentSCCSDirective(Result);
885 case tok::pp_sccs:
886 return HandleIdentSCCSDirective(Result);
887 case tok::pp_assert:
888 //isExtension = true; // FIXME: implement #assert
889 break;
890 case tok::pp_unassert:
891 //isExtension = true; // FIXME: implement #unassert
892 break;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +0000893
Douglas Gregor663b48f2012-01-03 19:48:16 +0000894 case tok::pp___public_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000895 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000896 return HandleMacroPublicDirective(Result);
897 break;
898
Douglas Gregor663b48f2012-01-03 19:48:16 +0000899 case tok::pp___private_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000900 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000901 return HandleMacroPrivateDirective(Result);
902 break;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000903 }
904 break;
905 }
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner2d17ab72009-03-18 21:00:25 +0000907 // If this is a .S file, treat unknown # directives as non-preprocessor
908 // directives. This is important because # may be a comment or introduce
909 // various pseudo-ops. Just return the # token and push back the following
910 // token to be lexed next time.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000911 if (getLangOpts().AsmPreprocessor) {
David Blaikie2eabcc92016-02-09 18:52:09 +0000912 auto Toks = llvm::make_unique<Token[]>(2);
Chris Lattner2d17ab72009-03-18 21:00:25 +0000913 // Return the # and the token after it.
Mike Stump11289f42009-09-09 15:08:12 +0000914 Toks[0] = SavedHash;
Chris Lattner2d17ab72009-03-18 21:00:25 +0000915 Toks[1] = Result;
Chris Lattner56f64c12011-01-06 05:01:51 +0000916
917 // If the second token is a hashhash token, then we need to translate it to
918 // unknown so the token lexer doesn't try to perform token pasting.
919 if (Result.is(tok::hashhash))
920 Toks[1].setKind(tok::unknown);
921
Chris Lattner2d17ab72009-03-18 21:00:25 +0000922 // Enter this token stream so that we re-lex the tokens. Make sure to
923 // enable macro expansion, in case the token after the # is an identifier
924 // that is expanded.
David Blaikie2eabcc92016-02-09 18:52:09 +0000925 EnterTokenStream(std::move(Toks), 2, false);
Chris Lattner2d17ab72009-03-18 21:00:25 +0000926 return;
927 }
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattnerf64b3522008-03-09 01:54:53 +0000929 // If we reached here, the preprocessing token is not valid!
930 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump11289f42009-09-09 15:08:12 +0000931
Chris Lattnerf64b3522008-03-09 01:54:53 +0000932 // Read the rest of the PP line.
933 DiscardUntilEndOfDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattnerf64b3522008-03-09 01:54:53 +0000935 // Okay, we're done parsing the directive.
936}
937
Chris Lattner76e68962009-01-26 06:19:46 +0000938/// GetLineValue - Convert a numeric token into an unsigned value, emitting
939/// Diagnostic DiagID if it is invalid, and returning the value in Val.
940static bool GetLineValue(Token &DigitTok, unsigned &Val,
Michael Ilsemane910cc82013-04-10 01:04:18 +0000941 unsigned DiagID, Preprocessor &PP,
942 bool IsGNULineDirective=false) {
Chris Lattner76e68962009-01-26 06:19:46 +0000943 if (DigitTok.isNot(tok::numeric_constant)) {
944 PP.Diag(DigitTok, DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000945
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000946 if (DigitTok.isNot(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000947 PP.DiscardUntilEndOfDirective();
948 return true;
949 }
Mike Stump11289f42009-09-09 15:08:12 +0000950
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000951 SmallString<64> IntegerBuffer;
Chris Lattner76e68962009-01-26 06:19:46 +0000952 IntegerBuffer.resize(DigitTok.getLength());
953 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregordc970f02010-03-16 22:30:13 +0000954 bool Invalid = false;
955 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
956 if (Invalid)
957 return true;
958
Chris Lattnerd66f1722009-04-18 18:35:15 +0000959 // Verify that we have a simple digit-sequence, and compute the value. This
960 // is always a simple digit string computed in decimal, so we do this manually
961 // here.
962 Val = 0;
963 for (unsigned i = 0; i != ActualLength; ++i) {
Richard Smith7f2707a2013-09-26 18:13:20 +0000964 // C++1y [lex.fcon]p1:
965 // Optional separating single quotes in a digit-sequence are ignored
966 if (DigitTokBegin[i] == '\'')
967 continue;
968
Jordan Rosea7d03842013-02-08 22:30:41 +0000969 if (!isDigit(DigitTokBegin[i])) {
Chris Lattnerd66f1722009-04-18 18:35:15 +0000970 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
Michael Ilsemane910cc82013-04-10 01:04:18 +0000971 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000972 PP.DiscardUntilEndOfDirective();
973 return true;
974 }
Mike Stump11289f42009-09-09 15:08:12 +0000975
Chris Lattnerd66f1722009-04-18 18:35:15 +0000976 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
977 if (NextVal < Val) { // overflow.
978 PP.Diag(DigitTok, DiagID);
979 PP.DiscardUntilEndOfDirective();
980 return true;
981 }
982 Val = NextVal;
Chris Lattner76e68962009-01-26 06:19:46 +0000983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000985 if (DigitTokBegin[0] == '0' && Val)
Michael Ilsemane910cc82013-04-10 01:04:18 +0000986 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
987 << IsGNULineDirective;
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattner76e68962009-01-26 06:19:46 +0000989 return false;
990}
991
James Dennettf6333ac2012-06-22 05:46:07 +0000992/// \brief Handle a \#line directive: C99 6.10.4.
993///
994/// The two acceptable forms are:
995/// \verbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000996/// # line digit-sequence
997/// # line digit-sequence "s-char-sequence"
James Dennettf6333ac2012-06-22 05:46:07 +0000998/// \endverbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000999void Preprocessor::HandleLineDirective(Token &Tok) {
1000 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1001 // expanded.
1002 Token DigitTok;
1003 Lex(DigitTok);
1004
Chris Lattner100c65e2009-01-26 05:29:08 +00001005 // Validate the number and convert it to an unsigned.
Chris Lattner76e68962009-01-26 06:19:46 +00001006 unsigned LineNo;
Chris Lattnerd66f1722009-04-18 18:35:15 +00001007 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner100c65e2009-01-26 05:29:08 +00001008 return;
Fariborz Jahanian0638c152012-06-26 21:19:20 +00001009
1010 if (LineNo == 0)
1011 Diag(DigitTok, diag::ext_pp_line_zero);
Chris Lattner100c65e2009-01-26 05:29:08 +00001012
Chris Lattner76e68962009-01-26 06:19:46 +00001013 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1014 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Eli Friedman192e0342011-10-10 23:35:28 +00001015 unsigned LineLimit = 32768U;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001016 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Eli Friedman192e0342011-10-10 23:35:28 +00001017 LineLimit = 2147483648U;
Chris Lattner100c65e2009-01-26 05:29:08 +00001018 if (LineNo >= LineLimit)
1019 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001020 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
Richard Smithacd4d3d2011-10-15 01:18:56 +00001021 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
Mike Stump11289f42009-09-09 15:08:12 +00001022
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001023 int FilenameID = -1;
Chris Lattner100c65e2009-01-26 05:29:08 +00001024 Token StrTok;
1025 Lex(StrTok);
1026
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001027 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1028 // string followed by eod.
1029 if (StrTok.is(tok::eod))
Chris Lattner100c65e2009-01-26 05:29:08 +00001030 ; // ok
1031 else if (StrTok.isNot(tok::string_literal)) {
1032 Diag(StrTok, diag::err_pp_line_invalid_filename);
Richard Smithd67aea22012-03-06 03:21:47 +00001033 return DiscardUntilEndOfDirective();
1034 } else if (StrTok.hasUDSuffix()) {
1035 Diag(StrTok, diag::err_invalid_string_udl);
1036 return DiscardUntilEndOfDirective();
Chris Lattner100c65e2009-01-26 05:29:08 +00001037 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001038 // Parse and validate the string, converting it into a unique ID.
Craig Topper9d5583e2014-06-26 04:58:39 +00001039 StringLiteralParser Literal(StrTok, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001040 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001041 if (Literal.hadError)
1042 return DiscardUntilEndOfDirective();
1043 if (Literal.Pascal) {
1044 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1045 return DiscardUntilEndOfDirective();
1046 }
Jay Foad9a6b0982011-06-21 15:13:30 +00001047 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +00001048
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001049 // Verify that there is nothing after the string, other than EOD. Because
Chris Lattner0003c272009-04-17 23:30:53 +00001050 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1051 CheckEndOfDirective("line", true);
Chris Lattner100c65e2009-01-26 05:29:08 +00001052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattner1eaa70a2009-02-03 21:52:55 +00001054 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattner839150e2009-03-27 17:13:49 +00001056 if (Callbacks)
Chris Lattnerc745cec2010-04-14 04:28:50 +00001057 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1058 PPCallbacks::RenameFile,
Chris Lattner839150e2009-03-27 17:13:49 +00001059 SrcMgr::C_User);
Chris Lattner100c65e2009-01-26 05:29:08 +00001060}
1061
Chris Lattner76e68962009-01-26 06:19:46 +00001062/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1063/// marker directive.
1064static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1065 bool &IsSystemHeader, bool &IsExternCHeader,
1066 Preprocessor &PP) {
1067 unsigned FlagVal;
1068 Token FlagTok;
1069 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001070 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001071 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1072 return true;
1073
1074 if (FlagVal == 1) {
1075 IsFileEntry = true;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Chris Lattner76e68962009-01-26 06:19:46 +00001077 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001078 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001079 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1080 return true;
1081 } else if (FlagVal == 2) {
1082 IsFileExit = true;
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattner1c967782009-02-04 06:25:26 +00001084 SourceManager &SM = PP.getSourceManager();
1085 // If we are leaving the current presumed file, check to make sure the
1086 // presumed include stack isn't empty!
1087 FileID CurFileID =
Chandler Carruthc7ca5212011-07-25 20:52:32 +00001088 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
Chris Lattner1c967782009-02-04 06:25:26 +00001089 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +00001090 if (PLoc.isInvalid())
1091 return true;
1092
Chris Lattner1c967782009-02-04 06:25:26 +00001093 // If there is no include loc (main file) or if the include loc is in a
1094 // different physical file, then we aren't in a "1" line marker flag region.
1095 SourceLocation IncLoc = PLoc.getIncludeLoc();
1096 if (IncLoc.isInvalid() ||
Chandler Carruthc7ca5212011-07-25 20:52:32 +00001097 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
Chris Lattner1c967782009-02-04 06:25:26 +00001098 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1099 PP.DiscardUntilEndOfDirective();
1100 return true;
1101 }
Mike Stump11289f42009-09-09 15:08:12 +00001102
Chris Lattner76e68962009-01-26 06:19:46 +00001103 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001104 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001105 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1106 return true;
1107 }
1108
1109 // We must have 3 if there are still flags.
1110 if (FlagVal != 3) {
1111 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001112 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001113 return true;
1114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattner76e68962009-01-26 06:19:46 +00001116 IsSystemHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner76e68962009-01-26 06:19:46 +00001118 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001119 if (FlagTok.is(tok::eod)) return false;
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001120 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner76e68962009-01-26 06:19:46 +00001121 return true;
1122
1123 // We must have 4 if there is yet another flag.
1124 if (FlagVal != 4) {
1125 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001126 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001127 return true;
1128 }
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattner76e68962009-01-26 06:19:46 +00001130 IsExternCHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner76e68962009-01-26 06:19:46 +00001132 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001133 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001134
1135 // There are no more valid flags here.
1136 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001137 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001138 return true;
1139}
1140
1141/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1142/// one of the following forms:
1143///
1144/// # 42
Mike Stump11289f42009-09-09 15:08:12 +00001145/// # 42 "file" ('1' | '2')?
Chris Lattner76e68962009-01-26 06:19:46 +00001146/// # 42 "file" ('1' | '2')? '3' '4'?
1147///
1148void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1149 // Validate the number and convert it to an unsigned. GNU does not have a
1150 // line # limit other than it fit in 32-bits.
1151 unsigned LineNo;
1152 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
Michael Ilsemane910cc82013-04-10 01:04:18 +00001153 *this, true))
Chris Lattner76e68962009-01-26 06:19:46 +00001154 return;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattner76e68962009-01-26 06:19:46 +00001156 Token StrTok;
1157 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001158
Chris Lattner76e68962009-01-26 06:19:46 +00001159 bool IsFileEntry = false, IsFileExit = false;
1160 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001161 int FilenameID = -1;
1162
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001163 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1164 // string followed by eod.
1165 if (StrTok.is(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +00001166 ; // ok
1167 else if (StrTok.isNot(tok::string_literal)) {
1168 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001169 return DiscardUntilEndOfDirective();
Richard Smithd67aea22012-03-06 03:21:47 +00001170 } else if (StrTok.hasUDSuffix()) {
1171 Diag(StrTok, diag::err_invalid_string_udl);
1172 return DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001173 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001174 // Parse and validate the string, converting it into a unique ID.
Craig Topper9d5583e2014-06-26 04:58:39 +00001175 StringLiteralParser Literal(StrTok, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001176 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001177 if (Literal.hadError)
1178 return DiscardUntilEndOfDirective();
1179 if (Literal.Pascal) {
1180 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1181 return DiscardUntilEndOfDirective();
1182 }
Jay Foad9a6b0982011-06-21 15:13:30 +00001183 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +00001184
Chris Lattner76e68962009-01-26 06:19:46 +00001185 // If a filename was present, read any flags that are present.
Mike Stump11289f42009-09-09 15:08:12 +00001186 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001187 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner76e68962009-01-26 06:19:46 +00001188 return;
Chris Lattner76e68962009-01-26 06:19:46 +00001189 }
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001191 // Create a line note with this information.
1192 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump11289f42009-09-09 15:08:12 +00001193 IsFileEntry, IsFileExit,
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001194 IsSystemHeader, IsExternCHeader);
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattner839150e2009-03-27 17:13:49 +00001196 // If the preprocessor has callbacks installed, notify them of the #line
1197 // change. This is used so that the line marker comes out in -E mode for
1198 // example.
1199 if (Callbacks) {
1200 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1201 if (IsFileEntry)
1202 Reason = PPCallbacks::EnterFile;
1203 else if (IsFileExit)
1204 Reason = PPCallbacks::ExitFile;
1205 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1206 if (IsExternCHeader)
1207 FileKind = SrcMgr::C_ExternCSystem;
1208 else if (IsSystemHeader)
1209 FileKind = SrcMgr::C_System;
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattnerc745cec2010-04-14 04:28:50 +00001211 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner839150e2009-03-27 17:13:49 +00001212 }
Chris Lattner76e68962009-01-26 06:19:46 +00001213}
1214
Chris Lattner38d7fd22009-01-26 05:30:54 +00001215/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1216///
Mike Stump11289f42009-09-09 15:08:12 +00001217void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001218 bool isWarning) {
Chris Lattner38d7fd22009-01-26 05:30:54 +00001219 // PTH doesn't emit #warning or #error directives.
1220 if (CurPTHLexer)
Chris Lattner100c65e2009-01-26 05:29:08 +00001221 return CurPTHLexer->DiscardToEndOfLine();
1222
Chris Lattnerf64b3522008-03-09 01:54:53 +00001223 // Read the rest of the line raw. We do this because we don't want macros
1224 // to be expanded and we don't require that the tokens be valid preprocessing
1225 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1226 // collapse multiple consequtive white space between tokens, but this isn't
1227 // specified by the standard.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001228 SmallString<128> Message;
1229 CurLexer->ReadToEndOfLine(&Message);
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001230
1231 // Find the first non-whitespace character, so that we can make the
1232 // diagnostic more succinct.
David Majnemerbf7e0c62016-02-24 22:07:26 +00001233 StringRef Msg = StringRef(Message).ltrim(' ');
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001234
Chris Lattner100c65e2009-01-26 05:29:08 +00001235 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001236 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001237 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001238 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001239}
1240
1241/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1242///
1243void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1244 // Yes, this directive is an extension.
1245 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattnerf64b3522008-03-09 01:54:53 +00001247 // Read the string argument.
1248 Token StrTok;
1249 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001250
Chris Lattnerf64b3522008-03-09 01:54:53 +00001251 // If the token kind isn't a string, it's a malformed directive.
1252 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001253 StrTok.isNot(tok::wide_string_literal)) {
1254 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001255 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001256 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001257 return;
1258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Richard Smithd67aea22012-03-06 03:21:47 +00001260 if (StrTok.hasUDSuffix()) {
1261 Diag(StrTok, diag::err_invalid_string_udl);
1262 return DiscardUntilEndOfDirective();
1263 }
1264
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001265 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001266 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001267
Douglas Gregordc970f02010-03-16 22:30:13 +00001268 if (Callbacks) {
1269 bool Invalid = false;
1270 std::string Str = getSpelling(StrTok, &Invalid);
1271 if (!Invalid)
1272 Callbacks->Ident(Tok.getLocation(), Str);
1273 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001274}
1275
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001276/// \brief Handle a #public directive.
1277void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001278 Token MacroNameTok;
Serge Pavlovd024f522014-10-24 17:31:32 +00001279 ReadMacroName(MacroNameTok, MU_Undef);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001280
1281 // Error reading macro name? If so, diagnostic already issued.
1282 if (MacroNameTok.is(tok::eod))
1283 return;
1284
Douglas Gregor663b48f2012-01-03 19:48:16 +00001285 // Check to see if this is the last token on the #__public_macro line.
1286 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001287
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001288 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001289 // Okay, we finally have a valid identifier to undef.
Richard Smith20e883e2015-04-29 23:20:19 +00001290 MacroDirective *MD = getLocalMacroDirective(II);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001291
1292 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001293 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001294 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001295 return;
1296 }
1297
1298 // Note that this macro has now been exported.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001299 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1300 MacroNameTok.getLocation(), /*IsPublic=*/true));
Douglas Gregorebf00492011-10-17 15:32:29 +00001301}
1302
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001303/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001304void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1305 Token MacroNameTok;
Serge Pavlovd024f522014-10-24 17:31:32 +00001306 ReadMacroName(MacroNameTok, MU_Undef);
Douglas Gregorebf00492011-10-17 15:32:29 +00001307
1308 // Error reading macro name? If so, diagnostic already issued.
1309 if (MacroNameTok.is(tok::eod))
1310 return;
1311
Douglas Gregor663b48f2012-01-03 19:48:16 +00001312 // Check to see if this is the last token on the #__private_macro line.
1313 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001314
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001315 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregorebf00492011-10-17 15:32:29 +00001316 // Okay, we finally have a valid identifier to undef.
Richard Smith20e883e2015-04-29 23:20:19 +00001317 MacroDirective *MD = getLocalMacroDirective(II);
Douglas Gregorebf00492011-10-17 15:32:29 +00001318
1319 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001320 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001321 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregorebf00492011-10-17 15:32:29 +00001322 return;
1323 }
1324
1325 // Note that this macro has now been marked private.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001326 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1327 MacroNameTok.getLocation(), /*IsPublic=*/false));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001328}
1329
Chris Lattnerf64b3522008-03-09 01:54:53 +00001330//===----------------------------------------------------------------------===//
1331// Preprocessor Include Directive Handling.
1332//===----------------------------------------------------------------------===//
1333
1334/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
James Dennettf6333ac2012-06-22 05:46:07 +00001335/// checked and spelled filename, e.g. as an operand of \#include. This returns
Chris Lattnerf64b3522008-03-09 01:54:53 +00001336/// true if the input filename was in <>'s or false if it were in ""'s. The
1337/// caller is expected to provide a buffer that is large enough to hold the
1338/// spelling of the filename, but is also expected to handle the case when
1339/// this method decides to use a different buffer.
1340bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001341 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001342 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001343 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001344
Chris Lattnerf64b3522008-03-09 01:54:53 +00001345 // Make sure the filename is <x> or "x".
1346 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001347 if (Buffer[0] == '<') {
1348 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001349 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001350 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001351 return true;
1352 }
1353 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001354 } else if (Buffer[0] == '"') {
1355 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001356 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001357 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001358 return true;
1359 }
1360 isAngled = false;
1361 } else {
1362 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001363 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001364 return true;
1365 }
Mike Stump11289f42009-09-09 15:08:12 +00001366
Chris Lattnerf64b3522008-03-09 01:54:53 +00001367 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001368 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001369 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001370 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001371 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001372 }
Mike Stump11289f42009-09-09 15:08:12 +00001373
Chris Lattnerf64b3522008-03-09 01:54:53 +00001374 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001375 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001376 return isAngled;
1377}
1378
James Dennett4a4f72d2013-11-27 01:27:40 +00001379// \brief Handle cases where the \#include name is expanded from a macro
1380// as multiple tokens, which need to be glued together.
1381//
1382// This occurs for code like:
1383// \code
1384// \#define FOO <a/b.h>
1385// \#include FOO
1386// \endcode
1387// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1388//
1389// This code concatenates and consumes tokens up to the '>' token. It returns
1390// false if the > was found, otherwise it returns true if it finds and consumes
1391// the EOD marker.
1392bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001393 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001394 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001395
John Thompsonb5353522009-10-30 13:49:06 +00001396 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001397 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001398 End = CurTok.getLocation();
1399
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001400 // FIXME: Provide code completion for #includes.
1401 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001402 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001403 Lex(CurTok);
1404 continue;
1405 }
1406
Chris Lattnerf64b3522008-03-09 01:54:53 +00001407 // Append the spelling of this token to the buffer. If there was a space
1408 // before it, add it now.
1409 if (CurTok.hasLeadingSpace())
1410 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001411
Chris Lattnerf64b3522008-03-09 01:54:53 +00001412 // Get the spelling of the token, directly into FilenameBuffer if possible.
1413 unsigned PreAppendSize = FilenameBuffer.size();
1414 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001415
Chris Lattnerf64b3522008-03-09 01:54:53 +00001416 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001417 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001418
Chris Lattnerf64b3522008-03-09 01:54:53 +00001419 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1420 if (BufPtr != &FilenameBuffer[PreAppendSize])
1421 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattnerf64b3522008-03-09 01:54:53 +00001423 // Resize FilenameBuffer to the correct size.
1424 if (CurTok.getLength() != ActualLen)
1425 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001426
Chris Lattnerf64b3522008-03-09 01:54:53 +00001427 // If we found the '>' marker, return success.
1428 if (CurTok.is(tok::greater))
1429 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001430
John Thompsonb5353522009-10-30 13:49:06 +00001431 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001432 }
1433
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001434 // If we hit the eod marker, emit an error and return true so that the caller
1435 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001436 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001437 return true;
1438}
1439
Richard Smith34f30512013-11-23 04:06:09 +00001440/// \brief Push a token onto the token stream containing an annotation.
1441static void EnterAnnotationToken(Preprocessor &PP,
1442 SourceLocation Begin, SourceLocation End,
1443 tok::TokenKind Kind, void *AnnotationVal) {
Richard Smithdbbc5232015-05-14 02:25:44 +00001444 // FIXME: Produce this as the current token directly, rather than
1445 // allocating a new token for it.
David Blaikie2eabcc92016-02-09 18:52:09 +00001446 auto Tok = llvm::make_unique<Token[]>(1);
Richard Smith34f30512013-11-23 04:06:09 +00001447 Tok[0].startToken();
1448 Tok[0].setKind(Kind);
1449 Tok[0].setLocation(Begin);
1450 Tok[0].setAnnotationEndLoc(End);
1451 Tok[0].setAnnotationValue(AnnotationVal);
David Blaikie2eabcc92016-02-09 18:52:09 +00001452 PP.EnterTokenStream(std::move(Tok), 1, true);
Richard Smith34f30512013-11-23 04:06:09 +00001453}
1454
Richard Smith63b6fce2015-05-18 04:45:41 +00001455/// \brief Produce a diagnostic informing the user that a #include or similar
1456/// was implicitly treated as a module import.
1457static void diagnoseAutoModuleImport(
1458 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1459 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1460 SourceLocation PathEnd) {
1461 assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1462
1463 SmallString<128> PathString;
1464 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1465 if (I)
1466 PathString += '.';
1467 PathString += Path[I].first->getName();
1468 }
1469 int IncludeKind = 0;
1470
1471 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1472 case tok::pp_include:
1473 IncludeKind = 0;
1474 break;
1475
1476 case tok::pp_import:
1477 IncludeKind = 1;
1478 break;
1479
1480 case tok::pp_include_next:
1481 IncludeKind = 2;
1482 break;
1483
1484 case tok::pp___include_macros:
1485 IncludeKind = 3;
1486 break;
1487
1488 default:
1489 llvm_unreachable("unknown include directive kind");
1490 }
1491
1492 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1493 /*IsTokenRange=*/false);
1494 PP.Diag(HashLoc, diag::warn_auto_module_import)
1495 << IncludeKind << PathString
1496 << FixItHint::CreateReplacement(ReplaceRange,
1497 ("@import " + PathString + ";").str());
1498}
1499
James Dennettf6333ac2012-06-22 05:46:07 +00001500/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1501/// the file to be included from the lexer, then include it! This is a common
1502/// routine with functionality shared between \#include, \#include_next and
1503/// \#import. LookupFrom is set when this is a \#include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001504/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001505void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1506 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001507 const DirectoryLookup *LookupFrom,
Richard Smith25d50752014-10-20 00:15:49 +00001508 const FileEntry *LookupFromFile,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001509 bool isImport) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001510 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001511 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001512
Chris Lattnerf64b3522008-03-09 01:54:53 +00001513 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001514 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001515 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001516 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001517 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001518
Chris Lattnerf64b3522008-03-09 01:54:53 +00001519 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001520 case tok::eod:
1521 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001522 return;
Mike Stump11289f42009-09-09 15:08:12 +00001523
Chris Lattnerf64b3522008-03-09 01:54:53 +00001524 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001525 case tok::string_literal:
1526 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001527 End = FilenameTok.getLocation();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001528 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001529 break;
Mike Stump11289f42009-09-09 15:08:12 +00001530
Chris Lattnerf64b3522008-03-09 01:54:53 +00001531 case tok::less:
1532 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1533 // case, glue the tokens together into FilenameBuffer and interpret those.
1534 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001535 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001536 return; // Found <eod> but no ">"? Diagnostic already emitted.
Yaron Keren92e1b622015-03-18 10:17:07 +00001537 Filename = FilenameBuffer;
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001538 CharEnd = End.getLocWithOffset(1);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001539 break;
1540 default:
1541 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1542 DiscardUntilEndOfDirective();
1543 return;
1544 }
Mike Stump11289f42009-09-09 15:08:12 +00001545
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001546 CharSourceRange FilenameRange
1547 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
Aaron Ballman611306e2012-03-02 22:51:54 +00001548 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001549 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001550 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001551 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1552 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001553 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001554 DiscardUntilEndOfDirective();
1555 return;
1556 }
Mike Stump11289f42009-09-09 15:08:12 +00001557
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001558 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001559 // we allow macros that expand to nothing after the filename, because this
1560 // falls into the category of "#include pp-tokens new-line" specified in
1561 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001562 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001563
1564 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001565 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1566 Diag(FilenameTok, diag::err_pp_include_too_deep);
1567 return;
1568 }
Mike Stump11289f42009-09-09 15:08:12 +00001569
John McCall32f5fe12011-09-30 05:12:12 +00001570 // Complain about attempts to #include files in an audit pragma.
1571 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1572 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1573 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1574
1575 // Immediately leave the pragma.
1576 PragmaARCCFCodeAuditedLoc = SourceLocation();
1577 }
1578
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001579 // Complain about attempts to #include files in an assume-nonnull pragma.
1580 if (PragmaAssumeNonNullLoc.isValid()) {
1581 Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1582 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1583
1584 // Immediately leave the pragma.
1585 PragmaAssumeNonNullLoc = SourceLocation();
1586 }
1587
Aaron Ballman611306e2012-03-02 22:51:54 +00001588 if (HeaderInfo.HasIncludeAliasMap()) {
1589 // Map the filename with the brackets still attached. If the name doesn't
1590 // map to anything, fall back on the filename we've already gotten the
1591 // spelling for.
1592 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1593 if (!NewName.empty())
1594 Filename = NewName;
1595 }
1596
Chris Lattnerf64b3522008-03-09 01:54:53 +00001597 // Search include directories.
1598 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001599 SmallString<1024> SearchPath;
1600 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001601 // We get the raw path only if we have 'Callbacks' to which we later pass
1602 // the path.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001603 ModuleMap::KnownHeader SuggestedModule;
1604 SourceLocation FilenameLoc = FilenameTok.getLocation();
Saleem Abdulrasool729b7d32014-03-12 02:26:08 +00001605 SmallString<128> NormalizedPath;
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001606 if (LangOpts.MSVCCompat) {
1607 NormalizedPath = Filename.str();
Yaron Keren1801d1b2014-08-09 18:13:01 +00001608#ifndef LLVM_ON_WIN32
Rafael Espindolaf6002232014-08-08 21:31:04 +00001609 llvm::sys::path::native(NormalizedPath);
Yaron Keren1801d1b2014-08-09 18:13:01 +00001610#endif
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001611 }
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001612 const FileEntry *File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001613 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Richard Smith25d50752014-10-20 00:15:49 +00001614 isAngled, LookupFrom, LookupFromFile, CurDir,
1615 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
Richard Smith47972af2015-06-16 00:08:24 +00001616 &SuggestedModule);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001617
Richard Smithdbbc5232015-05-14 02:25:44 +00001618 if (!File) {
1619 if (Callbacks) {
Douglas Gregor11729f02011-11-30 18:12:06 +00001620 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001621 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001622 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1623 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1624 // Add the recovery path to the list of search paths.
Daniel Dunbarae4feb62013-01-25 01:50:28 +00001625 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Douglas Gregor11729f02011-11-30 18:12:06 +00001626 HeaderInfo.AddSearchPath(DL, isAngled);
1627
1628 // Try the lookup again, skipping the cache.
Richard Smith25d50752014-10-20 00:15:49 +00001629 File = LookupFile(
1630 FilenameLoc,
1631 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1632 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
Richard Smith47972af2015-06-16 00:08:24 +00001633 &SuggestedModule, /*SkipCache*/ true);
Douglas Gregor11729f02011-11-30 18:12:06 +00001634 }
1635 }
1636 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001637
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001638 if (!SuppressIncludeNotFoundError) {
1639 // If the file could not be located and it was included via angle
1640 // brackets, we can attempt a lookup as though it were a quoted path to
1641 // provide the user with a possible fixit.
1642 if (isAngled) {
Daniel Jasper07e6c402013-08-05 20:26:17 +00001643 File = LookupFile(
Richard Smith25d50752014-10-20 00:15:49 +00001644 FilenameLoc,
1645 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1646 LookupFrom, LookupFromFile, CurDir,
1647 Callbacks ? &SearchPath : nullptr,
Craig Topperd2d442c2014-05-17 23:10:59 +00001648 Callbacks ? &RelativePath : nullptr,
Richard Smith47972af2015-06-16 00:08:24 +00001649 &SuggestedModule);
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001650 if (File) {
1651 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1652 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1653 Filename <<
1654 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1655 }
1656 }
Richard Smithdbbc5232015-05-14 02:25:44 +00001657
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001658 // If the file is still not found, just go with the vanilla diagnostic
1659 if (!File)
1660 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1661 }
Douglas Gregor11729f02011-11-30 18:12:06 +00001662 }
1663
Richard Smith63b6fce2015-05-18 04:45:41 +00001664 // Should we enter the source file? Set to false if either the source file is
1665 // known to have no effect beyond its effect on module visibility -- that is,
1666 // if it's got an include guard that is already defined or is a modular header
1667 // we've imported or already built.
1668 bool ShouldEnter = true;
Richard Smithdbbc5232015-05-14 02:25:44 +00001669
Richard Smith63b6fce2015-05-18 04:45:41 +00001670 // Determine whether we should try to import the module for this #include, if
1671 // there is one. Don't do so if precompiled module support is disabled or we
1672 // are processing this module textually (because we're building the module).
1673 if (File && SuggestedModule && getLangOpts().Modules &&
1674 SuggestedModule.getModule()->getTopLevelModuleName() !=
Richard Smith7e82e012016-02-19 22:25:36 +00001675 getLangOpts().CurrentModule) {
Sean Silva8b7c0392015-08-17 16:39:30 +00001676 // If this include corresponds to a module but that module is
1677 // unavailable, diagnose the situation and bail out.
Richard Smith58df3432016-04-12 19:58:30 +00001678 // FIXME: Remove this; loadModule does the same check (but produces
1679 // slightly worse diagnostics).
1680 if (!SuggestedModule.getModule()->isAvailable() &&
Richard Smith68935702016-04-12 20:20:33 +00001681 !SuggestedModule.getModule()
1682 ->getTopLevelModule()
1683 ->HasIncompatibleModuleFile) {
Sean Silva8b7c0392015-08-17 16:39:30 +00001684 clang::Module::Requirement Requirement;
1685 clang::Module::UnresolvedHeaderDirective MissingHeader;
1686 Module *M = SuggestedModule.getModule();
1687 // Identify the cause.
1688 (void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
1689 MissingHeader);
1690 if (MissingHeader.FileNameLoc.isValid()) {
1691 Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1692 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1693 } else {
1694 Diag(M->DefinitionLoc, diag::err_module_unavailable)
1695 << M->getFullModuleName() << Requirement.second << Requirement.first;
1696 }
1697 Diag(FilenameTok.getLocation(),
1698 diag::note_implicit_top_level_module_import_here)
1699 << M->getTopLevelModuleName();
1700 return;
1701 }
1702
Douglas Gregor71944202011-11-30 00:36:36 +00001703 // Compute the module access path corresponding to this module.
1704 // FIXME: Should we have a second loadModule() overload to avoid this
1705 // extra lookup step?
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001706 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001707 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001708 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1709 FilenameTok.getLocation()));
1710 std::reverse(Path.begin(), Path.end());
1711
Douglas Gregor41e115a2011-11-30 18:02:36 +00001712 // Warn that we're replacing the include/import with a module import.
Richard Smith63b6fce2015-05-18 04:45:41 +00001713 // We only do this in Objective-C, where we have a module-import syntax.
1714 if (getLangOpts().ObjC2)
1715 diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
Douglas Gregor41e115a2011-11-30 18:02:36 +00001716
Richard Smith10434f32015-05-02 02:08:26 +00001717 // Load the module to import its macros. We'll make the declarations
Richard Smithce587f52013-11-15 04:24:58 +00001718 // visible when the parser gets here.
Richard Smithdbbc5232015-05-14 02:25:44 +00001719 // FIXME: Pass SuggestedModule in here rather than converting it to a path
1720 // and making the module loader convert it back again.
Richard Smith10434f32015-05-02 02:08:26 +00001721 ModuleLoadResult Imported = TheModuleLoader.loadModule(
1722 IncludeTok.getLocation(), Path, Module::Hidden,
1723 /*IsIncludeDirective=*/true);
Craig Topperd2d442c2014-05-17 23:10:59 +00001724 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
Argyrios Kyrtzidis051b4432012-09-29 01:06:01 +00001725 "the imported module is different than the suggested one");
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00001726
Richard Smith63b6fce2015-05-18 04:45:41 +00001727 if (Imported)
1728 ShouldEnter = false;
1729 else if (Imported.isMissingExpected()) {
1730 // We failed to find a submodule that we assumed would exist (because it
1731 // was in the directory of an umbrella header, for instance), but no
1732 // actual module exists for it (because the umbrella header is
1733 // incomplete). Treat this as a textual inclusion.
1734 SuggestedModule = ModuleMap::KnownHeader();
1735 } else {
1736 // We hit an error processing the import. Bail out.
1737 if (hadModuleLoaderFatalFailure()) {
1738 // With a fatal failure in the module loader, we abort parsing.
1739 Token &Result = IncludeTok;
1740 if (CurLexer) {
1741 Result.startToken();
1742 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1743 CurLexer->cutOffLexing();
1744 } else {
1745 assert(CurPTHLexer && "#include but no current lexer set!");
1746 CurPTHLexer->getEOF(Result);
1747 }
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00001748 }
1749 return;
1750 }
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001751 }
1752
Richard Smith63b6fce2015-05-18 04:45:41 +00001753 if (Callbacks) {
1754 // Notify the callback object that we've seen an inclusion directive.
1755 Callbacks->InclusionDirective(
1756 HashLoc, IncludeTok,
1757 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1758 FilenameRange, File, SearchPath, RelativePath,
1759 ShouldEnter ? nullptr : SuggestedModule.getModule());
Douglas Gregor97eec242011-09-15 22:00:41 +00001760 }
Richard Smith63b6fce2015-05-18 04:45:41 +00001761
1762 if (!File)
1763 return;
Douglas Gregor97eec242011-09-15 22:00:41 +00001764
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001765 // The #included file will be considered to be a system header if either it is
1766 // in a system include directory, or if the #includer is a system include
1767 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001768 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001769 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001770 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001771
Richard Smith54ef4c32015-05-19 19:58:11 +00001772 // FIXME: If we have a suggested module, and we've already visited this file,
1773 // don't bother entering it again. We know it has no further effect.
1774
Chris Lattner72286d62010-04-19 20:44:31 +00001775 // Ask HeaderInfo if we should enter this #include file. If not, #including
Richard Smith54ef4c32015-05-19 19:58:11 +00001776 // this file will have no effect.
Richard Smith63b6fce2015-05-18 04:45:41 +00001777 if (ShouldEnter &&
Richard Smith035f6dc2015-07-01 01:51:38 +00001778 !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1779 SuggestedModule.getModule())) {
Richard Smith63b6fce2015-05-18 04:45:41 +00001780 ShouldEnter = false;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001781 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001782 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Richard Smith63b6fce2015-05-18 04:45:41 +00001783 }
Richard Smithdbbc5232015-05-14 02:25:44 +00001784
Richard Smith63b6fce2015-05-18 04:45:41 +00001785 // If we don't need to enter the file, stop now.
1786 if (!ShouldEnter) {
Richard Smithdbbc5232015-05-14 02:25:44 +00001787 // If this is a module import, make it visible if needed.
Richard Smitha0aafa32015-05-18 03:52:30 +00001788 if (auto *M = SuggestedModule.getModule()) {
1789 makeModuleVisible(M, HashLoc);
Richard Smithdbbc5232015-05-14 02:25:44 +00001790
1791 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1792 tok::pp___include_macros)
Richard Smitha0aafa32015-05-18 03:52:30 +00001793 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
Richard Smithdbbc5232015-05-14 02:25:44 +00001794 }
Chris Lattner72286d62010-04-19 20:44:31 +00001795 return;
1796 }
1797
Chris Lattnerf64b3522008-03-09 01:54:53 +00001798 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001799 SourceLocation IncludePos = End;
1800 // If the filename string was the result of macro expansions, set the include
1801 // position on the file where it will be included and after the expansions.
1802 if (IncludePos.isMacroID())
1803 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1804 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Yaron Keren8b563662015-10-03 10:46:20 +00001805 assert(FID.isValid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001806
Richard Smith34f30512013-11-23 04:06:09 +00001807 // If all is good, enter the new file!
Richard Smith67294e22014-01-31 20:47:44 +00001808 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1809 return;
Richard Smith34f30512013-11-23 04:06:09 +00001810
Richard Smitha0aafa32015-05-18 03:52:30 +00001811 // Determine if we're switching to building a new submodule, and which one.
Richard Smitha0aafa32015-05-18 03:52:30 +00001812 if (auto *M = SuggestedModule.getModule()) {
Richard Smith67294e22014-01-31 20:47:44 +00001813 assert(!CurSubmodule && "should not have marked this as a module yet");
Richard Smitha0aafa32015-05-18 03:52:30 +00001814 CurSubmodule = M;
Richard Smith67294e22014-01-31 20:47:44 +00001815
Richard Smitha0aafa32015-05-18 03:52:30 +00001816 // Let the macro handling code know that any future macros are within
1817 // the new submodule.
1818 EnterSubmodule(M, HashLoc);
Richard Smithb8b2ed62015-04-23 18:18:26 +00001819
Richard Smitha0aafa32015-05-18 03:52:30 +00001820 // Let the parser know that any future declarations are within the new
1821 // submodule.
1822 // FIXME: There's no point doing this if we're handling a #__include_macros
1823 // directive.
1824 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
Richard Smith67294e22014-01-31 20:47:44 +00001825 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001826}
1827
James Dennettf6333ac2012-06-22 05:46:07 +00001828/// HandleIncludeNextDirective - Implements \#include_next.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001829///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001830void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1831 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001832 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001833
Chris Lattnerf64b3522008-03-09 01:54:53 +00001834 // #include_next is like #include, except that we start searching after
1835 // the current found directory. If we can't do this, issue a
1836 // diagnostic.
1837 const DirectoryLookup *Lookup = CurDirLookup;
Richard Smith25d50752014-10-20 00:15:49 +00001838 const FileEntry *LookupFromFile = nullptr;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001839 if (isInPrimaryFile()) {
Craig Topperd2d442c2014-05-17 23:10:59 +00001840 Lookup = nullptr;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001841 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Richard Smith25d50752014-10-20 00:15:49 +00001842 } else if (CurSubmodule) {
1843 // Start looking up in the directory *after* the one in which the current
1844 // file would be found, if any.
1845 assert(CurPPLexer && "#include_next directive in macro?");
1846 LookupFromFile = CurPPLexer->getFileEntry();
1847 Lookup = nullptr;
Craig Topperd2d442c2014-05-17 23:10:59 +00001848 } else if (!Lookup) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001849 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1850 } else {
1851 // Start looking up in the next directory.
1852 ++Lookup;
1853 }
Mike Stump11289f42009-09-09 15:08:12 +00001854
Richard Smith25d50752014-10-20 00:15:49 +00001855 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
1856 LookupFromFile);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001857}
1858
James Dennettf6333ac2012-06-22 05:46:07 +00001859/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
Aaron Ballman0467f552012-03-18 03:10:37 +00001860void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1861 // The Microsoft #import directive takes a type library and generates header
1862 // files from it, and includes those. This is beyond the scope of what clang
1863 // does, so we ignore it and error out. However, #import can optionally have
1864 // trailing attributes that span multiple lines. We're going to eat those
1865 // so we can continue processing from there.
1866 Diag(Tok, diag::err_pp_import_directive_ms );
1867
1868 // Read tokens until we get to the end of the directive. Note that the
1869 // directive can be split over multiple lines using the backslash character.
1870 DiscardUntilEndOfDirective();
1871}
1872
James Dennettf6333ac2012-06-22 05:46:07 +00001873/// HandleImportDirective - Implements \#import.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001874///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001875void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1876 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001877 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
Alp Tokerbfa39342014-01-14 12:51:41 +00001878 if (LangOpts.MSVCCompat)
Aaron Ballman0467f552012-03-18 03:10:37 +00001879 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001880 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001881 }
Richard Smith25d50752014-10-20 00:15:49 +00001882 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001883}
1884
Chris Lattner58a1eb02009-04-08 18:46:40 +00001885/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1886/// pseudo directive in the predefines buffer. This handles it by sucking all
1887/// tokens through the preprocessor and discarding them (only keeping the side
1888/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001889void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1890 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001891 // This directive should only occur in the predefines buffer. If not, emit an
1892 // error and reject it.
1893 SourceLocation Loc = IncludeMacrosTok.getLocation();
1894 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1895 Diag(IncludeMacrosTok.getLocation(),
1896 diag::pp_include_macros_out_of_predefines);
1897 DiscardUntilEndOfDirective();
1898 return;
1899 }
Mike Stump11289f42009-09-09 15:08:12 +00001900
Chris Lattnere01d82b2009-04-08 20:53:24 +00001901 // Treat this as a normal #include for checking purposes. If this is
1902 // successful, it will push a new lexer onto the include stack.
Richard Smith25d50752014-10-20 00:15:49 +00001903 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
Mike Stump11289f42009-09-09 15:08:12 +00001904
Chris Lattnere01d82b2009-04-08 20:53:24 +00001905 Token TmpTok;
1906 do {
1907 Lex(TmpTok);
1908 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1909 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001910}
1911
Chris Lattnerf64b3522008-03-09 01:54:53 +00001912//===----------------------------------------------------------------------===//
1913// Preprocessor Macro Directive Handling.
1914//===----------------------------------------------------------------------===//
1915
1916/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1917/// definition has just been read. Lex the rest of the arguments and the
1918/// closing ), updating MI with what we learn. Return true if an error occurs
1919/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001920bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001921 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001922
Chris Lattnerf64b3522008-03-09 01:54:53 +00001923 while (1) {
1924 LexUnexpandedToken(Tok);
1925 switch (Tok.getKind()) {
1926 case tok::r_paren:
1927 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001928 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001929 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001930 // Otherwise we have #define FOO(A,)
1931 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1932 return true;
1933 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001934 if (!LangOpts.C99)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001935 Diag(Tok, LangOpts.CPlusPlus11 ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001936 diag::warn_cxx98_compat_variadic_macro :
1937 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001938
Joey Gouly1d58cdb2013-01-17 17:35:00 +00001939 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1940 if (LangOpts.OpenCL) {
1941 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1942 return true;
1943 }
1944
Chris Lattnerf64b3522008-03-09 01:54:53 +00001945 // Lex the token after the identifier.
1946 LexUnexpandedToken(Tok);
1947 if (Tok.isNot(tok::r_paren)) {
1948 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1949 return true;
1950 }
1951 // Add the __VA_ARGS__ identifier as an argument.
1952 Arguments.push_back(Ident__VA_ARGS__);
1953 MI->setIsC99Varargs();
Craig Topperd96b3f92015-10-22 04:59:52 +00001954 MI->setArgumentList(Arguments, BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001955 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001956 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001957 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1958 return true;
1959 default:
1960 // Handle keywords and identifiers here to accept things like
1961 // #define Foo(for) for.
1962 IdentifierInfo *II = Tok.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +00001963 if (!II) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001964 // #define X(1
1965 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1966 return true;
1967 }
1968
1969 // If this is already used as an argument, it is used multiple times (e.g.
1970 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001971 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001972 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001973 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001974 return true;
1975 }
Mike Stump11289f42009-09-09 15:08:12 +00001976
Chris Lattnerf64b3522008-03-09 01:54:53 +00001977 // Add the argument to the macro info.
1978 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001979
Chris Lattnerf64b3522008-03-09 01:54:53 +00001980 // Lex the token after the identifier.
1981 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001982
Chris Lattnerf64b3522008-03-09 01:54:53 +00001983 switch (Tok.getKind()) {
1984 default: // #define X(A B
1985 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1986 return true;
1987 case tok::r_paren: // #define X(A)
Craig Topperd96b3f92015-10-22 04:59:52 +00001988 MI->setArgumentList(Arguments, BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001989 return false;
1990 case tok::comma: // #define X(A,
1991 break;
1992 case tok::ellipsis: // #define X(A... -> GCC extension
1993 // Diagnose extension.
1994 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001995
Chris Lattnerf64b3522008-03-09 01:54:53 +00001996 // Lex the token after the identifier.
1997 LexUnexpandedToken(Tok);
1998 if (Tok.isNot(tok::r_paren)) {
1999 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2000 return true;
2001 }
Mike Stump11289f42009-09-09 15:08:12 +00002002
Chris Lattnerf64b3522008-03-09 01:54:53 +00002003 MI->setIsGNUVarargs();
Craig Topperd96b3f92015-10-22 04:59:52 +00002004 MI->setArgumentList(Arguments, BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002005 return false;
2006 }
2007 }
2008 }
2009}
2010
Serge Pavlov07c0f042014-12-18 11:14:21 +00002011static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2012 const LangOptions &LOptions) {
2013 if (MI->getNumTokens() == 1) {
2014 const Token &Value = MI->getReplacementToken(0);
2015
2016 // Macro that is identity, like '#define inline inline' is a valid pattern.
2017 if (MacroName.getKind() == Value.getKind())
2018 return true;
2019
2020 // Macro that maps a keyword to the same keyword decorated with leading/
2021 // trailing underscores is a valid pattern:
2022 // #define inline __inline
2023 // #define inline __inline__
2024 // #define inline _inline (in MS compatibility mode)
2025 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2026 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2027 if (!II->isKeyword(LOptions))
2028 return false;
2029 StringRef ValueText = II->getName();
2030 StringRef TrimmedValue = ValueText;
2031 if (!ValueText.startswith("__")) {
2032 if (ValueText.startswith("_"))
2033 TrimmedValue = TrimmedValue.drop_front(1);
2034 else
2035 return false;
2036 } else {
2037 TrimmedValue = TrimmedValue.drop_front(2);
2038 if (TrimmedValue.endswith("__"))
2039 TrimmedValue = TrimmedValue.drop_back(2);
2040 }
2041 return TrimmedValue.equals(MacroText);
2042 } else {
2043 return false;
2044 }
2045 }
2046
2047 // #define inline
Alexander Kornienkoa26c4952015-12-28 15:30:42 +00002048 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2049 tok::kw_const) &&
2050 MI->getNumTokens() == 0;
Serge Pavlov07c0f042014-12-18 11:14:21 +00002051}
2052
James Dennettf6333ac2012-06-22 05:46:07 +00002053/// HandleDefineDirective - Implements \#define. This consumes the entire macro
Chris Lattnerf64b3522008-03-09 01:54:53 +00002054/// line then lets the caller lex the next real token.
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002055void Preprocessor::HandleDefineDirective(Token &DefineTok,
2056 bool ImmediatelyAfterHeaderGuard) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002057 ++NumDefined;
2058
2059 Token MacroNameTok;
Serge Pavlov07c0f042014-12-18 11:14:21 +00002060 bool MacroShadowsKeyword;
2061 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
Mike Stump11289f42009-09-09 15:08:12 +00002062
Chris Lattnerf64b3522008-03-09 01:54:53 +00002063 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002064 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00002065 return;
2066
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002067 Token LastTok = MacroNameTok;
2068
Chris Lattnerf64b3522008-03-09 01:54:53 +00002069 // If we are supposed to keep comments in #defines, reenable comment saving
2070 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00002071 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00002072
Chris Lattnerf64b3522008-03-09 01:54:53 +00002073 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002074 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00002075
Chris Lattnerf64b3522008-03-09 01:54:53 +00002076 Token Tok;
2077 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002078
Chris Lattnerf64b3522008-03-09 01:54:53 +00002079 // If this is a function-like macro definition, parse the argument list,
2080 // marking each of the identifiers as being used as macro arguments. Also,
2081 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002082 if (Tok.is(tok::eod)) {
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002083 if (ImmediatelyAfterHeaderGuard) {
2084 // Save this macro information since it may part of a header guard.
2085 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2086 MacroNameTok.getLocation());
2087 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002088 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00002089 } else if (Tok.hasLeadingSpace()) {
2090 // This is a normal token with leading space. Clear the leading space
2091 // marker on the first token to get proper expansion.
2092 Tok.clearFlag(Token::LeadingSpace);
2093 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002094 // This is a function-like macro definition. Read the argument list.
2095 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00002096 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002097 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002098 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00002099 DiscardUntilEndOfDirective();
2100 return;
2101 }
2102
Chris Lattner249c38b2009-04-19 18:26:34 +00002103 // If this is a definition of a variadic C99 function-like macro, not using
2104 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00002105
Chris Lattner249c38b2009-04-19 18:26:34 +00002106 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2107 // This gets unpoisoned where it is allowed.
2108 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2109 if (MI->isC99Varargs())
2110 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00002111
Chris Lattnerf64b3522008-03-09 01:54:53 +00002112 // Read the first token after the arg list for down below.
2113 LexUnexpandedToken(Tok);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002114 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002115 // C99 requires whitespace between the macro definition and the body. Emit
2116 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00002117 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002118 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00002119 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2120 // first character of a replacement list is not a character required by
2121 // subclause 5.2.1, then there shall be white-space separation between the
2122 // identifier and the replacement list.". 5.2.1 lists this set:
2123 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2124 // is irrelevant here.
2125 bool isInvalid = false;
2126 if (Tok.is(tok::at)) // @ is not in the list above.
2127 isInvalid = true;
2128 else if (Tok.is(tok::unknown)) {
2129 // If we have an unknown token, it is something strange like "`". Since
2130 // all of valid characters would have lexed into a single character
2131 // token of some sort, we know this is not a valid case.
2132 isInvalid = true;
2133 }
2134 if (isInvalid)
2135 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2136 else
2137 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002138 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002139
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002140 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002141 LastTok = Tok;
2142
Chris Lattnerf64b3522008-03-09 01:54:53 +00002143 // Read the rest of the macro body.
2144 if (MI->isObjectLike()) {
2145 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002146 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002147 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002148 MI->AddTokenToBody(Tok);
2149 // Get the next token of the macro.
2150 LexUnexpandedToken(Tok);
2151 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002152 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00002153 // Otherwise, read the body of a function-like macro. While we are at it,
2154 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2155 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002156 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002157 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002158
Andy Gibbs6f8cfccb2016-04-01 19:02:20 +00002159 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00002160 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002161
Chris Lattnerf64b3522008-03-09 01:54:53 +00002162 // Get the next token of the macro.
2163 LexUnexpandedToken(Tok);
2164 continue;
2165 }
Mike Stump11289f42009-09-09 15:08:12 +00002166
Richard Smith701a3522013-07-09 01:00:29 +00002167 // If we're in -traditional mode, then we should ignore stringification
2168 // and token pasting. Mark the tokens as unknown so as not to confuse
2169 // things.
2170 if (getLangOpts().TraditionalCPP) {
2171 Tok.setKind(tok::unknown);
2172 MI->AddTokenToBody(Tok);
2173
2174 // Get the next token of the macro.
2175 LexUnexpandedToken(Tok);
2176 continue;
2177 }
2178
Eli Friedman14d3c792012-11-14 02:18:46 +00002179 if (Tok.is(tok::hashhash)) {
Eli Friedman14d3c792012-11-14 02:18:46 +00002180 // If we see token pasting, check if it looks like the gcc comma
2181 // pasting extension. We'll use this information to suppress
2182 // diagnostics later on.
2183
2184 // Get the next token of the macro.
2185 LexUnexpandedToken(Tok);
2186
2187 if (Tok.is(tok::eod)) {
2188 MI->AddTokenToBody(LastTok);
2189 break;
2190 }
2191
2192 unsigned NumTokens = MI->getNumTokens();
2193 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2194 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2195 MI->setHasCommaPasting();
2196
David Majnemer76faf1f2013-11-05 09:30:17 +00002197 // Things look ok, add the '##' token to the macro.
Eli Friedman14d3c792012-11-14 02:18:46 +00002198 MI->AddTokenToBody(LastTok);
Eli Friedman14d3c792012-11-14 02:18:46 +00002199 continue;
2200 }
2201
Chris Lattnerf64b3522008-03-09 01:54:53 +00002202 // Get the next token of the macro.
2203 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002204
Chris Lattner83bd8282009-05-25 17:16:10 +00002205 // Check for a valid macro arg identifier.
Craig Topperd2d442c2014-05-17 23:10:59 +00002206 if (Tok.getIdentifierInfo() == nullptr ||
Chris Lattner83bd8282009-05-25 17:16:10 +00002207 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2208
2209 // If this is assembler-with-cpp mode, we accept random gibberish after
2210 // the '#' because '#' is often a comment character. However, change
2211 // the kind of the token to tok::unknown so that the preprocessor isn't
2212 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002213 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00002214 LastTok.setKind(tok::unknown);
Eli Friedmancdf8b882013-06-18 21:33:38 +00002215 MI->AddTokenToBody(LastTok);
2216 continue;
Chris Lattner83bd8282009-05-25 17:16:10 +00002217 } else {
Andy Gibbs6f8cfccb2016-04-01 19:02:20 +00002218 Diag(Tok, diag::err_pp_stringize_not_parameter)
2219 << LastTok.is(tok::hashat);
Mike Stump11289f42009-09-09 15:08:12 +00002220
Chris Lattner83bd8282009-05-25 17:16:10 +00002221 // Disable __VA_ARGS__ again.
2222 Ident__VA_ARGS__->setIsPoisoned(true);
2223 return;
2224 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002225 }
Mike Stump11289f42009-09-09 15:08:12 +00002226
Chris Lattner83bd8282009-05-25 17:16:10 +00002227 // Things look ok, add the '#' and param name tokens to the macro.
2228 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002229 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00002230 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00002231
Chris Lattnerf64b3522008-03-09 01:54:53 +00002232 // Get the next token of the macro.
2233 LexUnexpandedToken(Tok);
2234 }
2235 }
Mike Stump11289f42009-09-09 15:08:12 +00002236
Serge Pavlov07c0f042014-12-18 11:14:21 +00002237 if (MacroShadowsKeyword &&
2238 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2239 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2240 }
Mike Stump11289f42009-09-09 15:08:12 +00002241
Chris Lattnerf64b3522008-03-09 01:54:53 +00002242 // Disable __VA_ARGS__ again.
2243 Ident__VA_ARGS__->setIsPoisoned(true);
2244
Chris Lattner57540c52011-04-15 05:22:18 +00002245 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00002246 // replacement list.
2247 unsigned NumTokens = MI->getNumTokens();
2248 if (NumTokens != 0) {
2249 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2250 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002251 return;
2252 }
2253 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2254 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002255 return;
2256 }
2257 }
Mike Stump11289f42009-09-09 15:08:12 +00002258
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002259 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00002260
Chris Lattnerf64b3522008-03-09 01:54:53 +00002261 // Finally, if this identifier already had a macro defined for it, verify that
Alexander Kornienko8b3f6232012-08-29 00:20:03 +00002262 // the macro bodies are identical, and issue diagnostics if they are not.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002263 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
John McCall83760372015-12-10 23:31:01 +00002264 // In Objective-C, ignore attempts to directly redefine the builtin
2265 // definitions of the ownership qualifiers. It's still possible to
2266 // #undef them.
2267 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2268 return II->isStr("__strong") ||
2269 II->isStr("__weak") ||
2270 II->isStr("__unsafe_unretained") ||
2271 II->isStr("__autoreleasing");
2272 };
2273 if (getLangOpts().ObjC1 &&
2274 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2275 == getPredefinesFileID() &&
2276 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2277 // Warn if it changes the tokens.
2278 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2279 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2280 !MI->isIdenticalTo(*OtherMI, *this,
2281 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2282 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2283 }
2284 assert(!OtherMI->isWarnIfUnused());
2285 return;
2286 }
2287
Chris Lattner5244f342009-01-16 19:50:11 +00002288 // It is very common for system headers to have tons of macro redefinitions
2289 // and for warnings to be disabled in system headers. If this is the case,
2290 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00002291 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00002292 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002293 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00002294 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002295
Richard Smith7b242542013-03-06 00:46:00 +00002296 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2297 // C++ [cpp.predefined]p4, but allow it as an extension.
2298 if (OtherMI->isBuiltinMacro())
2299 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
Chris Lattnerc0a585d2010-08-17 15:55:45 +00002300 // Macros must be identical. This means all tokens and whitespace
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002301 // separation must be the same. C99 6.10.3p2.
Richard Smith7b242542013-03-06 00:46:00 +00002302 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002303 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
Chris Lattner5244f342009-01-16 19:50:11 +00002304 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2305 << MacroNameTok.getIdentifierInfo();
2306 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2307 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002308 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002309 if (OtherMI->isWarnIfUnused())
2310 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002311 }
Mike Stump11289f42009-09-09 15:08:12 +00002312
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002313 DefMacroDirective *MD =
2314 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00002315
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002316 assert(!MI->isUsed());
2317 // If we need warning for not using the macro, add its location in the
2318 // warn-because-unused-macro set. If it gets used it will be removed from set.
Eli Friedman5ba37d52013-08-22 00:27:10 +00002319 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002320 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002321 MI->setIsWarnIfUnused(true);
2322 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2323 }
2324
Chris Lattner928e9092009-04-12 01:39:54 +00002325 // If the callbacks want to know, tell them about the macro definition.
2326 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002327 Callbacks->MacroDefined(MacroNameTok, MD);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002328}
2329
James Dennettf6333ac2012-06-22 05:46:07 +00002330/// HandleUndefDirective - Implements \#undef.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002331///
2332void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2333 ++NumUndefined;
2334
2335 Token MacroNameTok;
Serge Pavlovd024f522014-10-24 17:31:32 +00002336 ReadMacroName(MacroNameTok, MU_Undef);
Mike Stump11289f42009-09-09 15:08:12 +00002337
Chris Lattnerf64b3522008-03-09 01:54:53 +00002338 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002339 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00002340 return;
Mike Stump11289f42009-09-09 15:08:12 +00002341
Chris Lattnerf64b3522008-03-09 01:54:53 +00002342 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002343 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00002344
Richard Smith20e883e2015-04-29 23:20:19 +00002345 // Okay, we have a valid identifier to undef.
2346 auto *II = MacroNameTok.getIdentifierInfo();
Richard Smith36bd40d2015-05-04 03:15:40 +00002347 auto MD = getMacroDefinition(II);
Mike Stump11289f42009-09-09 15:08:12 +00002348
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002349 // If the callbacks want to know, tell them about the macro #undef.
2350 // Note: no matter if the macro was defined or not.
Richard Smith36bd40d2015-05-04 03:15:40 +00002351 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002352 Callbacks->MacroUndefined(MacroNameTok, MD);
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002353
Chris Lattnerf64b3522008-03-09 01:54:53 +00002354 // If the macro is not defined, this is a noop undef, just return.
Richard Smith36bd40d2015-05-04 03:15:40 +00002355 const MacroInfo *MI = MD.getMacroInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +00002356 if (!MI)
2357 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002358
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00002359 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00002360 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00002361
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002362 if (MI->isWarnIfUnused())
2363 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2364
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002365 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2366 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
Chris Lattnerf64b3522008-03-09 01:54:53 +00002367}
2368
Chris Lattnerf64b3522008-03-09 01:54:53 +00002369//===----------------------------------------------------------------------===//
2370// Preprocessor Conditional Directive Handling.
2371//===----------------------------------------------------------------------===//
2372
James Dennettf6333ac2012-06-22 05:46:07 +00002373/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2374/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2375/// true if any tokens have been returned or pp-directives activated before this
2376/// \#ifndef has been lexed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002377///
2378void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2379 bool ReadAnyTokensBeforeDirective) {
2380 ++NumIf;
2381 Token DirectiveTok = Result;
2382
2383 Token MacroNameTok;
2384 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00002385
Chris Lattnerf64b3522008-03-09 01:54:53 +00002386 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002387 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002388 // Skip code until we get to #endif. This helps with recovery by not
2389 // emitting an error when the #endif is reached.
2390 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2391 /*Foundnonskip*/false, /*FoundElse*/false);
2392 return;
2393 }
Mike Stump11289f42009-09-09 15:08:12 +00002394
Chris Lattnerf64b3522008-03-09 01:54:53 +00002395 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002396 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00002397
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002398 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Richard Smith36bd40d2015-05-04 03:15:40 +00002399 auto MD = getMacroDefinition(MII);
2400 MacroInfo *MI = MD.getMacroInfo();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002401
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002402 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002403 // If the start of a top-level #ifdef and if the macro is not defined,
2404 // inform MIOpt that this might be the start of a proper include guard.
2405 // Otherwise it is some other form of unknown conditional which we can't
2406 // handle.
Craig Topperd2d442c2014-05-17 23:10:59 +00002407 if (!ReadAnyTokensBeforeDirective && !MI) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002408 assert(isIfndef && "#ifdef shouldn't reach here");
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002409 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002410 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002411 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002412 }
2413
Chris Lattnerf64b3522008-03-09 01:54:53 +00002414 // If there is a macro, process it.
2415 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002416 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002417
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002418 if (Callbacks) {
2419 if (isIfndef)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002420 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002421 else
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002422 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002423 }
2424
Chris Lattnerf64b3522008-03-09 01:54:53 +00002425 // Should we include the stuff contained by this directive?
2426 if (!MI == isIfndef) {
2427 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00002428 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2429 /*wasskip*/false, /*foundnonskip*/true,
2430 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002431 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002432 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002433 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00002434 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002435 /*FoundElse*/false);
2436 }
2437}
2438
James Dennettf6333ac2012-06-22 05:46:07 +00002439/// HandleIfDirective - Implements the \#if directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002440///
2441void Preprocessor::HandleIfDirective(Token &IfToken,
2442 bool ReadAnyTokensBeforeDirective) {
2443 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00002444
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002445 // Parse and evaluate the conditional expression.
Craig Topperd2d442c2014-05-17 23:10:59 +00002446 IdentifierInfo *IfNDefMacro = nullptr;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002447 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2448 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2449 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00002450
2451 // If this condition is equivalent to #ifndef X, and if this is the first
2452 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002453 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002454 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Richard Smith089ee152013-06-16 05:05:39 +00002455 // FIXME: Pass in the location of the macro name, not the 'if' token.
2456 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
Nuno Lopes363212b2008-06-01 18:31:24 +00002457 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002458 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00002459 }
2460
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002461 if (Callbacks)
2462 Callbacks->If(IfToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002463 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002464 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002465
Chris Lattnerf64b3522008-03-09 01:54:53 +00002466 // Should we include the stuff contained by this directive?
2467 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002468 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002469 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002470 /*foundnonskip*/true, /*foundelse*/false);
2471 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002472 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00002473 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002474 /*FoundElse*/false);
2475 }
2476}
2477
James Dennettf6333ac2012-06-22 05:46:07 +00002478/// HandleEndifDirective - Implements the \#endif directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002479///
2480void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2481 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00002482
Chris Lattnerf64b3522008-03-09 01:54:53 +00002483 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002484 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00002485
Chris Lattnerf64b3522008-03-09 01:54:53 +00002486 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002487 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002488 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00002489 Diag(EndifToken, diag::err_pp_endif_without_if);
2490 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002491 }
Mike Stump11289f42009-09-09 15:08:12 +00002492
Chris Lattnerf64b3522008-03-09 01:54:53 +00002493 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002494 if (CurPPLexer->getConditionalStackDepth() == 0)
2495 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002496
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002497 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002498 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002499
2500 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002501 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002502}
2503
James Dennettf6333ac2012-06-22 05:46:07 +00002504/// HandleElseDirective - Implements the \#else directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002505///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002506void Preprocessor::HandleElseDirective(Token &Result) {
2507 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002508
Chris Lattnerf64b3522008-03-09 01:54:53 +00002509 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002510 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002511
Chris Lattnerf64b3522008-03-09 01:54:53 +00002512 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002513 if (CurPPLexer->popConditionalLevel(CI)) {
2514 Diag(Result, diag::pp_err_else_without_if);
2515 return;
2516 }
Mike Stump11289f42009-09-09 15:08:12 +00002517
Chris Lattnerf64b3522008-03-09 01:54:53 +00002518 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002519 if (CurPPLexer->getConditionalStackDepth() == 0)
2520 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002521
2522 // If this is a #else with a #else before it, report the error.
2523 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002524
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002525 if (Callbacks)
2526 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2527
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002528 // Finally, skip the rest of the contents of this block.
2529 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002530 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002531}
2532
James Dennettf6333ac2012-06-22 05:46:07 +00002533/// HandleElifDirective - Implements the \#elif directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002534///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002535void Preprocessor::HandleElifDirective(Token &ElifToken) {
2536 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002537
Chris Lattnerf64b3522008-03-09 01:54:53 +00002538 // #elif directive in a non-skipping conditional... start skipping.
2539 // We don't care what the condition is, because we will always skip it (since
2540 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002541 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002542 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002543 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002544
2545 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002546 if (CurPPLexer->popConditionalLevel(CI)) {
2547 Diag(ElifToken, diag::pp_err_elif_without_if);
2548 return;
2549 }
Mike Stump11289f42009-09-09 15:08:12 +00002550
Chris Lattnerf64b3522008-03-09 01:54:53 +00002551 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002552 if (CurPPLexer->getConditionalStackDepth() == 0)
2553 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002554
Chris Lattnerf64b3522008-03-09 01:54:53 +00002555 // If this is a #elif with a #else before it, report the error.
2556 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002557
2558 if (Callbacks)
2559 Callbacks->Elif(ElifToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002560 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002561 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002562
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002563 // Finally, skip the rest of the contents of this block.
2564 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002565 /*FoundElse*/CI.FoundElse,
2566 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002567}