blob: 4250619d084d82337a15632c28cc1655fe68e73f [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"
Chris Lattnerf64b3522008-03-09 01:54:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Utility Methods for Preprocessor Directive Handling.
34//===----------------------------------------------------------------------===//
35
Chris Lattnerc0a585d2010-08-17 15:55:45 +000036MacroInfo *Preprocessor::AllocateMacroInfo() {
Richard Smithee0c4c12014-07-24 01:13:23 +000037 MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
Ted Kremenekc8456f82010-10-19 22:15:20 +000038 MIChain->Next = MIChainHead;
Ted Kremenekc8456f82010-10-19 22:15:20 +000039 MIChainHead = MIChain;
Richard Smithee0c4c12014-07-24 01:13:23 +000040 return &MIChain->MI;
Chris Lattnerc0a585d2010-08-17 15:55:45 +000041}
42
43MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
44 MacroInfo *MI = AllocateMacroInfo();
Ted Kremenek6c7ea112008-12-15 19:56:42 +000045 new (MI) MacroInfo(L);
46 return MI;
47}
48
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000049MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
50 unsigned SubModuleID) {
Chandler Carruth06dde922014-03-02 13:02:01 +000051 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
52 "alignment for MacroInfo is less than the ID");
Argyrios Kyrtzidisd48b91d2013-04-30 05:05:35 +000053 DeserializedMacroInfoChain *MIChain =
54 BP.Allocate<DeserializedMacroInfoChain>();
55 MIChain->Next = DeserialMIChainHead;
56 DeserialMIChainHead = MIChain;
57
58 MacroInfo *MI = &MIChain->MI;
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000059 new (MI) MacroInfo(L);
60 MI->FromASTFile = true;
61 MI->setOwningModuleID(SubModuleID);
62 return MI;
63}
64
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000065DefMacroDirective *
66Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
Richard Smithdaa69e02014-07-25 04:40:03 +000067 unsigned ImportedFromModuleID,
68 ArrayRef<unsigned> Overrides) {
69 unsigned NumExtra = (ImportedFromModuleID ? 1 : 0) + Overrides.size();
70 return new (BP.Allocate(sizeof(DefMacroDirective) +
71 sizeof(unsigned) * NumExtra,
72 llvm::alignOf<DefMacroDirective>()))
73 DefMacroDirective(MI, Loc, ImportedFromModuleID, Overrides);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000074}
75
76UndefMacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +000077Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc,
78 unsigned ImportedFromModuleID,
79 ArrayRef<unsigned> Overrides) {
80 unsigned NumExtra = (ImportedFromModuleID ? 1 : 0) + Overrides.size();
81 return new (BP.Allocate(sizeof(UndefMacroDirective) +
82 sizeof(unsigned) * NumExtra,
83 llvm::alignOf<UndefMacroDirective>()))
84 UndefMacroDirective(UndefLoc, ImportedFromModuleID, Overrides);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000085}
86
87VisibilityMacroDirective *
88Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
89 bool isPublic) {
Richard Smithdaa69e02014-07-25 04:40:03 +000090 return new (BP) VisibilityMacroDirective(Loc, isPublic);
Chris Lattnerc0a585d2010-08-17 15:55:45 +000091}
92
James Dennettf6333ac2012-06-22 05:46:07 +000093/// \brief Read and discard all tokens remaining on the current line until
94/// the tok::eod token is found.
Chris Lattnerf64b3522008-03-09 01:54:53 +000095void Preprocessor::DiscardUntilEndOfDirective() {
96 Token Tmp;
97 do {
98 LexUnexpandedToken(Tmp);
Peter Collingbournef29ce972011-02-22 13:49:06 +000099 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000100 } while (Tmp.isNot(tok::eod));
Chris Lattnerf64b3522008-03-09 01:54:53 +0000101}
102
Alp Tokerb05e0b52014-05-21 06:13:51 +0000103bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
104 // Missing macro name?
105 if (MacroNameTok.is(tok::eod))
106 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
107
108 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
109 if (!II) {
110 bool Invalid = false;
111 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
112 if (Invalid)
113 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Alp Tokerf33619c2014-05-31 03:38:08 +0000114 II = getIdentifierInfo(Spelling);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000115
Alp Tokerf33619c2014-05-31 03:38:08 +0000116 if (!II->isCPlusPlusOperatorKeyword())
117 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000118
Alp Tokere03e9e12014-05-31 16:32:22 +0000119 // C++ 2.5p2: Alternative tokens behave the same as its primary token
120 // except for their spellings.
121 Diag(MacroNameTok, getLangOpts().MicrosoftExt
122 ? diag::ext_pp_operator_used_as_macro_name
123 : diag::err_pp_operator_used_as_macro_name)
124 << II << MacroNameTok.getKind();
Alp Tokerb05e0b52014-05-21 06:13:51 +0000125
Alp Tokerc5d194fc2014-05-31 03:38:17 +0000126 // Allow #defining |and| and friends for Microsoft compatibility or
127 // recovery when legacy C headers are included in C++.
Alp Tokerf33619c2014-05-31 03:38:08 +0000128 MacroNameTok.setIdentifierInfo(II);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000129 }
130
131 if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
132 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
133 return Diag(MacroNameTok, diag::err_defined_macro_name);
134 }
135
136 if (isDefineUndef == 2 && II->hasMacroDefinition() &&
137 getMacroInfo(II)->isBuiltinMacro()) {
138 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
139 // and C++ [cpp.predefined]p4], but allow it as an extension.
140 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
141 }
142
143 // Okay, we got a good identifier.
144 return false;
145}
146
James Dennettf6333ac2012-06-22 05:46:07 +0000147/// \brief Lex and validate a macro name, which occurs after a
148/// \#define or \#undef.
149///
150/// This sets the token kind to eod and discards the rest
151/// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
152/// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
153/// else (e.g. \#ifdef).
Chris Lattnerf64b3522008-03-09 01:54:53 +0000154void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
155 // Read the token, don't allow macro expansion on it.
156 LexUnexpandedToken(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Douglas Gregor12785102010-08-24 20:21:13 +0000158 if (MacroNameTok.is(tok::code_completion)) {
159 if (CodeComplete)
160 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000161 setCodeCompletionReached();
Douglas Gregor12785102010-08-24 20:21:13 +0000162 LexUnexpandedToken(MacroNameTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000163 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000164
165 if (!CheckMacroName(MacroNameTok, isDefineUndef))
Chris Lattner907dfe92008-11-18 07:59:24 +0000166 return;
Alp Tokerb05e0b52014-05-21 06:13:51 +0000167
168 // Invalid macro name, read and discard the rest of the line and set the
169 // token kind to tok::eod if necessary.
170 if (MacroNameTok.isNot(tok::eod)) {
171 MacroNameTok.setKind(tok::eod);
172 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +0000173 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000174}
175
James Dennettf6333ac2012-06-22 05:46:07 +0000176/// \brief Ensure that the next token is a tok::eod token.
177///
178/// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
Chris Lattner0003c272009-04-17 23:30:53 +0000179/// true, then we consider macros that expand to zero tokens as being ok.
180void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000181 Token Tmp;
Chris Lattner0003c272009-04-17 23:30:53 +0000182 // Lex unexpanded tokens for most directives: macros might expand to zero
183 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
184 // #line) allow empty macros.
185 if (EnableMacros)
186 Lex(Tmp);
187 else
188 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattnerf64b3522008-03-09 01:54:53 +0000190 // There should be no tokens after the directive, but we allow them as an
191 // extension.
192 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
193 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000194
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000195 if (Tmp.isNot(tok::eod)) {
Chris Lattner825676a2009-04-14 05:15:20 +0000196 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000197 // or if this is a macro-style preprocessing directive, because it is more
198 // trouble than it is worth to insert /**/ and check that there is no /**/
199 // in the range also.
Douglas Gregora771f462010-03-31 17:46:05 +0000200 FixItHint Hint;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000201 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000202 !CurTokenLexer)
Douglas Gregora771f462010-03-31 17:46:05 +0000203 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
204 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000205 DiscardUntilEndOfDirective();
206 }
207}
208
209
210
James Dennettf6333ac2012-06-22 05:46:07 +0000211/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
212/// decided that the subsequent tokens are in the \#if'd out portion of the
213/// file. Lex the rest of the file, until we see an \#endif. If
Chris Lattnerf64b3522008-03-09 01:54:53 +0000214/// FoundNonSkipPortion is true, then we have already emitted code for part of
James Dennettf6333ac2012-06-22 05:46:07 +0000215/// this \#if directive, so \#else/\#elif blocks should never be entered.
216/// If ElseOk is true, then \#else directives are ok, if not, then we have
217/// already seen one so a \#else directive is a duplicate. When this returns,
218/// the caller can lex the first valid token.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000219void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
220 bool FoundNonSkipPortion,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000221 bool FoundElse,
222 SourceLocation ElseLoc) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000223 ++NumSkipped;
David Blaikie7d170102013-05-15 07:37:26 +0000224 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000225
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000226 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000227 FoundNonSkipPortion, FoundElse);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenek56572ab2008-12-12 18:34:08 +0000229 if (CurPTHLexer) {
230 PTHSkipExcludedConditionalBlock();
231 return;
232 }
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattnerf64b3522008-03-09 01:54:53 +0000234 // Enter raw mode to disable identifier lookup (and thus macro expansion),
235 // disabling warnings, etc.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000236 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000237 Token Tok;
238 while (1) {
Chris Lattnerf406b242010-01-18 22:33:01 +0000239 CurLexer->Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000240
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000241 if (Tok.is(tok::code_completion)) {
242 if (CodeComplete)
243 CodeComplete->CodeCompleteInConditionalExclusion();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000244 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000245 continue;
246 }
247
Chris Lattnerf64b3522008-03-09 01:54:53 +0000248 // If this is the end of the buffer, we have an error.
249 if (Tok.is(tok::eof)) {
250 // Emit errors for each unterminated conditional on the stack, including
251 // the current one.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000252 while (!CurPPLexer->ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000253 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000254 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
255 diag::err_pp_unterminated_conditional);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000256 CurPPLexer->ConditionalStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000257 }
258
Chris Lattnerf64b3522008-03-09 01:54:53 +0000259 // Just return and let the caller lex after this #include.
260 break;
261 }
Mike Stump11289f42009-09-09 15:08:12 +0000262
Chris Lattnerf64b3522008-03-09 01:54:53 +0000263 // If this token is not a preprocessor directive, just skip it.
264 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
265 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattnerf64b3522008-03-09 01:54:53 +0000267 // We just parsed a # character at the start of a line, so we're in
268 // directive mode. Tell the lexer this so any newlines we see will be
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000269 // converted into an EOD token (this terminates the macro).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000270 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rose176057b2013-02-22 00:32:00 +0000271 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000272
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattnerf64b3522008-03-09 01:54:53 +0000274 // Read the next token, the directive flavor.
275 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattnerf64b3522008-03-09 01:54:53 +0000277 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
278 // something bogus), skip it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000279 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000280 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000281 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000282 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000283 continue;
284 }
285
286 // If the first letter isn't i or e, it isn't intesting to us. We know that
287 // this is safe in the face of spelling differences, because there is no way
288 // to spell an i/e in a strange way that is another letter. Skipping this
289 // allows us to avoid looking up the identifier info for #define/#undef and
290 // other common directives.
Alp Toker2d57cea2014-05-17 04:53:25 +0000291 StringRef RI = Tok.getRawIdentifier();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000292
Alp Toker2d57cea2014-05-17 04:53:25 +0000293 char FirstChar = RI[0];
Mike Stump11289f42009-09-09 15:08:12 +0000294 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000295 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000296 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000297 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000298 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000299 continue;
300 }
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattnerf64b3522008-03-09 01:54:53 +0000302 // Get the identifier name without trigraphs or embedded newlines. Note
303 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
304 // when skipping.
Benjamin Kramer144884642009-12-31 13:32:38 +0000305 char DirectiveBuf[20];
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000306 StringRef Directive;
Alp Toker2d57cea2014-05-17 04:53:25 +0000307 if (!Tok.needsCleaning() && RI.size() < 20) {
308 Directive = RI;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000309 } else {
310 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramer144884642009-12-31 13:32:38 +0000311 unsigned IdLen = DirectiveStr.size();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000312 if (IdLen >= 20) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000313 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000314 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000315 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000316 continue;
317 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000318 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000319 Directive = StringRef(DirectiveBuf, IdLen);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000320 }
Mike Stump11289f42009-09-09 15:08:12 +0000321
Benjamin Kramer144884642009-12-31 13:32:38 +0000322 if (Directive.startswith("if")) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000323 StringRef Sub = Directive.substr(2);
Benjamin Kramer144884642009-12-31 13:32:38 +0000324 if (Sub.empty() || // "if"
325 Sub == "def" || // "ifdef"
326 Sub == "ndef") { // "ifndef"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000327 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
328 // bother parsing the condition.
329 DiscardUntilEndOfDirective();
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000330 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000331 /*foundnonskip*/false,
Chandler Carruth540960f2011-01-03 17:40:17 +0000332 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000333 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000334 } else if (Directive[0] == 'e') {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000335 StringRef Sub = Directive.substr(1);
Benjamin Kramer144884642009-12-31 13:32:38 +0000336 if (Sub == "ndif") { // "endif"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000337 PPConditionalInfo CondInfo;
338 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000339 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000340 (void)InCond; // Silence warning in no-asserts mode.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000341 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump11289f42009-09-09 15:08:12 +0000342
Chris Lattnerf64b3522008-03-09 01:54:53 +0000343 // If we popped the outermost skipping block, we're done skipping!
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000344 if (!CondInfo.WasSkipping) {
Richard Smith87d8fb92012-06-24 23:56:26 +0000345 // Restore the value of LexingRawMode so that trailing comments
346 // are handled correctly, if we've reached the outermost block.
347 CurPPLexer->LexingRawMode = false;
Richard Smithd0124572012-06-21 00:35:03 +0000348 CheckEndOfDirective("endif");
Richard Smith87d8fb92012-06-24 23:56:26 +0000349 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000350 if (Callbacks)
351 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000352 break;
Richard Smithd0124572012-06-21 00:35:03 +0000353 } else {
354 DiscardUntilEndOfDirective();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000355 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000356 } else if (Sub == "lse") { // "else".
Chris Lattnerf64b3522008-03-09 01:54:53 +0000357 // #else directive in a skipping conditional. If not in some other
358 // skipping conditional, and if #else hasn't already been seen, enter it
359 // as a non-skipping conditional.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000360 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattnerf64b3522008-03-09 01:54:53 +0000362 // If this is a #else with a #else before it, report the error.
363 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattnerf64b3522008-03-09 01:54:53 +0000365 // Note that we've seen a #else in this conditional.
366 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnerf64b3522008-03-09 01:54:53 +0000368 // If the conditional is at the top level, and the #if block wasn't
369 // entered, enter the #else block now.
370 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
371 CondInfo.FoundNonSkip = true;
Richard Smith87d8fb92012-06-24 23:56:26 +0000372 // Restore the value of LexingRawMode so that trailing comments
373 // are handled correctly.
374 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000375 CheckEndOfDirective("else");
Richard Smith87d8fb92012-06-24 23:56:26 +0000376 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000377 if (Callbacks)
378 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000379 break;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000380 } else {
381 DiscardUntilEndOfDirective(); // C99 6.10p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000382 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000383 } else if (Sub == "lif") { // "elif".
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000384 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000385
John Thompson17c35732013-12-04 20:19:30 +0000386 // If this is a #elif with a #else before it, report the error.
387 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
388
Chris Lattnerf64b3522008-03-09 01:54:53 +0000389 // If this is in a skipping block or if we're already handled this #if
390 // block, don't bother parsing the condition.
391 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
392 DiscardUntilEndOfDirective();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000393 } else {
John Thompson17c35732013-12-04 20:19:30 +0000394 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000395 // Restore the value of LexingRawMode so that identifiers are
396 // looked up, etc, inside the #elif expression.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000397 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
398 CurPPLexer->LexingRawMode = false;
Craig Topperd2d442c2014-05-17 23:10:59 +0000399 IdentifierInfo *IfNDefMacro = nullptr;
John Thompson17c35732013-12-04 20:19:30 +0000400 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000401 CurPPLexer->LexingRawMode = true;
John Thompson17c35732013-12-04 20:19:30 +0000402 if (Callbacks) {
403 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000404 Callbacks->Elif(Tok.getLocation(),
John Thompson17c35732013-12-04 20:19:30 +0000405 SourceRange(CondBegin, CondEnd),
John Thompson87f9fef2013-12-07 08:41:15 +0000406 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
John Thompson17c35732013-12-04 20:19:30 +0000407 }
408 // If this condition is true, enter it!
409 if (CondValue) {
410 CondInfo.FoundNonSkip = true;
411 break;
412 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000413 }
414 }
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000417 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000418 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000419 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000420 }
421
422 // Finally, if we are out of the conditional (saw an #endif or ran off the end
423 // of the file, just stop skipping and return to lexing whatever came after
424 // the #if block.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000425 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000426
427 if (Callbacks) {
428 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
429 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
430 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000431}
432
Ted Kremenek56572ab2008-12-12 18:34:08 +0000433void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump11289f42009-09-09 15:08:12 +0000434
435 while (1) {
Ted Kremenek56572ab2008-12-12 18:34:08 +0000436 assert(CurPTHLexer);
437 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenek56572ab2008-12-12 18:34:08 +0000439 // Skip to the next '#else', '#elif', or #endif.
440 if (CurPTHLexer->SkipBlock()) {
441 // We have reached an #endif. Both the '#' and 'endif' tokens
442 // have been consumed by the PTHLexer. Just pop off the condition level.
443 PPConditionalInfo CondInfo;
444 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000445 (void)InCond; // Silence warning in no-asserts mode.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000446 assert(!InCond && "Can't be skipping if not in a conditional!");
447 break;
448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenek56572ab2008-12-12 18:34:08 +0000450 // We have reached a '#else' or '#elif'. Lex the next token to get
451 // the directive flavor.
452 Token Tok;
453 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000454
Ted Kremenek56572ab2008-12-12 18:34:08 +0000455 // We can actually look up the IdentifierInfo here since we aren't in
456 // raw mode.
457 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
458
459 if (K == tok::pp_else) {
460 // #else: Enter the else condition. We aren't in a nested condition
461 // since we skip those. We're always in the one matching the last
462 // blocked we skipped.
463 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
464 // Note that we've seen a #else in this conditional.
465 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenek56572ab2008-12-12 18:34:08 +0000467 // If the #if block wasn't entered then enter the #else block now.
468 if (!CondInfo.FoundNonSkip) {
469 CondInfo.FoundNonSkip = true;
Mike Stump11289f42009-09-09 15:08:12 +0000470
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000471 // Scan until the eod token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000472 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar2cba6be2009-04-13 17:57:49 +0000473 DiscardUntilEndOfDirective();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000474 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000475
Ted Kremenek56572ab2008-12-12 18:34:08 +0000476 break;
477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ted Kremenek56572ab2008-12-12 18:34:08 +0000479 // Otherwise skip this block.
480 continue;
481 }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Ted Kremenek56572ab2008-12-12 18:34:08 +0000483 assert(K == tok::pp_elif);
484 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
485
486 // If this is a #elif with a #else before it, report the error.
487 if (CondInfo.FoundElse)
488 Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000489
Ted Kremenek56572ab2008-12-12 18:34:08 +0000490 // If this is in a skipping block or if we're already handled this #if
Mike Stump11289f42009-09-09 15:08:12 +0000491 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000492 if (CondInfo.FoundNonSkip)
493 continue;
494
495 // Evaluate the condition of the #elif.
Craig Topperd2d442c2014-05-17 23:10:59 +0000496 IdentifierInfo *IfNDefMacro = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000497 CurPTHLexer->ParsingPreprocessorDirective = true;
498 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
499 CurPTHLexer->ParsingPreprocessorDirective = false;
500
501 // If this condition is true, enter it!
502 if (ShouldEnter) {
503 CondInfo.FoundNonSkip = true;
504 break;
505 }
506
507 // Otherwise, skip this block and go to the next one.
508 continue;
509 }
510}
511
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000512Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) {
513 ModuleMap &ModMap = HeaderInfo.getModuleMap();
514 if (SourceMgr.isInMainFile(FilenameLoc)) {
515 if (Module *CurMod = getCurrentModule())
516 return CurMod; // Compiling a module.
517 return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
518 }
519 // Try to determine the module of the include directive.
Daniel Jasper88d86952013-12-03 20:30:36 +0000520 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
Manuel Klimek98a9a6c2014-03-19 10:22:36 +0000521 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(FilenameLoc));
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000522 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
523 // The include comes from a file.
524 return ModMap.findModuleForHeader(EntryOfIncl).getModule();
525 } else {
526 // The include does not come from a file,
527 // so it is probably a module compilation.
528 return getCurrentModule();
529 }
530}
531
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000532const FileEntry *Preprocessor::LookupFile(
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000533 SourceLocation FilenameLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000534 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000535 bool isAngled,
536 const DirectoryLookup *FromDir,
537 const DirectoryLookup *&CurDir,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000538 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000539 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000540 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000541 bool SkipCache) {
Will Wilson0fafd342013-12-27 19:46:16 +0000542 // If the header lookup mechanism may be relative to the current inclusion
543 // stack, record the parent #includes.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000544 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
545 Includers;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000546 if (!FromDir) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000547 FileID FID = getCurrentFileLexer()->getFileID();
Will Wilson0fafd342013-12-27 19:46:16 +0000548 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattner022923a2009-02-04 19:45:07 +0000550 // If there is no file entry associated with this file, it must be the
551 // predefines buffer. Any other file is not lexed with a normal lexer, so
Douglas Gregor618e64a2010-08-08 07:49:23 +0000552 // it won't be scanned for preprocessor directives. If we have the
553 // predefines buffer, resolve #include references (which come from the
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000554 // -include command line argument) from the current working directory
555 // instead of relative to the main file.
556 if (!FileEnt) {
Will Wilson0fafd342013-12-27 19:46:16 +0000557 FileEnt = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000558 if (FileEnt)
559 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
560 } else {
561 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
562 }
Will Wilson0fafd342013-12-27 19:46:16 +0000563
564 // MSVC searches the current include stack from top to bottom for
565 // headers included by quoted include directives.
566 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
Alp Tokerbfa39342014-01-14 12:51:41 +0000567 if (LangOpts.MSVCCompat && !isAngled) {
Will Wilson0fafd342013-12-27 19:46:16 +0000568 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
569 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
570 if (IsFileLexer(ISEntry))
571 if ((FileEnt = SourceMgr.getFileEntryForID(
572 ISEntry.ThePPLexer->getFileID())))
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000573 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
Will Wilson0fafd342013-12-27 19:46:16 +0000574 }
Chris Lattner022923a2009-02-04 19:45:07 +0000575 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000578 // Do a standard file entry lookup.
579 CurDir = CurDirLookup;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000580 const FileEntry *FE = HeaderInfo.LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000581 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
582 RelativePath, SuggestedModule, SkipCache);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000583 if (FE) {
Daniel Jasper5c77e392014-03-14 14:53:17 +0000584 if (SuggestedModule && !LangOpts.AsmPreprocessor)
Daniel Jasper92669ee2013-12-20 12:09:36 +0000585 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
586 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000587 return FE;
588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Will Wilson0fafd342013-12-27 19:46:16 +0000590 const FileEntry *CurFileEnt;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000591 // Otherwise, see if this is a subframework header. If so, this is relative
592 // to one of the headers on the #include stack. Walk the list of the current
593 // headers on the #include stack and pass them to HeaderInfo.
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000594 if (IsFileLexer()) {
Ben Langmuir71e1a642014-05-05 21:44:13 +0000595 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) {
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000596 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000597 SearchPath, RelativePath,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000598 SuggestedModule))) {
599 if (SuggestedModule && !LangOpts.AsmPreprocessor)
600 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
601 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000602 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000603 }
604 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000607 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
608 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000609 if (IsFileLexer(ISEntry)) {
Mike Stump11289f42009-09-09 15:08:12 +0000610 if ((CurFileEnt =
Ben Langmuir71e1a642014-05-05 21:44:13 +0000611 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000612 if ((FE = HeaderInfo.LookupSubframeworkHeader(
Douglas Gregorf5f94522013-02-08 00:10:48 +0000613 Filename, CurFileEnt, SearchPath, RelativePath,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000614 SuggestedModule))) {
615 if (SuggestedModule && !LangOpts.AsmPreprocessor)
616 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
617 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000618 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000619 }
620 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000621 }
622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000624 // Otherwise, we really couldn't find the file.
Craig Topperd2d442c2014-05-17 23:10:59 +0000625 return nullptr;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000626}
627
Chris Lattnerf64b3522008-03-09 01:54:53 +0000628
629//===----------------------------------------------------------------------===//
630// Preprocessor Directive Handling.
631//===----------------------------------------------------------------------===//
632
David Blaikied5321242012-06-06 18:52:13 +0000633class Preprocessor::ResetMacroExpansionHelper {
634public:
635 ResetMacroExpansionHelper(Preprocessor *pp)
636 : PP(pp), save(pp->DisableMacroExpansion) {
637 if (pp->MacroExpansionInDirectivesOverride)
638 pp->DisableMacroExpansion = false;
639 }
640 ~ResetMacroExpansionHelper() {
641 PP->DisableMacroExpansion = save;
642 }
643private:
644 Preprocessor *PP;
645 bool save;
646};
647
Chris Lattnerf64b3522008-03-09 01:54:53 +0000648/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump11289f42009-09-09 15:08:12 +0000649/// at the start of a line. This consumes the directive, modifies the
Chris Lattnerf64b3522008-03-09 01:54:53 +0000650/// lexer/preprocessor state, and advances the lexer(s) so that the next token
651/// read is the correct one.
652void Preprocessor::HandleDirective(Token &Result) {
653 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump11289f42009-09-09 15:08:12 +0000654
Chris Lattnerf64b3522008-03-09 01:54:53 +0000655 // We just parsed a # character at the start of a line, so we're in directive
656 // mode. Tell the lexer this so any newlines we see will be converted into an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000657 // EOD token (which terminates the directive).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000658 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000659 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000661 bool ImmediatelyAfterTopLevelIfndef =
662 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
663 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
664
Chris Lattnerf64b3522008-03-09 01:54:53 +0000665 ++NumDirectives;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000666
Chris Lattnerf64b3522008-03-09 01:54:53 +0000667 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump11289f42009-09-09 15:08:12 +0000668 // work, we have to remember if we had read any tokens *before* this
Chris Lattnerf64b3522008-03-09 01:54:53 +0000669 // pp-directive.
Chris Lattner8cf1f932009-12-14 04:54:40 +0000670 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattner2d17ab72009-03-18 21:00:25 +0000672 // Save the '#' token in case we need to return it later.
673 Token SavedHash = Result;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattnerf64b3522008-03-09 01:54:53 +0000675 // Read the next token, the directive flavor. This isn't expanded due to
676 // C99 6.10.3p8.
677 LexUnexpandedToken(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattnerf64b3522008-03-09 01:54:53 +0000679 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
680 // #define A(x) #x
681 // A(abc
682 // #warning blah
683 // def)
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000684 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
685 // not support this for #include-like directives, since that can result in
686 // terrible diagnostics, and does not work in GCC.
687 if (InMacroArgs) {
688 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
689 switch (II->getPPKeywordID()) {
690 case tok::pp_include:
691 case tok::pp_import:
692 case tok::pp_include_next:
693 case tok::pp___include_macros:
694 Diag(Result, diag::err_embedded_include) << II->getName();
695 DiscardUntilEndOfDirective();
696 return;
697 default:
698 break;
699 }
700 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000701 Diag(Result, diag::ext_embedded_directive);
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000702 }
Mike Stump11289f42009-09-09 15:08:12 +0000703
David Blaikied5321242012-06-06 18:52:13 +0000704 // Temporarily enable macro expansion if set so
705 // and reset to previous state when returning from this function.
706 ResetMacroExpansionHelper helper(this);
707
Chris Lattnerf64b3522008-03-09 01:54:53 +0000708 switch (Result.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000709 case tok::eod:
Chris Lattnerf64b3522008-03-09 01:54:53 +0000710 return; // null directive.
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000711 case tok::code_completion:
712 if (CodeComplete)
713 CodeComplete->CodeCompleteDirective(
714 CurPPLexer->getConditionalStackDepth() > 0);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000715 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000716 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000717 case tok::numeric_constant: // # 7 GNU line marker directive.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000718 if (getLangOpts().AsmPreprocessor)
Chris Lattner5eb8ae22009-03-18 20:41:10 +0000719 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner76e68962009-01-26 06:19:46 +0000720 return HandleDigitDirective(Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000721 default:
722 IdentifierInfo *II = Result.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +0000723 if (!II) break; // Not an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattnerf64b3522008-03-09 01:54:53 +0000725 // Ask what the preprocessor keyword ID is.
726 switch (II->getPPKeywordID()) {
727 default: break;
728 // C99 6.10.1 - Conditional Inclusion.
729 case tok::pp_if:
730 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
731 case tok::pp_ifdef:
732 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
733 case tok::pp_ifndef:
734 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
735 case tok::pp_elif:
736 return HandleElifDirective(Result);
737 case tok::pp_else:
738 return HandleElseDirective(Result);
739 case tok::pp_endif:
740 return HandleEndifDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000741
Chris Lattnerf64b3522008-03-09 01:54:53 +0000742 // C99 6.10.2 - Source File Inclusion.
743 case tok::pp_include:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000744 // Handle #include.
745 return HandleIncludeDirective(SavedHash.getLocation(), Result);
Chris Lattner14a7f392009-04-08 18:24:34 +0000746 case tok::pp___include_macros:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000747 // Handle -imacros.
748 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000749
Chris Lattnerf64b3522008-03-09 01:54:53 +0000750 // C99 6.10.3 - Macro Replacement.
751 case tok::pp_define:
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000752 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000753 case tok::pp_undef:
754 return HandleUndefDirective(Result);
755
756 // C99 6.10.4 - Line Control.
757 case tok::pp_line:
Chris Lattner100c65e2009-01-26 05:29:08 +0000758 return HandleLineDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattnerf64b3522008-03-09 01:54:53 +0000760 // C99 6.10.5 - Error Directive.
761 case tok::pp_error:
762 return HandleUserDiagnosticDirective(Result, false);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattnerf64b3522008-03-09 01:54:53 +0000764 // C99 6.10.6 - Pragma Directive.
765 case tok::pp_pragma:
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000766 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattnerf64b3522008-03-09 01:54:53 +0000768 // GNU Extensions.
769 case tok::pp_import:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000770 return HandleImportDirective(SavedHash.getLocation(), Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000771 case tok::pp_include_next:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000772 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattnerf64b3522008-03-09 01:54:53 +0000774 case tok::pp_warning:
775 Diag(Result, diag::ext_pp_warning_directive);
776 return HandleUserDiagnosticDirective(Result, true);
777 case tok::pp_ident:
778 return HandleIdentSCCSDirective(Result);
779 case tok::pp_sccs:
780 return HandleIdentSCCSDirective(Result);
781 case tok::pp_assert:
782 //isExtension = true; // FIXME: implement #assert
783 break;
784 case tok::pp_unassert:
785 //isExtension = true; // FIXME: implement #unassert
786 break;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +0000787
Douglas Gregor663b48f2012-01-03 19:48:16 +0000788 case tok::pp___public_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000789 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000790 return HandleMacroPublicDirective(Result);
791 break;
792
Douglas Gregor663b48f2012-01-03 19:48:16 +0000793 case tok::pp___private_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000794 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000795 return HandleMacroPrivateDirective(Result);
796 break;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000797 }
798 break;
799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattner2d17ab72009-03-18 21:00:25 +0000801 // If this is a .S file, treat unknown # directives as non-preprocessor
802 // directives. This is important because # may be a comment or introduce
803 // various pseudo-ops. Just return the # token and push back the following
804 // token to be lexed next time.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000805 if (getLangOpts().AsmPreprocessor) {
Daniel Dunbar48b4d1e2009-07-13 21:48:50 +0000806 Token *Toks = new Token[2];
Chris Lattner2d17ab72009-03-18 21:00:25 +0000807 // Return the # and the token after it.
Mike Stump11289f42009-09-09 15:08:12 +0000808 Toks[0] = SavedHash;
Chris Lattner2d17ab72009-03-18 21:00:25 +0000809 Toks[1] = Result;
Chris Lattner56f64c12011-01-06 05:01:51 +0000810
811 // If the second token is a hashhash token, then we need to translate it to
812 // unknown so the token lexer doesn't try to perform token pasting.
813 if (Result.is(tok::hashhash))
814 Toks[1].setKind(tok::unknown);
815
Chris Lattner2d17ab72009-03-18 21:00:25 +0000816 // Enter this token stream so that we re-lex the tokens. Make sure to
817 // enable macro expansion, in case the token after the # is an identifier
818 // that is expanded.
819 EnterTokenStream(Toks, 2, false, true);
820 return;
821 }
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattnerf64b3522008-03-09 01:54:53 +0000823 // If we reached here, the preprocessing token is not valid!
824 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump11289f42009-09-09 15:08:12 +0000825
Chris Lattnerf64b3522008-03-09 01:54:53 +0000826 // Read the rest of the PP line.
827 DiscardUntilEndOfDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattnerf64b3522008-03-09 01:54:53 +0000829 // Okay, we're done parsing the directive.
830}
831
Chris Lattner76e68962009-01-26 06:19:46 +0000832/// GetLineValue - Convert a numeric token into an unsigned value, emitting
833/// Diagnostic DiagID if it is invalid, and returning the value in Val.
834static bool GetLineValue(Token &DigitTok, unsigned &Val,
Michael Ilsemane910cc82013-04-10 01:04:18 +0000835 unsigned DiagID, Preprocessor &PP,
836 bool IsGNULineDirective=false) {
Chris Lattner76e68962009-01-26 06:19:46 +0000837 if (DigitTok.isNot(tok::numeric_constant)) {
838 PP.Diag(DigitTok, DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000839
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000840 if (DigitTok.isNot(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000841 PP.DiscardUntilEndOfDirective();
842 return true;
843 }
Mike Stump11289f42009-09-09 15:08:12 +0000844
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000845 SmallString<64> IntegerBuffer;
Chris Lattner76e68962009-01-26 06:19:46 +0000846 IntegerBuffer.resize(DigitTok.getLength());
847 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregordc970f02010-03-16 22:30:13 +0000848 bool Invalid = false;
849 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
850 if (Invalid)
851 return true;
852
Chris Lattnerd66f1722009-04-18 18:35:15 +0000853 // Verify that we have a simple digit-sequence, and compute the value. This
854 // is always a simple digit string computed in decimal, so we do this manually
855 // here.
856 Val = 0;
857 for (unsigned i = 0; i != ActualLength; ++i) {
Richard Smith7f2707a2013-09-26 18:13:20 +0000858 // C++1y [lex.fcon]p1:
859 // Optional separating single quotes in a digit-sequence are ignored
860 if (DigitTokBegin[i] == '\'')
861 continue;
862
Jordan Rosea7d03842013-02-08 22:30:41 +0000863 if (!isDigit(DigitTokBegin[i])) {
Chris Lattnerd66f1722009-04-18 18:35:15 +0000864 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
Michael Ilsemane910cc82013-04-10 01:04:18 +0000865 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000866 PP.DiscardUntilEndOfDirective();
867 return true;
868 }
Mike Stump11289f42009-09-09 15:08:12 +0000869
Chris Lattnerd66f1722009-04-18 18:35:15 +0000870 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
871 if (NextVal < Val) { // overflow.
872 PP.Diag(DigitTok, DiagID);
873 PP.DiscardUntilEndOfDirective();
874 return true;
875 }
876 Val = NextVal;
Chris Lattner76e68962009-01-26 06:19:46 +0000877 }
Mike Stump11289f42009-09-09 15:08:12 +0000878
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000879 if (DigitTokBegin[0] == '0' && Val)
Michael Ilsemane910cc82013-04-10 01:04:18 +0000880 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
881 << IsGNULineDirective;
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattner76e68962009-01-26 06:19:46 +0000883 return false;
884}
885
James Dennettf6333ac2012-06-22 05:46:07 +0000886/// \brief Handle a \#line directive: C99 6.10.4.
887///
888/// The two acceptable forms are:
889/// \verbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000890/// # line digit-sequence
891/// # line digit-sequence "s-char-sequence"
James Dennettf6333ac2012-06-22 05:46:07 +0000892/// \endverbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000893void Preprocessor::HandleLineDirective(Token &Tok) {
894 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
895 // expanded.
896 Token DigitTok;
897 Lex(DigitTok);
898
Chris Lattner100c65e2009-01-26 05:29:08 +0000899 // Validate the number and convert it to an unsigned.
Chris Lattner76e68962009-01-26 06:19:46 +0000900 unsigned LineNo;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000901 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner100c65e2009-01-26 05:29:08 +0000902 return;
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000903
904 if (LineNo == 0)
905 Diag(DigitTok, diag::ext_pp_line_zero);
Chris Lattner100c65e2009-01-26 05:29:08 +0000906
Chris Lattner76e68962009-01-26 06:19:46 +0000907 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
908 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Eli Friedman192e0342011-10-10 23:35:28 +0000909 unsigned LineLimit = 32768U;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000910 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Eli Friedman192e0342011-10-10 23:35:28 +0000911 LineLimit = 2147483648U;
Chris Lattner100c65e2009-01-26 05:29:08 +0000912 if (LineNo >= LineLimit)
913 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000914 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
Richard Smithacd4d3d2011-10-15 01:18:56 +0000915 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000917 int FilenameID = -1;
Chris Lattner100c65e2009-01-26 05:29:08 +0000918 Token StrTok;
919 Lex(StrTok);
920
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000921 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
922 // string followed by eod.
923 if (StrTok.is(tok::eod))
Chris Lattner100c65e2009-01-26 05:29:08 +0000924 ; // ok
925 else if (StrTok.isNot(tok::string_literal)) {
926 Diag(StrTok, diag::err_pp_line_invalid_filename);
Richard Smithd67aea22012-03-06 03:21:47 +0000927 return DiscardUntilEndOfDirective();
928 } else if (StrTok.hasUDSuffix()) {
929 Diag(StrTok, diag::err_invalid_string_udl);
930 return DiscardUntilEndOfDirective();
Chris Lattner100c65e2009-01-26 05:29:08 +0000931 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000932 // Parse and validate the string, converting it into a unique ID.
Craig Topper9d5583e2014-06-26 04:58:39 +0000933 StringLiteralParser Literal(StrTok, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000934 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000935 if (Literal.hadError)
936 return DiscardUntilEndOfDirective();
937 if (Literal.Pascal) {
938 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
939 return DiscardUntilEndOfDirective();
940 }
Jay Foad9a6b0982011-06-21 15:13:30 +0000941 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +0000942
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000943 // Verify that there is nothing after the string, other than EOD. Because
Chris Lattner0003c272009-04-17 23:30:53 +0000944 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
945 CheckEndOfDirective("line", true);
Chris Lattner100c65e2009-01-26 05:29:08 +0000946 }
Mike Stump11289f42009-09-09 15:08:12 +0000947
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000948 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner839150e2009-03-27 17:13:49 +0000950 if (Callbacks)
Chris Lattnerc745cec2010-04-14 04:28:50 +0000951 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
952 PPCallbacks::RenameFile,
Chris Lattner839150e2009-03-27 17:13:49 +0000953 SrcMgr::C_User);
Chris Lattner100c65e2009-01-26 05:29:08 +0000954}
955
Chris Lattner76e68962009-01-26 06:19:46 +0000956/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
957/// marker directive.
958static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
959 bool &IsSystemHeader, bool &IsExternCHeader,
960 Preprocessor &PP) {
961 unsigned FlagVal;
962 Token FlagTok;
963 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000964 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000965 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
966 return true;
967
968 if (FlagVal == 1) {
969 IsFileEntry = true;
Mike Stump11289f42009-09-09 15:08:12 +0000970
Chris Lattner76e68962009-01-26 06:19:46 +0000971 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000972 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000973 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
974 return true;
975 } else if (FlagVal == 2) {
976 IsFileExit = true;
Mike Stump11289f42009-09-09 15:08:12 +0000977
Chris Lattner1c967782009-02-04 06:25:26 +0000978 SourceManager &SM = PP.getSourceManager();
979 // If we are leaving the current presumed file, check to make sure the
980 // presumed include stack isn't empty!
981 FileID CurFileID =
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000982 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
Chris Lattner1c967782009-02-04 06:25:26 +0000983 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000984 if (PLoc.isInvalid())
985 return true;
986
Chris Lattner1c967782009-02-04 06:25:26 +0000987 // If there is no include loc (main file) or if the include loc is in a
988 // different physical file, then we aren't in a "1" line marker flag region.
989 SourceLocation IncLoc = PLoc.getIncludeLoc();
990 if (IncLoc.isInvalid() ||
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000991 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
Chris Lattner1c967782009-02-04 06:25:26 +0000992 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
993 PP.DiscardUntilEndOfDirective();
994 return true;
995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattner76e68962009-01-26 06:19:46 +0000997 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000998 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000999 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1000 return true;
1001 }
1002
1003 // We must have 3 if there are still flags.
1004 if (FlagVal != 3) {
1005 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001006 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001007 return true;
1008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner76e68962009-01-26 06:19:46 +00001010 IsSystemHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattner76e68962009-01-26 06:19:46 +00001012 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001013 if (FlagTok.is(tok::eod)) return false;
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001014 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner76e68962009-01-26 06:19:46 +00001015 return true;
1016
1017 // We must have 4 if there is yet another flag.
1018 if (FlagVal != 4) {
1019 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001020 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001021 return true;
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner76e68962009-01-26 06:19:46 +00001024 IsExternCHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001025
Chris Lattner76e68962009-01-26 06:19:46 +00001026 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001027 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001028
1029 // There are no more valid flags here.
1030 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001031 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001032 return true;
1033}
1034
1035/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1036/// one of the following forms:
1037///
1038/// # 42
Mike Stump11289f42009-09-09 15:08:12 +00001039/// # 42 "file" ('1' | '2')?
Chris Lattner76e68962009-01-26 06:19:46 +00001040/// # 42 "file" ('1' | '2')? '3' '4'?
1041///
1042void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1043 // Validate the number and convert it to an unsigned. GNU does not have a
1044 // line # limit other than it fit in 32-bits.
1045 unsigned LineNo;
1046 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
Michael Ilsemane910cc82013-04-10 01:04:18 +00001047 *this, true))
Chris Lattner76e68962009-01-26 06:19:46 +00001048 return;
Mike Stump11289f42009-09-09 15:08:12 +00001049
Chris Lattner76e68962009-01-26 06:19:46 +00001050 Token StrTok;
1051 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner76e68962009-01-26 06:19:46 +00001053 bool IsFileEntry = false, IsFileExit = false;
1054 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001055 int FilenameID = -1;
1056
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001057 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1058 // string followed by eod.
1059 if (StrTok.is(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +00001060 ; // ok
1061 else if (StrTok.isNot(tok::string_literal)) {
1062 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001063 return DiscardUntilEndOfDirective();
Richard Smithd67aea22012-03-06 03:21:47 +00001064 } else if (StrTok.hasUDSuffix()) {
1065 Diag(StrTok, diag::err_invalid_string_udl);
1066 return DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001067 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001068 // Parse and validate the string, converting it into a unique ID.
Craig Topper9d5583e2014-06-26 04:58:39 +00001069 StringLiteralParser Literal(StrTok, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001070 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001071 if (Literal.hadError)
1072 return DiscardUntilEndOfDirective();
1073 if (Literal.Pascal) {
1074 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1075 return DiscardUntilEndOfDirective();
1076 }
Jay Foad9a6b0982011-06-21 15:13:30 +00001077 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattner76e68962009-01-26 06:19:46 +00001079 // If a filename was present, read any flags that are present.
Mike Stump11289f42009-09-09 15:08:12 +00001080 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001081 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner76e68962009-01-26 06:19:46 +00001082 return;
Chris Lattner76e68962009-01-26 06:19:46 +00001083 }
Mike Stump11289f42009-09-09 15:08:12 +00001084
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001085 // Create a line note with this information.
1086 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump11289f42009-09-09 15:08:12 +00001087 IsFileEntry, IsFileExit,
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001088 IsSystemHeader, IsExternCHeader);
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattner839150e2009-03-27 17:13:49 +00001090 // If the preprocessor has callbacks installed, notify them of the #line
1091 // change. This is used so that the line marker comes out in -E mode for
1092 // example.
1093 if (Callbacks) {
1094 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1095 if (IsFileEntry)
1096 Reason = PPCallbacks::EnterFile;
1097 else if (IsFileExit)
1098 Reason = PPCallbacks::ExitFile;
1099 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1100 if (IsExternCHeader)
1101 FileKind = SrcMgr::C_ExternCSystem;
1102 else if (IsSystemHeader)
1103 FileKind = SrcMgr::C_System;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattnerc745cec2010-04-14 04:28:50 +00001105 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner839150e2009-03-27 17:13:49 +00001106 }
Chris Lattner76e68962009-01-26 06:19:46 +00001107}
1108
1109
Chris Lattner38d7fd22009-01-26 05:30:54 +00001110/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1111///
Mike Stump11289f42009-09-09 15:08:12 +00001112void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001113 bool isWarning) {
Chris Lattner38d7fd22009-01-26 05:30:54 +00001114 // PTH doesn't emit #warning or #error directives.
1115 if (CurPTHLexer)
Chris Lattner100c65e2009-01-26 05:29:08 +00001116 return CurPTHLexer->DiscardToEndOfLine();
1117
Chris Lattnerf64b3522008-03-09 01:54:53 +00001118 // Read the rest of the line raw. We do this because we don't want macros
1119 // to be expanded and we don't require that the tokens be valid preprocessing
1120 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1121 // collapse multiple consequtive white space between tokens, but this isn't
1122 // specified by the standard.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001123 SmallString<128> Message;
1124 CurLexer->ReadToEndOfLine(&Message);
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001125
1126 // Find the first non-whitespace character, so that we can make the
1127 // diagnostic more succinct.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001128 StringRef Msg = Message.str().ltrim(" ");
1129
Chris Lattner100c65e2009-01-26 05:29:08 +00001130 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001131 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001132 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001133 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001134}
1135
1136/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1137///
1138void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1139 // Yes, this directive is an extension.
1140 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001141
Chris Lattnerf64b3522008-03-09 01:54:53 +00001142 // Read the string argument.
1143 Token StrTok;
1144 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001145
Chris Lattnerf64b3522008-03-09 01:54:53 +00001146 // If the token kind isn't a string, it's a malformed directive.
1147 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001148 StrTok.isNot(tok::wide_string_literal)) {
1149 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001150 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001151 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001152 return;
1153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Richard Smithd67aea22012-03-06 03:21:47 +00001155 if (StrTok.hasUDSuffix()) {
1156 Diag(StrTok, diag::err_invalid_string_udl);
1157 return DiscardUntilEndOfDirective();
1158 }
1159
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001160 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001161 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001162
Douglas Gregordc970f02010-03-16 22:30:13 +00001163 if (Callbacks) {
1164 bool Invalid = false;
1165 std::string Str = getSpelling(StrTok, &Invalid);
1166 if (!Invalid)
1167 Callbacks->Ident(Tok.getLocation(), Str);
1168 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001169}
1170
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001171/// \brief Handle a #public directive.
1172void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001173 Token MacroNameTok;
1174 ReadMacroName(MacroNameTok, 2);
1175
1176 // Error reading macro name? If so, diagnostic already issued.
1177 if (MacroNameTok.is(tok::eod))
1178 return;
1179
Douglas Gregor663b48f2012-01-03 19:48:16 +00001180 // Check to see if this is the last token on the #__public_macro line.
1181 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001182
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001183 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001184 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001185 MacroDirective *MD = getMacroDirective(II);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001186
1187 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001188 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001189 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001190 return;
1191 }
1192
1193 // Note that this macro has now been exported.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001194 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1195 MacroNameTok.getLocation(), /*IsPublic=*/true));
Douglas Gregorebf00492011-10-17 15:32:29 +00001196}
1197
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001198/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001199void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1200 Token MacroNameTok;
1201 ReadMacroName(MacroNameTok, 2);
1202
1203 // Error reading macro name? If so, diagnostic already issued.
1204 if (MacroNameTok.is(tok::eod))
1205 return;
1206
Douglas Gregor663b48f2012-01-03 19:48:16 +00001207 // Check to see if this is the last token on the #__private_macro line.
1208 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001209
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001210 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregorebf00492011-10-17 15:32:29 +00001211 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001212 MacroDirective *MD = getMacroDirective(II);
Douglas Gregorebf00492011-10-17 15:32:29 +00001213
1214 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001215 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001216 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregorebf00492011-10-17 15:32:29 +00001217 return;
1218 }
1219
1220 // Note that this macro has now been marked private.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001221 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1222 MacroNameTok.getLocation(), /*IsPublic=*/false));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001223}
1224
Chris Lattnerf64b3522008-03-09 01:54:53 +00001225//===----------------------------------------------------------------------===//
1226// Preprocessor Include Directive Handling.
1227//===----------------------------------------------------------------------===//
1228
1229/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
James Dennettf6333ac2012-06-22 05:46:07 +00001230/// checked and spelled filename, e.g. as an operand of \#include. This returns
Chris Lattnerf64b3522008-03-09 01:54:53 +00001231/// true if the input filename was in <>'s or false if it were in ""'s. The
1232/// caller is expected to provide a buffer that is large enough to hold the
1233/// spelling of the filename, but is also expected to handle the case when
1234/// this method decides to use a different buffer.
1235bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001236 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001237 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001238 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001239
Chris Lattnerf64b3522008-03-09 01:54:53 +00001240 // Make sure the filename is <x> or "x".
1241 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001242 if (Buffer[0] == '<') {
1243 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001244 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001245 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001246 return true;
1247 }
1248 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001249 } else if (Buffer[0] == '"') {
1250 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001251 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001252 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001253 return true;
1254 }
1255 isAngled = false;
1256 } else {
1257 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001258 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001259 return true;
1260 }
Mike Stump11289f42009-09-09 15:08:12 +00001261
Chris Lattnerf64b3522008-03-09 01:54:53 +00001262 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001263 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001264 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001265 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001266 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
Chris Lattnerf64b3522008-03-09 01:54:53 +00001269 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001270 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001271 return isAngled;
1272}
1273
James Dennett4a4f72d2013-11-27 01:27:40 +00001274// \brief Handle cases where the \#include name is expanded from a macro
1275// as multiple tokens, which need to be glued together.
1276//
1277// This occurs for code like:
1278// \code
1279// \#define FOO <a/b.h>
1280// \#include FOO
1281// \endcode
1282// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1283//
1284// This code concatenates and consumes tokens up to the '>' token. It returns
1285// false if the > was found, otherwise it returns true if it finds and consumes
1286// the EOD marker.
1287bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001288 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001289 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001290
John Thompsonb5353522009-10-30 13:49:06 +00001291 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001292 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001293 End = CurTok.getLocation();
1294
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001295 // FIXME: Provide code completion for #includes.
1296 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001297 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001298 Lex(CurTok);
1299 continue;
1300 }
1301
Chris Lattnerf64b3522008-03-09 01:54:53 +00001302 // Append the spelling of this token to the buffer. If there was a space
1303 // before it, add it now.
1304 if (CurTok.hasLeadingSpace())
1305 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001306
Chris Lattnerf64b3522008-03-09 01:54:53 +00001307 // Get the spelling of the token, directly into FilenameBuffer if possible.
1308 unsigned PreAppendSize = FilenameBuffer.size();
1309 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattnerf64b3522008-03-09 01:54:53 +00001311 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001312 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001313
Chris Lattnerf64b3522008-03-09 01:54:53 +00001314 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1315 if (BufPtr != &FilenameBuffer[PreAppendSize])
1316 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattnerf64b3522008-03-09 01:54:53 +00001318 // Resize FilenameBuffer to the correct size.
1319 if (CurTok.getLength() != ActualLen)
1320 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001321
Chris Lattnerf64b3522008-03-09 01:54:53 +00001322 // If we found the '>' marker, return success.
1323 if (CurTok.is(tok::greater))
1324 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001325
John Thompsonb5353522009-10-30 13:49:06 +00001326 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001327 }
1328
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001329 // If we hit the eod marker, emit an error and return true so that the caller
1330 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001331 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001332 return true;
1333}
1334
Richard Smith34f30512013-11-23 04:06:09 +00001335/// \brief Push a token onto the token stream containing an annotation.
1336static void EnterAnnotationToken(Preprocessor &PP,
1337 SourceLocation Begin, SourceLocation End,
1338 tok::TokenKind Kind, void *AnnotationVal) {
1339 Token *Tok = new Token[1];
1340 Tok[0].startToken();
1341 Tok[0].setKind(Kind);
1342 Tok[0].setLocation(Begin);
1343 Tok[0].setAnnotationEndLoc(End);
1344 Tok[0].setAnnotationValue(AnnotationVal);
1345 PP.EnterTokenStream(Tok, 1, true, true);
1346}
1347
James Dennettf6333ac2012-06-22 05:46:07 +00001348/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1349/// the file to be included from the lexer, then include it! This is a common
1350/// routine with functionality shared between \#include, \#include_next and
1351/// \#import. LookupFrom is set when this is a \#include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001352/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001353void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1354 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001355 const DirectoryLookup *LookupFrom,
1356 bool isImport) {
1357
1358 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001359 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001360
Chris Lattnerf64b3522008-03-09 01:54:53 +00001361 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001362 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001363 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001364 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001365 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001366
Chris Lattnerf64b3522008-03-09 01:54:53 +00001367 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001368 case tok::eod:
1369 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001370 return;
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattnerf64b3522008-03-09 01:54:53 +00001372 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001373 case tok::string_literal:
1374 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001375 End = FilenameTok.getLocation();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001376 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001377 break;
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattnerf64b3522008-03-09 01:54:53 +00001379 case tok::less:
1380 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1381 // case, glue the tokens together into FilenameBuffer and interpret those.
1382 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001383 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001384 return; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001385 Filename = FilenameBuffer.str();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001386 CharEnd = End.getLocWithOffset(1);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001387 break;
1388 default:
1389 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1390 DiscardUntilEndOfDirective();
1391 return;
1392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001394 CharSourceRange FilenameRange
1395 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
Aaron Ballman611306e2012-03-02 22:51:54 +00001396 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001397 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001398 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001399 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1400 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001401 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001402 DiscardUntilEndOfDirective();
1403 return;
1404 }
Mike Stump11289f42009-09-09 15:08:12 +00001405
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001406 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001407 // we allow macros that expand to nothing after the filename, because this
1408 // falls into the category of "#include pp-tokens new-line" specified in
1409 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001410 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001411
1412 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001413 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1414 Diag(FilenameTok, diag::err_pp_include_too_deep);
1415 return;
1416 }
Mike Stump11289f42009-09-09 15:08:12 +00001417
John McCall32f5fe12011-09-30 05:12:12 +00001418 // Complain about attempts to #include files in an audit pragma.
1419 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1420 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1421 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1422
1423 // Immediately leave the pragma.
1424 PragmaARCCFCodeAuditedLoc = SourceLocation();
1425 }
1426
Aaron Ballman611306e2012-03-02 22:51:54 +00001427 if (HeaderInfo.HasIncludeAliasMap()) {
1428 // Map the filename with the brackets still attached. If the name doesn't
1429 // map to anything, fall back on the filename we've already gotten the
1430 // spelling for.
1431 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1432 if (!NewName.empty())
1433 Filename = NewName;
1434 }
1435
Chris Lattnerf64b3522008-03-09 01:54:53 +00001436 // Search include directories.
1437 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001438 SmallString<1024> SearchPath;
1439 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001440 // We get the raw path only if we have 'Callbacks' to which we later pass
1441 // the path.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001442 ModuleMap::KnownHeader SuggestedModule;
1443 SourceLocation FilenameLoc = FilenameTok.getLocation();
Saleem Abdulrasool729b7d32014-03-12 02:26:08 +00001444 SmallString<128> NormalizedPath;
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001445 if (LangOpts.MSVCCompat) {
1446 NormalizedPath = Filename.str();
Yaron Keren1801d1b2014-08-09 18:13:01 +00001447#ifndef LLVM_ON_WIN32
Rafael Espindolaf6002232014-08-08 21:31:04 +00001448 llvm::sys::path::native(NormalizedPath);
Yaron Keren1801d1b2014-08-09 18:13:01 +00001449#endif
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001450 }
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001451 const FileEntry *File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001452 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001453 isAngled, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1454 Callbacks ? &RelativePath : nullptr,
1455 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : nullptr);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001456
Douglas Gregor11729f02011-11-30 18:12:06 +00001457 if (Callbacks) {
1458 if (!File) {
1459 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001460 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001461 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1462 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1463 // Add the recovery path to the list of search paths.
Daniel Dunbarae4feb62013-01-25 01:50:28 +00001464 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Douglas Gregor11729f02011-11-30 18:12:06 +00001465 HeaderInfo.AddSearchPath(DL, isAngled);
1466
1467 // Try the lookup again, skipping the cache.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001468 File = LookupFile(FilenameLoc,
1469 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1470 : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001471 isAngled, LookupFrom, CurDir, nullptr, nullptr,
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001472 HeaderInfo.getHeaderSearchOpts().ModuleMaps
Daniel Jasper07e6c402013-08-05 20:26:17 +00001473 ? &SuggestedModule
Craig Topperd2d442c2014-05-17 23:10:59 +00001474 : nullptr,
Daniel Jasper07e6c402013-08-05 20:26:17 +00001475 /*SkipCache*/ true);
Douglas Gregor11729f02011-11-30 18:12:06 +00001476 }
1477 }
1478 }
1479
Daniel Jasper07e6c402013-08-05 20:26:17 +00001480 if (!SuggestedModule || !getLangOpts().Modules) {
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001481 // Notify the callback object that we've seen an inclusion directive.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001482 Callbacks->InclusionDirective(HashLoc, IncludeTok,
1483 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1484 : Filename,
1485 isAngled, FilenameRange, File, SearchPath,
Craig Topperd2d442c2014-05-17 23:10:59 +00001486 RelativePath, /*ImportedModule=*/nullptr);
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001487 }
Douglas Gregor11729f02011-11-30 18:12:06 +00001488 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001489
1490 if (!File) {
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001491 if (!SuppressIncludeNotFoundError) {
1492 // If the file could not be located and it was included via angle
1493 // brackets, we can attempt a lookup as though it were a quoted path to
1494 // provide the user with a possible fixit.
1495 if (isAngled) {
Daniel Jasper07e6c402013-08-05 20:26:17 +00001496 File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001497 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001498 false, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1499 Callbacks ? &RelativePath : nullptr,
1500 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule
1501 : nullptr);
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001502 if (File) {
1503 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1504 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1505 Filename <<
1506 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1507 }
1508 }
1509 // If the file is still not found, just go with the vanilla diagnostic
1510 if (!File)
1511 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1512 }
1513 if (!File)
1514 return;
Douglas Gregor11729f02011-11-30 18:12:06 +00001515 }
1516
Douglas Gregor97eec242011-09-15 22:00:41 +00001517 // If we are supposed to import a module rather than including the header,
1518 // do so now.
Ben Langmuirb537a3a2014-07-23 15:30:23 +00001519 if (SuggestedModule && getLangOpts().Modules &&
1520 SuggestedModule.getModule()->getTopLevelModuleName() !=
1521 getLangOpts().ImplementationOfModule) {
Douglas Gregor71944202011-11-30 00:36:36 +00001522 // Compute the module access path corresponding to this module.
1523 // FIXME: Should we have a second loadModule() overload to avoid this
1524 // extra lookup step?
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001525 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001526 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001527 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1528 FilenameTok.getLocation()));
1529 std::reverse(Path.begin(), Path.end());
1530
Douglas Gregor41e115a2011-11-30 18:02:36 +00001531 // Warn that we're replacing the include/import with a module import.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001532 SmallString<128> PathString;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001533 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1534 if (I)
1535 PathString += '.';
1536 PathString += Path[I].first->getName();
1537 }
1538 int IncludeKind = 0;
1539
1540 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1541 case tok::pp_include:
1542 IncludeKind = 0;
1543 break;
1544
1545 case tok::pp_import:
1546 IncludeKind = 1;
1547 break;
1548
Douglas Gregor4401fbe2011-11-30 18:03:26 +00001549 case tok::pp_include_next:
1550 IncludeKind = 2;
1551 break;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001552
1553 case tok::pp___include_macros:
1554 IncludeKind = 3;
1555 break;
1556
1557 default:
1558 llvm_unreachable("unknown include directive kind");
Douglas Gregor41e115a2011-11-30 18:02:36 +00001559 }
1560
Douglas Gregor2537a362011-12-08 17:01:29 +00001561 // Determine whether we are actually building the module that this
1562 // include directive maps to.
1563 bool BuildingImportedModule
David Blaikiebbafb8a2012-03-11 07:00:24 +00001564 = Path[0].first->getName() == getLangOpts().CurrentModule;
Richard Smith34f30512013-11-23 04:06:09 +00001565
David Blaikiebbafb8a2012-03-11 07:00:24 +00001566 if (!BuildingImportedModule && getLangOpts().ObjC2) {
Douglas Gregor2537a362011-12-08 17:01:29 +00001567 // If we're not building the imported module, warn that we're going
1568 // to automatically turn this inclusion directive into a module import.
Douglas Gregorda82e702012-01-03 19:32:59 +00001569 // We only do this in Objective-C, where we have a module-import syntax.
Douglas Gregor2537a362011-12-08 17:01:29 +00001570 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1571 /*IsTokenRange=*/false);
1572 Diag(HashLoc, diag::warn_auto_module_import)
1573 << IncludeKind << PathString
1574 << FixItHint::CreateReplacement(ReplaceRange,
Douglas Gregorc50d4922012-12-11 22:11:52 +00001575 "@import " + PathString.str().str() + ";");
Douglas Gregor2537a362011-12-08 17:01:29 +00001576 }
Douglas Gregor41e115a2011-11-30 18:02:36 +00001577
Richard Smithce587f52013-11-15 04:24:58 +00001578 // Load the module. Only make macros visible. We'll make the declarations
1579 // visible when the parser gets here.
1580 Module::NameVisibilityKind Visibility = Module::MacrosVisible;
Douglas Gregor7a626572012-11-29 23:55:25 +00001581 ModuleLoadResult Imported
Douglas Gregor98a52db2011-12-20 00:28:52 +00001582 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1583 /*IsIncludeDirective=*/true);
Craig Topperd2d442c2014-05-17 23:10:59 +00001584 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
Argyrios Kyrtzidis051b4432012-09-29 01:06:01 +00001585 "the imported module is different than the suggested one");
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00001586
1587 if (!Imported && hadModuleLoaderFatalFailure()) {
1588 // With a fatal failure in the module loader, we abort parsing.
1589 Token &Result = IncludeTok;
1590 if (CurLexer) {
1591 Result.startToken();
1592 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1593 CurLexer->cutOffLexing();
1594 } else {
1595 assert(CurPTHLexer && "#include but no current lexer set!");
1596 CurPTHLexer->getEOF(Result);
1597 }
1598 return;
1599 }
Richard Smithce587f52013-11-15 04:24:58 +00001600
Douglas Gregor2537a362011-12-08 17:01:29 +00001601 // If this header isn't part of the module we're building, we're done.
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001602 if (!BuildingImportedModule && Imported) {
1603 if (Callbacks) {
1604 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1605 FilenameRange, File,
1606 SearchPath, RelativePath, Imported);
1607 }
Richard Smithce587f52013-11-15 04:24:58 +00001608
1609 if (IncludeKind != 3) {
1610 // Let the parser know that we hit a module import, and it should
1611 // make the module visible.
1612 // FIXME: Produce this as the current token directly, rather than
1613 // allocating a new token for it.
Richard Smith34f30512013-11-23 04:06:09 +00001614 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include,
1615 Imported);
Richard Smithce587f52013-11-15 04:24:58 +00001616 }
Douglas Gregor2537a362011-12-08 17:01:29 +00001617 return;
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001618 }
Douglas Gregor7a626572012-11-29 23:55:25 +00001619
1620 // If we failed to find a submodule that we expected to find, we can
1621 // continue. Otherwise, there's an error in the included file, so we
1622 // don't want to include it.
1623 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1624 return;
1625 }
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001626 }
1627
1628 if (Callbacks && SuggestedModule) {
1629 // We didn't notify the callback object that we've seen an inclusion
1630 // directive before. Now that we are parsing the include normally and not
1631 // turning it to a module import, notify the callback object.
1632 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1633 FilenameRange, File,
1634 SearchPath, RelativePath,
Craig Topperd2d442c2014-05-17 23:10:59 +00001635 /*ImportedModule=*/nullptr);
Douglas Gregor97eec242011-09-15 22:00:41 +00001636 }
1637
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001638 // The #included file will be considered to be a system header if either it is
1639 // in a system include directory, or if the #includer is a system include
1640 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001641 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001642 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001643 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001644
Chris Lattner72286d62010-04-19 20:44:31 +00001645 // Ask HeaderInfo if we should enter this #include file. If not, #including
1646 // this file will have no effect.
1647 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001648 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001649 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner72286d62010-04-19 20:44:31 +00001650 return;
1651 }
1652
Chris Lattnerf64b3522008-03-09 01:54:53 +00001653 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001654 SourceLocation IncludePos = End;
1655 // If the filename string was the result of macro expansions, set the include
1656 // position on the file where it will be included and after the expansions.
1657 if (IncludePos.isMacroID())
1658 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1659 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Peter Collingbourned395b932011-06-30 16:41:03 +00001660 assert(!FID.isInvalid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001661
Richard Smith34f30512013-11-23 04:06:09 +00001662 // Determine if we're switching to building a new submodule, and which one.
1663 ModuleMap::KnownHeader BuildingModule;
1664 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) {
1665 Module *RequestingModule = getModuleForLocation(FilenameLoc);
1666 BuildingModule =
1667 HeaderInfo.getModuleMap().findModuleForHeader(File, RequestingModule);
1668 }
1669
1670 // If all is good, enter the new file!
Richard Smith67294e22014-01-31 20:47:44 +00001671 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1672 return;
Richard Smith34f30512013-11-23 04:06:09 +00001673
1674 // If we're walking into another part of the same module, let the parser
1675 // know that any future declarations are within that other submodule.
Richard Smith67294e22014-01-31 20:47:44 +00001676 if (BuildingModule) {
1677 assert(!CurSubmodule && "should not have marked this as a module yet");
1678 CurSubmodule = BuildingModule.getModule();
1679
Richard Smith34f30512013-11-23 04:06:09 +00001680 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin,
Richard Smith67294e22014-01-31 20:47:44 +00001681 CurSubmodule);
1682 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001683}
1684
James Dennettf6333ac2012-06-22 05:46:07 +00001685/// HandleIncludeNextDirective - Implements \#include_next.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001686///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001687void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1688 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001689 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001690
Chris Lattnerf64b3522008-03-09 01:54:53 +00001691 // #include_next is like #include, except that we start searching after
1692 // the current found directory. If we can't do this, issue a
1693 // diagnostic.
1694 const DirectoryLookup *Lookup = CurDirLookup;
1695 if (isInPrimaryFile()) {
Craig Topperd2d442c2014-05-17 23:10:59 +00001696 Lookup = nullptr;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001697 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Craig Topperd2d442c2014-05-17 23:10:59 +00001698 } else if (!Lookup) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001699 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1700 } else {
1701 // Start looking up in the next directory.
1702 ++Lookup;
1703 }
Mike Stump11289f42009-09-09 15:08:12 +00001704
Douglas Gregor796d76a2010-10-20 22:00:55 +00001705 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001706}
1707
James Dennettf6333ac2012-06-22 05:46:07 +00001708/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
Aaron Ballman0467f552012-03-18 03:10:37 +00001709void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1710 // The Microsoft #import directive takes a type library and generates header
1711 // files from it, and includes those. This is beyond the scope of what clang
1712 // does, so we ignore it and error out. However, #import can optionally have
1713 // trailing attributes that span multiple lines. We're going to eat those
1714 // so we can continue processing from there.
1715 Diag(Tok, diag::err_pp_import_directive_ms );
1716
1717 // Read tokens until we get to the end of the directive. Note that the
1718 // directive can be split over multiple lines using the backslash character.
1719 DiscardUntilEndOfDirective();
1720}
1721
James Dennettf6333ac2012-06-22 05:46:07 +00001722/// HandleImportDirective - Implements \#import.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001723///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001724void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1725 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001726 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
Alp Tokerbfa39342014-01-14 12:51:41 +00001727 if (LangOpts.MSVCCompat)
Aaron Ballman0467f552012-03-18 03:10:37 +00001728 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001729 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001730 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001731 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001732}
1733
Chris Lattner58a1eb02009-04-08 18:46:40 +00001734/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1735/// pseudo directive in the predefines buffer. This handles it by sucking all
1736/// tokens through the preprocessor and discarding them (only keeping the side
1737/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001738void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1739 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001740 // This directive should only occur in the predefines buffer. If not, emit an
1741 // error and reject it.
1742 SourceLocation Loc = IncludeMacrosTok.getLocation();
1743 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1744 Diag(IncludeMacrosTok.getLocation(),
1745 diag::pp_include_macros_out_of_predefines);
1746 DiscardUntilEndOfDirective();
1747 return;
1748 }
Mike Stump11289f42009-09-09 15:08:12 +00001749
Chris Lattnere01d82b2009-04-08 20:53:24 +00001750 // Treat this as a normal #include for checking purposes. If this is
1751 // successful, it will push a new lexer onto the include stack.
Craig Topperd2d442c2014-05-17 23:10:59 +00001752 HandleIncludeDirective(HashLoc, IncludeMacrosTok, nullptr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattnere01d82b2009-04-08 20:53:24 +00001754 Token TmpTok;
1755 do {
1756 Lex(TmpTok);
1757 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1758 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001759}
1760
Chris Lattnerf64b3522008-03-09 01:54:53 +00001761//===----------------------------------------------------------------------===//
1762// Preprocessor Macro Directive Handling.
1763//===----------------------------------------------------------------------===//
1764
1765/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1766/// definition has just been read. Lex the rest of the arguments and the
1767/// closing ), updating MI with what we learn. Return true if an error occurs
1768/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001769bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001770 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001771
Chris Lattnerf64b3522008-03-09 01:54:53 +00001772 while (1) {
1773 LexUnexpandedToken(Tok);
1774 switch (Tok.getKind()) {
1775 case tok::r_paren:
1776 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001777 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001778 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001779 // Otherwise we have #define FOO(A,)
1780 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1781 return true;
1782 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001783 if (!LangOpts.C99)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001784 Diag(Tok, LangOpts.CPlusPlus11 ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001785 diag::warn_cxx98_compat_variadic_macro :
1786 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001787
Joey Gouly1d58cdb2013-01-17 17:35:00 +00001788 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1789 if (LangOpts.OpenCL) {
1790 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1791 return true;
1792 }
1793
Chris Lattnerf64b3522008-03-09 01:54:53 +00001794 // Lex the token after the identifier.
1795 LexUnexpandedToken(Tok);
1796 if (Tok.isNot(tok::r_paren)) {
1797 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1798 return true;
1799 }
1800 // Add the __VA_ARGS__ identifier as an argument.
1801 Arguments.push_back(Ident__VA_ARGS__);
1802 MI->setIsC99Varargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001803 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001804 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001805 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001806 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1807 return true;
1808 default:
1809 // Handle keywords and identifiers here to accept things like
1810 // #define Foo(for) for.
1811 IdentifierInfo *II = Tok.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +00001812 if (!II) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001813 // #define X(1
1814 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1815 return true;
1816 }
1817
1818 // If this is already used as an argument, it is used multiple times (e.g.
1819 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001820 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001821 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001822 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001823 return true;
1824 }
Mike Stump11289f42009-09-09 15:08:12 +00001825
Chris Lattnerf64b3522008-03-09 01:54:53 +00001826 // Add the argument to the macro info.
1827 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001828
Chris Lattnerf64b3522008-03-09 01:54:53 +00001829 // Lex the token after the identifier.
1830 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001831
Chris Lattnerf64b3522008-03-09 01:54:53 +00001832 switch (Tok.getKind()) {
1833 default: // #define X(A B
1834 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1835 return true;
1836 case tok::r_paren: // #define X(A)
Chris Lattner70946da2009-02-20 22:46:43 +00001837 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001838 return false;
1839 case tok::comma: // #define X(A,
1840 break;
1841 case tok::ellipsis: // #define X(A... -> GCC extension
1842 // Diagnose extension.
1843 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001844
Chris Lattnerf64b3522008-03-09 01:54:53 +00001845 // Lex the token after the identifier.
1846 LexUnexpandedToken(Tok);
1847 if (Tok.isNot(tok::r_paren)) {
1848 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1849 return true;
1850 }
Mike Stump11289f42009-09-09 15:08:12 +00001851
Chris Lattnerf64b3522008-03-09 01:54:53 +00001852 MI->setIsGNUVarargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001853 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001854 return false;
1855 }
1856 }
1857 }
1858}
1859
James Dennettf6333ac2012-06-22 05:46:07 +00001860/// HandleDefineDirective - Implements \#define. This consumes the entire macro
Chris Lattnerf64b3522008-03-09 01:54:53 +00001861/// line then lets the caller lex the next real token.
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001862void Preprocessor::HandleDefineDirective(Token &DefineTok,
1863 bool ImmediatelyAfterHeaderGuard) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001864 ++NumDefined;
1865
1866 Token MacroNameTok;
1867 ReadMacroName(MacroNameTok, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001868
Chris Lattnerf64b3522008-03-09 01:54:53 +00001869 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001870 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001871 return;
1872
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001873 Token LastTok = MacroNameTok;
1874
Chris Lattnerf64b3522008-03-09 01:54:53 +00001875 // If we are supposed to keep comments in #defines, reenable comment saving
1876 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00001877 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00001878
Chris Lattnerf64b3522008-03-09 01:54:53 +00001879 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001880 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001881
Chris Lattnerf64b3522008-03-09 01:54:53 +00001882 Token Tok;
1883 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001884
Chris Lattnerf64b3522008-03-09 01:54:53 +00001885 // If this is a function-like macro definition, parse the argument list,
1886 // marking each of the identifiers as being used as macro arguments. Also,
1887 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001888 if (Tok.is(tok::eod)) {
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001889 if (ImmediatelyAfterHeaderGuard) {
1890 // Save this macro information since it may part of a header guard.
1891 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
1892 MacroNameTok.getLocation());
1893 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001894 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00001895 } else if (Tok.hasLeadingSpace()) {
1896 // This is a normal token with leading space. Clear the leading space
1897 // marker on the first token to get proper expansion.
1898 Tok.clearFlag(Token::LeadingSpace);
1899 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001900 // This is a function-like macro definition. Read the argument list.
1901 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001902 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001903 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001904 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00001905 DiscardUntilEndOfDirective();
1906 return;
1907 }
1908
Chris Lattner249c38b2009-04-19 18:26:34 +00001909 // If this is a definition of a variadic C99 function-like macro, not using
1910 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00001911
Chris Lattner249c38b2009-04-19 18:26:34 +00001912 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1913 // This gets unpoisoned where it is allowed.
1914 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1915 if (MI->isC99Varargs())
1916 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00001917
Chris Lattnerf64b3522008-03-09 01:54:53 +00001918 // Read the first token after the arg list for down below.
1919 LexUnexpandedToken(Tok);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001920 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001921 // C99 requires whitespace between the macro definition and the body. Emit
1922 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00001923 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001924 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00001925 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1926 // first character of a replacement list is not a character required by
1927 // subclause 5.2.1, then there shall be white-space separation between the
1928 // identifier and the replacement list.". 5.2.1 lists this set:
1929 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1930 // is irrelevant here.
1931 bool isInvalid = false;
1932 if (Tok.is(tok::at)) // @ is not in the list above.
1933 isInvalid = true;
1934 else if (Tok.is(tok::unknown)) {
1935 // If we have an unknown token, it is something strange like "`". Since
1936 // all of valid characters would have lexed into a single character
1937 // token of some sort, we know this is not a valid case.
1938 isInvalid = true;
1939 }
1940 if (isInvalid)
1941 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1942 else
1943 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001944 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001945
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001946 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001947 LastTok = Tok;
1948
Chris Lattnerf64b3522008-03-09 01:54:53 +00001949 // Read the rest of the macro body.
1950 if (MI->isObjectLike()) {
1951 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001952 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001953 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001954 MI->AddTokenToBody(Tok);
1955 // Get the next token of the macro.
1956 LexUnexpandedToken(Tok);
1957 }
Mike Stump11289f42009-09-09 15:08:12 +00001958
Chris Lattnerf64b3522008-03-09 01:54:53 +00001959 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00001960 // Otherwise, read the body of a function-like macro. While we are at it,
1961 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1962 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001963 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001964 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001965
Eli Friedman14d3c792012-11-14 02:18:46 +00001966 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001967 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001968
Chris Lattnerf64b3522008-03-09 01:54:53 +00001969 // Get the next token of the macro.
1970 LexUnexpandedToken(Tok);
1971 continue;
1972 }
Mike Stump11289f42009-09-09 15:08:12 +00001973
Richard Smith701a3522013-07-09 01:00:29 +00001974 // If we're in -traditional mode, then we should ignore stringification
1975 // and token pasting. Mark the tokens as unknown so as not to confuse
1976 // things.
1977 if (getLangOpts().TraditionalCPP) {
1978 Tok.setKind(tok::unknown);
1979 MI->AddTokenToBody(Tok);
1980
1981 // Get the next token of the macro.
1982 LexUnexpandedToken(Tok);
1983 continue;
1984 }
1985
Eli Friedman14d3c792012-11-14 02:18:46 +00001986 if (Tok.is(tok::hashhash)) {
1987
1988 // If we see token pasting, check if it looks like the gcc comma
1989 // pasting extension. We'll use this information to suppress
1990 // diagnostics later on.
1991
1992 // Get the next token of the macro.
1993 LexUnexpandedToken(Tok);
1994
1995 if (Tok.is(tok::eod)) {
1996 MI->AddTokenToBody(LastTok);
1997 break;
1998 }
1999
2000 unsigned NumTokens = MI->getNumTokens();
2001 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2002 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2003 MI->setHasCommaPasting();
2004
David Majnemer76faf1f2013-11-05 09:30:17 +00002005 // Things look ok, add the '##' token to the macro.
Eli Friedman14d3c792012-11-14 02:18:46 +00002006 MI->AddTokenToBody(LastTok);
Eli Friedman14d3c792012-11-14 02:18:46 +00002007 continue;
2008 }
2009
Chris Lattnerf64b3522008-03-09 01:54:53 +00002010 // Get the next token of the macro.
2011 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002012
Chris Lattner83bd8282009-05-25 17:16:10 +00002013 // Check for a valid macro arg identifier.
Craig Topperd2d442c2014-05-17 23:10:59 +00002014 if (Tok.getIdentifierInfo() == nullptr ||
Chris Lattner83bd8282009-05-25 17:16:10 +00002015 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2016
2017 // If this is assembler-with-cpp mode, we accept random gibberish after
2018 // the '#' because '#' is often a comment character. However, change
2019 // the kind of the token to tok::unknown so that the preprocessor isn't
2020 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002021 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00002022 LastTok.setKind(tok::unknown);
Eli Friedmancdf8b882013-06-18 21:33:38 +00002023 MI->AddTokenToBody(LastTok);
2024 continue;
Chris Lattner83bd8282009-05-25 17:16:10 +00002025 } else {
2026 Diag(Tok, diag::err_pp_stringize_not_parameter);
Mike Stump11289f42009-09-09 15:08:12 +00002027
Chris Lattner83bd8282009-05-25 17:16:10 +00002028 // Disable __VA_ARGS__ again.
2029 Ident__VA_ARGS__->setIsPoisoned(true);
2030 return;
2031 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002032 }
Mike Stump11289f42009-09-09 15:08:12 +00002033
Chris Lattner83bd8282009-05-25 17:16:10 +00002034 // Things look ok, add the '#' and param name tokens to the macro.
2035 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002036 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00002037 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00002038
Chris Lattnerf64b3522008-03-09 01:54:53 +00002039 // Get the next token of the macro.
2040 LexUnexpandedToken(Tok);
2041 }
2042 }
Mike Stump11289f42009-09-09 15:08:12 +00002043
2044
Chris Lattnerf64b3522008-03-09 01:54:53 +00002045 // Disable __VA_ARGS__ again.
2046 Ident__VA_ARGS__->setIsPoisoned(true);
2047
Chris Lattner57540c52011-04-15 05:22:18 +00002048 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00002049 // replacement list.
2050 unsigned NumTokens = MI->getNumTokens();
2051 if (NumTokens != 0) {
2052 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2053 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002054 return;
2055 }
2056 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2057 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002058 return;
2059 }
2060 }
Mike Stump11289f42009-09-09 15:08:12 +00002061
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002062 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00002063
Chris Lattnerf64b3522008-03-09 01:54:53 +00002064 // Finally, if this identifier already had a macro defined for it, verify that
Alexander Kornienko8b3f6232012-08-29 00:20:03 +00002065 // the macro bodies are identical, and issue diagnostics if they are not.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002066 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner5244f342009-01-16 19:50:11 +00002067 // It is very common for system headers to have tons of macro redefinitions
2068 // and for warnings to be disabled in system headers. If this is the case,
2069 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00002070 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00002071 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002072 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00002073 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002074
Richard Smith7b242542013-03-06 00:46:00 +00002075 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2076 // C++ [cpp.predefined]p4, but allow it as an extension.
2077 if (OtherMI->isBuiltinMacro())
2078 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
Chris Lattnerc0a585d2010-08-17 15:55:45 +00002079 // Macros must be identical. This means all tokens and whitespace
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002080 // separation must be the same. C99 6.10.3p2.
Richard Smith7b242542013-03-06 00:46:00 +00002081 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002082 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
Chris Lattner5244f342009-01-16 19:50:11 +00002083 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2084 << MacroNameTok.getIdentifierInfo();
2085 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2086 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002087 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002088 if (OtherMI->isWarnIfUnused())
2089 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002090 }
Mike Stump11289f42009-09-09 15:08:12 +00002091
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002092 DefMacroDirective *MD =
2093 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00002094
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002095 assert(!MI->isUsed());
2096 // If we need warning for not using the macro, add its location in the
2097 // warn-because-unused-macro set. If it gets used it will be removed from set.
Eli Friedman5ba37d52013-08-22 00:27:10 +00002098 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002099 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002100 MI->setIsWarnIfUnused(true);
2101 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2102 }
2103
Chris Lattner928e9092009-04-12 01:39:54 +00002104 // If the callbacks want to know, tell them about the macro definition.
2105 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002106 Callbacks->MacroDefined(MacroNameTok, MD);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002107}
2108
James Dennettf6333ac2012-06-22 05:46:07 +00002109/// HandleUndefDirective - Implements \#undef.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002110///
2111void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2112 ++NumUndefined;
2113
2114 Token MacroNameTok;
2115 ReadMacroName(MacroNameTok, 2);
Mike Stump11289f42009-09-09 15:08:12 +00002116
Chris Lattnerf64b3522008-03-09 01:54:53 +00002117 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002118 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00002119 return;
Mike Stump11289f42009-09-09 15:08:12 +00002120
Chris Lattnerf64b3522008-03-09 01:54:53 +00002121 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002122 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00002123
Chris Lattnerf64b3522008-03-09 01:54:53 +00002124 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002125 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
Craig Topperd2d442c2014-05-17 23:10:59 +00002126 const MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002127
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002128 // If the callbacks want to know, tell them about the macro #undef.
2129 // Note: no matter if the macro was defined or not.
2130 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002131 Callbacks->MacroUndefined(MacroNameTok, MD);
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002132
Chris Lattnerf64b3522008-03-09 01:54:53 +00002133 // If the macro is not defined, this is a noop undef, just return.
Craig Topperd2d442c2014-05-17 23:10:59 +00002134 if (!MI)
2135 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002136
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00002137 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00002138 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00002139
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002140 if (MI->isWarnIfUnused())
2141 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2142
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002143 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2144 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
Chris Lattnerf64b3522008-03-09 01:54:53 +00002145}
2146
2147
2148//===----------------------------------------------------------------------===//
2149// Preprocessor Conditional Directive Handling.
2150//===----------------------------------------------------------------------===//
2151
James Dennettf6333ac2012-06-22 05:46:07 +00002152/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2153/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2154/// true if any tokens have been returned or pp-directives activated before this
2155/// \#ifndef has been lexed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002156///
2157void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2158 bool ReadAnyTokensBeforeDirective) {
2159 ++NumIf;
2160 Token DirectiveTok = Result;
2161
2162 Token MacroNameTok;
2163 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00002164
Chris Lattnerf64b3522008-03-09 01:54:53 +00002165 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002166 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002167 // Skip code until we get to #endif. This helps with recovery by not
2168 // emitting an error when the #endif is reached.
2169 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2170 /*Foundnonskip*/false, /*FoundElse*/false);
2171 return;
2172 }
Mike Stump11289f42009-09-09 15:08:12 +00002173
Chris Lattnerf64b3522008-03-09 01:54:53 +00002174 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002175 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00002176
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002177 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002178 MacroDirective *MD = getMacroDirective(MII);
Craig Topperd2d442c2014-05-17 23:10:59 +00002179 MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002180
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002181 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002182 // If the start of a top-level #ifdef and if the macro is not defined,
2183 // inform MIOpt that this might be the start of a proper include guard.
2184 // Otherwise it is some other form of unknown conditional which we can't
2185 // handle.
Craig Topperd2d442c2014-05-17 23:10:59 +00002186 if (!ReadAnyTokensBeforeDirective && !MI) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002187 assert(isIfndef && "#ifdef shouldn't reach here");
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002188 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002189 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002190 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002191 }
2192
Chris Lattnerf64b3522008-03-09 01:54:53 +00002193 // If there is a macro, process it.
2194 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002195 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002196
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002197 if (Callbacks) {
2198 if (isIfndef)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002199 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002200 else
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002201 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002202 }
2203
Chris Lattnerf64b3522008-03-09 01:54:53 +00002204 // Should we include the stuff contained by this directive?
2205 if (!MI == isIfndef) {
2206 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00002207 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2208 /*wasskip*/false, /*foundnonskip*/true,
2209 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002210 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002211 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002212 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00002213 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002214 /*FoundElse*/false);
2215 }
2216}
2217
James Dennettf6333ac2012-06-22 05:46:07 +00002218/// HandleIfDirective - Implements the \#if directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002219///
2220void Preprocessor::HandleIfDirective(Token &IfToken,
2221 bool ReadAnyTokensBeforeDirective) {
2222 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00002223
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002224 // Parse and evaluate the conditional expression.
Craig Topperd2d442c2014-05-17 23:10:59 +00002225 IdentifierInfo *IfNDefMacro = nullptr;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002226 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2227 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2228 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00002229
2230 // If this condition is equivalent to #ifndef X, and if this is the first
2231 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002232 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002233 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Richard Smith089ee152013-06-16 05:05:39 +00002234 // FIXME: Pass in the location of the macro name, not the 'if' token.
2235 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
Nuno Lopes363212b2008-06-01 18:31:24 +00002236 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002237 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00002238 }
2239
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002240 if (Callbacks)
2241 Callbacks->If(IfToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002242 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002243 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002244
Chris Lattnerf64b3522008-03-09 01:54:53 +00002245 // Should we include the stuff contained by this directive?
2246 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002247 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002248 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002249 /*foundnonskip*/true, /*foundelse*/false);
2250 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002251 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00002252 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002253 /*FoundElse*/false);
2254 }
2255}
2256
James Dennettf6333ac2012-06-22 05:46:07 +00002257/// HandleEndifDirective - Implements the \#endif directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002258///
2259void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2260 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00002261
Chris Lattnerf64b3522008-03-09 01:54:53 +00002262 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002263 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00002264
Chris Lattnerf64b3522008-03-09 01:54:53 +00002265 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002266 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002267 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00002268 Diag(EndifToken, diag::err_pp_endif_without_if);
2269 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002270 }
Mike Stump11289f42009-09-09 15:08:12 +00002271
Chris Lattnerf64b3522008-03-09 01:54:53 +00002272 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002273 if (CurPPLexer->getConditionalStackDepth() == 0)
2274 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002275
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002276 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002277 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002278
2279 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002280 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002281}
2282
James Dennettf6333ac2012-06-22 05:46:07 +00002283/// HandleElseDirective - Implements the \#else directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002284///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002285void Preprocessor::HandleElseDirective(Token &Result) {
2286 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002287
Chris Lattnerf64b3522008-03-09 01:54:53 +00002288 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002289 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattnerf64b3522008-03-09 01:54:53 +00002291 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002292 if (CurPPLexer->popConditionalLevel(CI)) {
2293 Diag(Result, diag::pp_err_else_without_if);
2294 return;
2295 }
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattnerf64b3522008-03-09 01:54:53 +00002297 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002298 if (CurPPLexer->getConditionalStackDepth() == 0)
2299 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002300
2301 // If this is a #else with a #else before it, report the error.
2302 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002303
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002304 if (Callbacks)
2305 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2306
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002307 // Finally, skip the rest of the contents of this block.
2308 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002309 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002310}
2311
James Dennettf6333ac2012-06-22 05:46:07 +00002312/// HandleElifDirective - Implements the \#elif directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002313///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002314void Preprocessor::HandleElifDirective(Token &ElifToken) {
2315 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002316
Chris Lattnerf64b3522008-03-09 01:54:53 +00002317 // #elif directive in a non-skipping conditional... start skipping.
2318 // We don't care what the condition is, because we will always skip it (since
2319 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002320 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002321 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002322 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002323
2324 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002325 if (CurPPLexer->popConditionalLevel(CI)) {
2326 Diag(ElifToken, diag::pp_err_elif_without_if);
2327 return;
2328 }
Mike Stump11289f42009-09-09 15:08:12 +00002329
Chris Lattnerf64b3522008-03-09 01:54:53 +00002330 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002331 if (CurPPLexer->getConditionalStackDepth() == 0)
2332 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002333
Chris Lattnerf64b3522008-03-09 01:54:53 +00002334 // If this is a #elif with a #else before it, report the error.
2335 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002336
2337 if (Callbacks)
2338 Callbacks->Elif(ElifToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002339 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002340 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002341
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002342 // Finally, skip the rest of the contents of this block.
2343 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002344 /*FoundElse*/CI.FoundElse,
2345 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002346}