blob: 5f80f4487fb4d1d53a02696ddf1b3c3b1c9c7372 [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"
Saleem Abdulrasool19803412014-03-11 22:41:45 +000028#include "llvm/Support/FileSystem.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() {
Ted Kremenekc8456f82010-10-19 22:15:20 +000037 MacroInfoChain *MIChain;
Mike Stump11289f42009-09-09 15:08:12 +000038
Ted Kremenekc8456f82010-10-19 22:15:20 +000039 if (MICache) {
40 MIChain = MICache;
41 MICache = MICache->Next;
Ted Kremenek1f1e4bd2010-10-19 18:16:54 +000042 }
Ted Kremenekc8456f82010-10-19 22:15:20 +000043 else {
44 MIChain = BP.Allocate<MacroInfoChain>();
45 }
46
47 MIChain->Next = MIChainHead;
Craig Topperd2d442c2014-05-17 23:10:59 +000048 MIChain->Prev = nullptr;
Ted Kremenekc8456f82010-10-19 22:15:20 +000049 if (MIChainHead)
50 MIChainHead->Prev = MIChain;
51 MIChainHead = MIChain;
52
53 return &(MIChain->MI);
Chris Lattnerc0a585d2010-08-17 15:55:45 +000054}
55
56MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
57 MacroInfo *MI = AllocateMacroInfo();
Ted Kremenek6c7ea112008-12-15 19:56:42 +000058 new (MI) MacroInfo(L);
59 return MI;
60}
61
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000062MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
63 unsigned SubModuleID) {
Chandler Carruth06dde922014-03-02 13:02:01 +000064 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
65 "alignment for MacroInfo is less than the ID");
Argyrios Kyrtzidisd48b91d2013-04-30 05:05:35 +000066 DeserializedMacroInfoChain *MIChain =
67 BP.Allocate<DeserializedMacroInfoChain>();
68 MIChain->Next = DeserialMIChainHead;
69 DeserialMIChainHead = MIChain;
70
71 MacroInfo *MI = &MIChain->MI;
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000072 new (MI) MacroInfo(L);
73 MI->FromASTFile = true;
74 MI->setOwningModuleID(SubModuleID);
75 return MI;
76}
77
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000078DefMacroDirective *
79Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
80 bool isImported) {
81 DefMacroDirective *MD = BP.Allocate<DefMacroDirective>();
82 new (MD) DefMacroDirective(MI, Loc, isImported);
83 return MD;
84}
85
86UndefMacroDirective *
87Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
88 UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>();
89 new (MD) UndefMacroDirective(UndefLoc);
90 return MD;
91}
92
93VisibilityMacroDirective *
94Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
95 bool isPublic) {
96 VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>();
97 new (MD) VisibilityMacroDirective(Loc, isPublic);
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +000098 return MD;
Chris Lattnerc0a585d2010-08-17 15:55:45 +000099}
100
James Dennettf6333ac2012-06-22 05:46:07 +0000101/// \brief Release the specified MacroInfo to be reused for allocating
102/// new MacroInfo objects.
Chris Lattner66b67d22010-08-18 16:08:51 +0000103void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
Ted Kremenekc8456f82010-10-19 22:15:20 +0000104 MacroInfoChain *MIChain = (MacroInfoChain*) MI;
105 if (MacroInfoChain *Prev = MIChain->Prev) {
106 MacroInfoChain *Next = MIChain->Next;
107 Prev->Next = Next;
108 if (Next)
109 Next->Prev = Prev;
110 }
111 else {
112 assert(MIChainHead == MIChain);
113 MIChainHead = MIChain->Next;
Craig Topperd2d442c2014-05-17 23:10:59 +0000114 MIChainHead->Prev = nullptr;
Ted Kremenekc8456f82010-10-19 22:15:20 +0000115 }
116 MIChain->Next = MICache;
117 MICache = MIChain;
Chris Lattner666f7a42009-02-20 22:19:20 +0000118
Ted Kremenekc8456f82010-10-19 22:15:20 +0000119 MI->Destroy();
120}
Chris Lattner666f7a42009-02-20 22:19:20 +0000121
James Dennettf6333ac2012-06-22 05:46:07 +0000122/// \brief Read and discard all tokens remaining on the current line until
123/// the tok::eod token is found.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000124void Preprocessor::DiscardUntilEndOfDirective() {
125 Token Tmp;
126 do {
127 LexUnexpandedToken(Tmp);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000128 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000129 } while (Tmp.isNot(tok::eod));
Chris Lattnerf64b3522008-03-09 01:54:53 +0000130}
131
Alp Tokerb05e0b52014-05-21 06:13:51 +0000132bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
133 // Missing macro name?
134 if (MacroNameTok.is(tok::eod))
135 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
136
137 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
138 if (!II) {
139 bool Invalid = false;
140 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
141 if (Invalid)
142 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
143
144 const IdentifierInfo &Info = Identifiers.get(Spelling);
145
146 // Allow #defining |and| and friends in microsoft mode.
147 if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MSVCCompat) {
148 MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
149 return false;
150 }
151
152 if (Info.isCPlusPlusOperatorKeyword())
153 // C++ 2.5p2: Alternative tokens behave the same as its primary token
154 // except for their spellings.
155 return Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name)
156 << Spelling;
157
158 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
159 }
160
161 if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
162 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
163 return Diag(MacroNameTok, diag::err_defined_macro_name);
164 }
165
166 if (isDefineUndef == 2 && II->hasMacroDefinition() &&
167 getMacroInfo(II)->isBuiltinMacro()) {
168 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
169 // and C++ [cpp.predefined]p4], but allow it as an extension.
170 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
171 }
172
173 // Okay, we got a good identifier.
174 return false;
175}
176
James Dennettf6333ac2012-06-22 05:46:07 +0000177/// \brief Lex and validate a macro name, which occurs after a
178/// \#define or \#undef.
179///
180/// This sets the token kind to eod and discards the rest
181/// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
182/// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
183/// else (e.g. \#ifdef).
Chris Lattnerf64b3522008-03-09 01:54:53 +0000184void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
185 // Read the token, don't allow macro expansion on it.
186 LexUnexpandedToken(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +0000187
Douglas Gregor12785102010-08-24 20:21:13 +0000188 if (MacroNameTok.is(tok::code_completion)) {
189 if (CodeComplete)
190 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000191 setCodeCompletionReached();
Douglas Gregor12785102010-08-24 20:21:13 +0000192 LexUnexpandedToken(MacroNameTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000193 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000194
195 if (!CheckMacroName(MacroNameTok, isDefineUndef))
Chris Lattner907dfe92008-11-18 07:59:24 +0000196 return;
Alp Tokerb05e0b52014-05-21 06:13:51 +0000197
198 // Invalid macro name, read and discard the rest of the line and set the
199 // token kind to tok::eod if necessary.
200 if (MacroNameTok.isNot(tok::eod)) {
201 MacroNameTok.setKind(tok::eod);
202 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +0000203 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000204}
205
James Dennettf6333ac2012-06-22 05:46:07 +0000206/// \brief Ensure that the next token is a tok::eod token.
207///
208/// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
Chris Lattner0003c272009-04-17 23:30:53 +0000209/// true, then we consider macros that expand to zero tokens as being ok.
210void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000211 Token Tmp;
Chris Lattner0003c272009-04-17 23:30:53 +0000212 // Lex unexpanded tokens for most directives: macros might expand to zero
213 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
214 // #line) allow empty macros.
215 if (EnableMacros)
216 Lex(Tmp);
217 else
218 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattnerf64b3522008-03-09 01:54:53 +0000220 // There should be no tokens after the directive, but we allow them as an
221 // extension.
222 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
223 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000224
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000225 if (Tmp.isNot(tok::eod)) {
Chris Lattner825676a2009-04-14 05:15:20 +0000226 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000227 // or if this is a macro-style preprocessing directive, because it is more
228 // trouble than it is worth to insert /**/ and check that there is no /**/
229 // in the range also.
Douglas Gregora771f462010-03-31 17:46:05 +0000230 FixItHint Hint;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000231 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000232 !CurTokenLexer)
Douglas Gregora771f462010-03-31 17:46:05 +0000233 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
234 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000235 DiscardUntilEndOfDirective();
236 }
237}
238
239
240
James Dennettf6333ac2012-06-22 05:46:07 +0000241/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
242/// decided that the subsequent tokens are in the \#if'd out portion of the
243/// file. Lex the rest of the file, until we see an \#endif. If
Chris Lattnerf64b3522008-03-09 01:54:53 +0000244/// FoundNonSkipPortion is true, then we have already emitted code for part of
James Dennettf6333ac2012-06-22 05:46:07 +0000245/// this \#if directive, so \#else/\#elif blocks should never be entered.
246/// If ElseOk is true, then \#else directives are ok, if not, then we have
247/// already seen one so a \#else directive is a duplicate. When this returns,
248/// the caller can lex the first valid token.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000249void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
250 bool FoundNonSkipPortion,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000251 bool FoundElse,
252 SourceLocation ElseLoc) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000253 ++NumSkipped;
David Blaikie7d170102013-05-15 07:37:26 +0000254 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000255
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000256 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000257 FoundNonSkipPortion, FoundElse);
Mike Stump11289f42009-09-09 15:08:12 +0000258
Ted Kremenek56572ab2008-12-12 18:34:08 +0000259 if (CurPTHLexer) {
260 PTHSkipExcludedConditionalBlock();
261 return;
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattnerf64b3522008-03-09 01:54:53 +0000264 // Enter raw mode to disable identifier lookup (and thus macro expansion),
265 // disabling warnings, etc.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000266 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000267 Token Tok;
268 while (1) {
Chris Lattnerf406b242010-01-18 22:33:01 +0000269 CurLexer->Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000270
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000271 if (Tok.is(tok::code_completion)) {
272 if (CodeComplete)
273 CodeComplete->CodeCompleteInConditionalExclusion();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000274 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000275 continue;
276 }
277
Chris Lattnerf64b3522008-03-09 01:54:53 +0000278 // If this is the end of the buffer, we have an error.
279 if (Tok.is(tok::eof)) {
280 // Emit errors for each unterminated conditional on the stack, including
281 // the current one.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000282 while (!CurPPLexer->ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000283 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000284 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
285 diag::err_pp_unterminated_conditional);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000286 CurPPLexer->ConditionalStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000287 }
288
Chris Lattnerf64b3522008-03-09 01:54:53 +0000289 // Just return and let the caller lex after this #include.
290 break;
291 }
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattnerf64b3522008-03-09 01:54:53 +0000293 // If this token is not a preprocessor directive, just skip it.
294 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
295 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattnerf64b3522008-03-09 01:54:53 +0000297 // We just parsed a # character at the start of a line, so we're in
298 // directive mode. Tell the lexer this so any newlines we see will be
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000299 // converted into an EOD token (this terminates the macro).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000300 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rose176057b2013-02-22 00:32:00 +0000301 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000302
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerf64b3522008-03-09 01:54:53 +0000304 // Read the next token, the directive flavor.
305 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattnerf64b3522008-03-09 01:54:53 +0000307 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
308 // something bogus), skip it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000309 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000310 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000311 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000312 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000313 continue;
314 }
315
316 // If the first letter isn't i or e, it isn't intesting to us. We know that
317 // this is safe in the face of spelling differences, because there is no way
318 // to spell an i/e in a strange way that is another letter. Skipping this
319 // allows us to avoid looking up the identifier info for #define/#undef and
320 // other common directives.
Alp Toker2d57cea2014-05-17 04:53:25 +0000321 StringRef RI = Tok.getRawIdentifier();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000322
Alp Toker2d57cea2014-05-17 04:53:25 +0000323 char FirstChar = RI[0];
Mike Stump11289f42009-09-09 15:08:12 +0000324 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000325 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000326 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000327 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000328 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000329 continue;
330 }
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattnerf64b3522008-03-09 01:54:53 +0000332 // Get the identifier name without trigraphs or embedded newlines. Note
333 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
334 // when skipping.
Benjamin Kramer144884642009-12-31 13:32:38 +0000335 char DirectiveBuf[20];
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000336 StringRef Directive;
Alp Toker2d57cea2014-05-17 04:53:25 +0000337 if (!Tok.needsCleaning() && RI.size() < 20) {
338 Directive = RI;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000339 } else {
340 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramer144884642009-12-31 13:32:38 +0000341 unsigned IdLen = DirectiveStr.size();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000342 if (IdLen >= 20) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000343 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000344 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000345 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000346 continue;
347 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000348 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000349 Directive = StringRef(DirectiveBuf, IdLen);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Benjamin Kramer144884642009-12-31 13:32:38 +0000352 if (Directive.startswith("if")) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000353 StringRef Sub = Directive.substr(2);
Benjamin Kramer144884642009-12-31 13:32:38 +0000354 if (Sub.empty() || // "if"
355 Sub == "def" || // "ifdef"
356 Sub == "ndef") { // "ifndef"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000357 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
358 // bother parsing the condition.
359 DiscardUntilEndOfDirective();
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000360 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000361 /*foundnonskip*/false,
Chandler Carruth540960f2011-01-03 17:40:17 +0000362 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000363 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000364 } else if (Directive[0] == 'e') {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000365 StringRef Sub = Directive.substr(1);
Benjamin Kramer144884642009-12-31 13:32:38 +0000366 if (Sub == "ndif") { // "endif"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000367 PPConditionalInfo CondInfo;
368 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000369 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000370 (void)InCond; // Silence warning in no-asserts mode.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000371 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattnerf64b3522008-03-09 01:54:53 +0000373 // If we popped the outermost skipping block, we're done skipping!
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000374 if (!CondInfo.WasSkipping) {
Richard Smith87d8fb92012-06-24 23:56:26 +0000375 // Restore the value of LexingRawMode so that trailing comments
376 // are handled correctly, if we've reached the outermost block.
377 CurPPLexer->LexingRawMode = false;
Richard Smithd0124572012-06-21 00:35:03 +0000378 CheckEndOfDirective("endif");
Richard Smith87d8fb92012-06-24 23:56:26 +0000379 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000380 if (Callbacks)
381 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000382 break;
Richard Smithd0124572012-06-21 00:35:03 +0000383 } else {
384 DiscardUntilEndOfDirective();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000385 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000386 } else if (Sub == "lse") { // "else".
Chris Lattnerf64b3522008-03-09 01:54:53 +0000387 // #else directive in a skipping conditional. If not in some other
388 // skipping conditional, and if #else hasn't already been seen, enter it
389 // as a non-skipping conditional.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000390 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerf64b3522008-03-09 01:54:53 +0000392 // If this is a #else with a #else before it, report the error.
393 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattnerf64b3522008-03-09 01:54:53 +0000395 // Note that we've seen a #else in this conditional.
396 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattnerf64b3522008-03-09 01:54:53 +0000398 // If the conditional is at the top level, and the #if block wasn't
399 // entered, enter the #else block now.
400 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
401 CondInfo.FoundNonSkip = true;
Richard Smith87d8fb92012-06-24 23:56:26 +0000402 // Restore the value of LexingRawMode so that trailing comments
403 // are handled correctly.
404 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000405 CheckEndOfDirective("else");
Richard Smith87d8fb92012-06-24 23:56:26 +0000406 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000407 if (Callbacks)
408 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000409 break;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000410 } else {
411 DiscardUntilEndOfDirective(); // C99 6.10p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000412 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000413 } else if (Sub == "lif") { // "elif".
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000414 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000415
John Thompson17c35732013-12-04 20:19:30 +0000416 // If this is a #elif with a #else before it, report the error.
417 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
418
Chris Lattnerf64b3522008-03-09 01:54:53 +0000419 // If this is in a skipping block or if we're already handled this #if
420 // block, don't bother parsing the condition.
421 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
422 DiscardUntilEndOfDirective();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000423 } else {
John Thompson17c35732013-12-04 20:19:30 +0000424 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000425 // Restore the value of LexingRawMode so that identifiers are
426 // looked up, etc, inside the #elif expression.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000427 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
428 CurPPLexer->LexingRawMode = false;
Craig Topperd2d442c2014-05-17 23:10:59 +0000429 IdentifierInfo *IfNDefMacro = nullptr;
John Thompson17c35732013-12-04 20:19:30 +0000430 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000431 CurPPLexer->LexingRawMode = true;
John Thompson17c35732013-12-04 20:19:30 +0000432 if (Callbacks) {
433 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000434 Callbacks->Elif(Tok.getLocation(),
John Thompson17c35732013-12-04 20:19:30 +0000435 SourceRange(CondBegin, CondEnd),
John Thompson87f9fef2013-12-07 08:41:15 +0000436 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
John Thompson17c35732013-12-04 20:19:30 +0000437 }
438 // If this condition is true, enter it!
439 if (CondValue) {
440 CondInfo.FoundNonSkip = true;
441 break;
442 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000443 }
444 }
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000447 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000448 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000449 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000450 }
451
452 // Finally, if we are out of the conditional (saw an #endif or ran off the end
453 // of the file, just stop skipping and return to lexing whatever came after
454 // the #if block.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000455 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000456
457 if (Callbacks) {
458 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
459 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
460 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000461}
462
Ted Kremenek56572ab2008-12-12 18:34:08 +0000463void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump11289f42009-09-09 15:08:12 +0000464
465 while (1) {
Ted Kremenek56572ab2008-12-12 18:34:08 +0000466 assert(CurPTHLexer);
467 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenek56572ab2008-12-12 18:34:08 +0000469 // Skip to the next '#else', '#elif', or #endif.
470 if (CurPTHLexer->SkipBlock()) {
471 // We have reached an #endif. Both the '#' and 'endif' tokens
472 // have been consumed by the PTHLexer. Just pop off the condition level.
473 PPConditionalInfo CondInfo;
474 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000475 (void)InCond; // Silence warning in no-asserts mode.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000476 assert(!InCond && "Can't be skipping if not in a conditional!");
477 break;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek56572ab2008-12-12 18:34:08 +0000480 // We have reached a '#else' or '#elif'. Lex the next token to get
481 // the directive flavor.
482 Token Tok;
483 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek56572ab2008-12-12 18:34:08 +0000485 // We can actually look up the IdentifierInfo here since we aren't in
486 // raw mode.
487 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
488
489 if (K == tok::pp_else) {
490 // #else: Enter the else condition. We aren't in a nested condition
491 // since we skip those. We're always in the one matching the last
492 // blocked we skipped.
493 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
494 // Note that we've seen a #else in this conditional.
495 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Ted Kremenek56572ab2008-12-12 18:34:08 +0000497 // If the #if block wasn't entered then enter the #else block now.
498 if (!CondInfo.FoundNonSkip) {
499 CondInfo.FoundNonSkip = true;
Mike Stump11289f42009-09-09 15:08:12 +0000500
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000501 // Scan until the eod token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000502 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar2cba6be2009-04-13 17:57:49 +0000503 DiscardUntilEndOfDirective();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000504 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000505
Ted Kremenek56572ab2008-12-12 18:34:08 +0000506 break;
507 }
Mike Stump11289f42009-09-09 15:08:12 +0000508
Ted Kremenek56572ab2008-12-12 18:34:08 +0000509 // Otherwise skip this block.
510 continue;
511 }
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenek56572ab2008-12-12 18:34:08 +0000513 assert(K == tok::pp_elif);
514 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
515
516 // If this is a #elif with a #else before it, report the error.
517 if (CondInfo.FoundElse)
518 Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000519
Ted Kremenek56572ab2008-12-12 18:34:08 +0000520 // If this is in a skipping block or if we're already handled this #if
Mike Stump11289f42009-09-09 15:08:12 +0000521 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000522 if (CondInfo.FoundNonSkip)
523 continue;
524
525 // Evaluate the condition of the #elif.
Craig Topperd2d442c2014-05-17 23:10:59 +0000526 IdentifierInfo *IfNDefMacro = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000527 CurPTHLexer->ParsingPreprocessorDirective = true;
528 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
529 CurPTHLexer->ParsingPreprocessorDirective = false;
530
531 // If this condition is true, enter it!
532 if (ShouldEnter) {
533 CondInfo.FoundNonSkip = true;
534 break;
535 }
536
537 // Otherwise, skip this block and go to the next one.
538 continue;
539 }
540}
541
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000542Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) {
543 ModuleMap &ModMap = HeaderInfo.getModuleMap();
544 if (SourceMgr.isInMainFile(FilenameLoc)) {
545 if (Module *CurMod = getCurrentModule())
546 return CurMod; // Compiling a module.
547 return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
548 }
549 // Try to determine the module of the include directive.
Daniel Jasper88d86952013-12-03 20:30:36 +0000550 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
Manuel Klimek98a9a6c2014-03-19 10:22:36 +0000551 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(FilenameLoc));
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000552 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
553 // The include comes from a file.
554 return ModMap.findModuleForHeader(EntryOfIncl).getModule();
555 } else {
556 // The include does not come from a file,
557 // so it is probably a module compilation.
558 return getCurrentModule();
559 }
560}
561
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000562const FileEntry *Preprocessor::LookupFile(
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000563 SourceLocation FilenameLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000564 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000565 bool isAngled,
566 const DirectoryLookup *FromDir,
567 const DirectoryLookup *&CurDir,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000568 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000569 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000570 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000571 bool SkipCache) {
Will Wilson0fafd342013-12-27 19:46:16 +0000572 // If the header lookup mechanism may be relative to the current inclusion
573 // stack, record the parent #includes.
574 SmallVector<const FileEntry *, 16> Includers;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000575 if (!FromDir) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000576 FileID FID = getCurrentFileLexer()->getFileID();
Will Wilson0fafd342013-12-27 19:46:16 +0000577 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner022923a2009-02-04 19:45:07 +0000579 // If there is no file entry associated with this file, it must be the
580 // predefines buffer. Any other file is not lexed with a normal lexer, so
Douglas Gregor618e64a2010-08-08 07:49:23 +0000581 // it won't be scanned for preprocessor directives. If we have the
582 // predefines buffer, resolve #include references (which come from the
583 // -include command line argument) as if they came from the main file, this
584 // affects file lookup etc.
Will Wilson0fafd342013-12-27 19:46:16 +0000585 if (!FileEnt)
586 FileEnt = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
587
588 if (FileEnt)
589 Includers.push_back(FileEnt);
590
591 // MSVC searches the current include stack from top to bottom for
592 // headers included by quoted include directives.
593 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
Alp Tokerbfa39342014-01-14 12:51:41 +0000594 if (LangOpts.MSVCCompat && !isAngled) {
Will Wilson0fafd342013-12-27 19:46:16 +0000595 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
596 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
597 if (IsFileLexer(ISEntry))
598 if ((FileEnt = SourceMgr.getFileEntryForID(
599 ISEntry.ThePPLexer->getFileID())))
600 Includers.push_back(FileEnt);
601 }
Chris Lattner022923a2009-02-04 19:45:07 +0000602 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000605 // Do a standard file entry lookup.
606 CurDir = CurDirLookup;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000607 const FileEntry *FE = HeaderInfo.LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000608 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
609 RelativePath, SuggestedModule, SkipCache);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000610 if (FE) {
Daniel Jasper5c77e392014-03-14 14:53:17 +0000611 if (SuggestedModule && !LangOpts.AsmPreprocessor)
Daniel Jasper92669ee2013-12-20 12:09:36 +0000612 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
613 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000614 return FE;
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Will Wilson0fafd342013-12-27 19:46:16 +0000617 const FileEntry *CurFileEnt;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000618 // Otherwise, see if this is a subframework header. If so, this is relative
619 // to one of the headers on the #include stack. Walk the list of the current
620 // headers on the #include stack and pass them to HeaderInfo.
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000621 if (IsFileLexer()) {
Ben Langmuir71e1a642014-05-05 21:44:13 +0000622 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) {
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000623 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000624 SearchPath, RelativePath,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000625 SuggestedModule))) {
626 if (SuggestedModule && !LangOpts.AsmPreprocessor)
627 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
628 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000629 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000630 }
631 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000634 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
635 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000636 if (IsFileLexer(ISEntry)) {
Mike Stump11289f42009-09-09 15:08:12 +0000637 if ((CurFileEnt =
Ben Langmuir71e1a642014-05-05 21:44:13 +0000638 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000639 if ((FE = HeaderInfo.LookupSubframeworkHeader(
Douglas Gregorf5f94522013-02-08 00:10:48 +0000640 Filename, CurFileEnt, SearchPath, RelativePath,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000641 SuggestedModule))) {
642 if (SuggestedModule && !LangOpts.AsmPreprocessor)
643 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
644 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000645 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000646 }
647 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000648 }
649 }
Mike Stump11289f42009-09-09 15:08:12 +0000650
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000651 // Otherwise, we really couldn't find the file.
Craig Topperd2d442c2014-05-17 23:10:59 +0000652 return nullptr;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000653}
654
Chris Lattnerf64b3522008-03-09 01:54:53 +0000655
656//===----------------------------------------------------------------------===//
657// Preprocessor Directive Handling.
658//===----------------------------------------------------------------------===//
659
David Blaikied5321242012-06-06 18:52:13 +0000660class Preprocessor::ResetMacroExpansionHelper {
661public:
662 ResetMacroExpansionHelper(Preprocessor *pp)
663 : PP(pp), save(pp->DisableMacroExpansion) {
664 if (pp->MacroExpansionInDirectivesOverride)
665 pp->DisableMacroExpansion = false;
666 }
667 ~ResetMacroExpansionHelper() {
668 PP->DisableMacroExpansion = save;
669 }
670private:
671 Preprocessor *PP;
672 bool save;
673};
674
Chris Lattnerf64b3522008-03-09 01:54:53 +0000675/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump11289f42009-09-09 15:08:12 +0000676/// at the start of a line. This consumes the directive, modifies the
Chris Lattnerf64b3522008-03-09 01:54:53 +0000677/// lexer/preprocessor state, and advances the lexer(s) so that the next token
678/// read is the correct one.
679void Preprocessor::HandleDirective(Token &Result) {
680 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattnerf64b3522008-03-09 01:54:53 +0000682 // We just parsed a # character at the start of a line, so we're in directive
683 // mode. Tell the lexer this so any newlines we see will be converted into an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000684 // EOD token (which terminates the directive).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000685 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000686 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Mike Stump11289f42009-09-09 15:08:12 +0000687
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000688 bool ImmediatelyAfterTopLevelIfndef =
689 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
690 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
691
Chris Lattnerf64b3522008-03-09 01:54:53 +0000692 ++NumDirectives;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000693
Chris Lattnerf64b3522008-03-09 01:54:53 +0000694 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump11289f42009-09-09 15:08:12 +0000695 // work, we have to remember if we had read any tokens *before* this
Chris Lattnerf64b3522008-03-09 01:54:53 +0000696 // pp-directive.
Chris Lattner8cf1f932009-12-14 04:54:40 +0000697 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattner2d17ab72009-03-18 21:00:25 +0000699 // Save the '#' token in case we need to return it later.
700 Token SavedHash = Result;
Mike Stump11289f42009-09-09 15:08:12 +0000701
Chris Lattnerf64b3522008-03-09 01:54:53 +0000702 // Read the next token, the directive flavor. This isn't expanded due to
703 // C99 6.10.3p8.
704 LexUnexpandedToken(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattnerf64b3522008-03-09 01:54:53 +0000706 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
707 // #define A(x) #x
708 // A(abc
709 // #warning blah
710 // def)
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000711 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
712 // not support this for #include-like directives, since that can result in
713 // terrible diagnostics, and does not work in GCC.
714 if (InMacroArgs) {
715 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
716 switch (II->getPPKeywordID()) {
717 case tok::pp_include:
718 case tok::pp_import:
719 case tok::pp_include_next:
720 case tok::pp___include_macros:
721 Diag(Result, diag::err_embedded_include) << II->getName();
722 DiscardUntilEndOfDirective();
723 return;
724 default:
725 break;
726 }
727 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000728 Diag(Result, diag::ext_embedded_directive);
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000729 }
Mike Stump11289f42009-09-09 15:08:12 +0000730
David Blaikied5321242012-06-06 18:52:13 +0000731 // Temporarily enable macro expansion if set so
732 // and reset to previous state when returning from this function.
733 ResetMacroExpansionHelper helper(this);
734
Chris Lattnerf64b3522008-03-09 01:54:53 +0000735 switch (Result.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000736 case tok::eod:
Chris Lattnerf64b3522008-03-09 01:54:53 +0000737 return; // null directive.
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000738 case tok::code_completion:
739 if (CodeComplete)
740 CodeComplete->CodeCompleteDirective(
741 CurPPLexer->getConditionalStackDepth() > 0);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000742 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000743 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000744 case tok::numeric_constant: // # 7 GNU line marker directive.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000745 if (getLangOpts().AsmPreprocessor)
Chris Lattner5eb8ae22009-03-18 20:41:10 +0000746 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner76e68962009-01-26 06:19:46 +0000747 return HandleDigitDirective(Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000748 default:
749 IdentifierInfo *II = Result.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +0000750 if (!II) break; // Not an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattnerf64b3522008-03-09 01:54:53 +0000752 // Ask what the preprocessor keyword ID is.
753 switch (II->getPPKeywordID()) {
754 default: break;
755 // C99 6.10.1 - Conditional Inclusion.
756 case tok::pp_if:
757 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
758 case tok::pp_ifdef:
759 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
760 case tok::pp_ifndef:
761 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
762 case tok::pp_elif:
763 return HandleElifDirective(Result);
764 case tok::pp_else:
765 return HandleElseDirective(Result);
766 case tok::pp_endif:
767 return HandleEndifDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerf64b3522008-03-09 01:54:53 +0000769 // C99 6.10.2 - Source File Inclusion.
770 case tok::pp_include:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000771 // Handle #include.
772 return HandleIncludeDirective(SavedHash.getLocation(), Result);
Chris Lattner14a7f392009-04-08 18:24:34 +0000773 case tok::pp___include_macros:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000774 // Handle -imacros.
775 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnerf64b3522008-03-09 01:54:53 +0000777 // C99 6.10.3 - Macro Replacement.
778 case tok::pp_define:
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000779 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000780 case tok::pp_undef:
781 return HandleUndefDirective(Result);
782
783 // C99 6.10.4 - Line Control.
784 case tok::pp_line:
Chris Lattner100c65e2009-01-26 05:29:08 +0000785 return HandleLineDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattnerf64b3522008-03-09 01:54:53 +0000787 // C99 6.10.5 - Error Directive.
788 case tok::pp_error:
789 return HandleUserDiagnosticDirective(Result, false);
Mike Stump11289f42009-09-09 15:08:12 +0000790
Chris Lattnerf64b3522008-03-09 01:54:53 +0000791 // C99 6.10.6 - Pragma Directive.
792 case tok::pp_pragma:
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000793 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
Mike Stump11289f42009-09-09 15:08:12 +0000794
Chris Lattnerf64b3522008-03-09 01:54:53 +0000795 // GNU Extensions.
796 case tok::pp_import:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000797 return HandleImportDirective(SavedHash.getLocation(), Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000798 case tok::pp_include_next:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000799 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattnerf64b3522008-03-09 01:54:53 +0000801 case tok::pp_warning:
802 Diag(Result, diag::ext_pp_warning_directive);
803 return HandleUserDiagnosticDirective(Result, true);
804 case tok::pp_ident:
805 return HandleIdentSCCSDirective(Result);
806 case tok::pp_sccs:
807 return HandleIdentSCCSDirective(Result);
808 case tok::pp_assert:
809 //isExtension = true; // FIXME: implement #assert
810 break;
811 case tok::pp_unassert:
812 //isExtension = true; // FIXME: implement #unassert
813 break;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +0000814
Douglas Gregor663b48f2012-01-03 19:48:16 +0000815 case tok::pp___public_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000816 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000817 return HandleMacroPublicDirective(Result);
818 break;
819
Douglas Gregor663b48f2012-01-03 19:48:16 +0000820 case tok::pp___private_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000821 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000822 return HandleMacroPrivateDirective(Result);
823 break;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000824 }
825 break;
826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner2d17ab72009-03-18 21:00:25 +0000828 // If this is a .S file, treat unknown # directives as non-preprocessor
829 // directives. This is important because # may be a comment or introduce
830 // various pseudo-ops. Just return the # token and push back the following
831 // token to be lexed next time.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000832 if (getLangOpts().AsmPreprocessor) {
Daniel Dunbar48b4d1e2009-07-13 21:48:50 +0000833 Token *Toks = new Token[2];
Chris Lattner2d17ab72009-03-18 21:00:25 +0000834 // Return the # and the token after it.
Mike Stump11289f42009-09-09 15:08:12 +0000835 Toks[0] = SavedHash;
Chris Lattner2d17ab72009-03-18 21:00:25 +0000836 Toks[1] = Result;
Chris Lattner56f64c12011-01-06 05:01:51 +0000837
838 // If the second token is a hashhash token, then we need to translate it to
839 // unknown so the token lexer doesn't try to perform token pasting.
840 if (Result.is(tok::hashhash))
841 Toks[1].setKind(tok::unknown);
842
Chris Lattner2d17ab72009-03-18 21:00:25 +0000843 // Enter this token stream so that we re-lex the tokens. Make sure to
844 // enable macro expansion, in case the token after the # is an identifier
845 // that is expanded.
846 EnterTokenStream(Toks, 2, false, true);
847 return;
848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattnerf64b3522008-03-09 01:54:53 +0000850 // If we reached here, the preprocessing token is not valid!
851 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattnerf64b3522008-03-09 01:54:53 +0000853 // Read the rest of the PP line.
854 DiscardUntilEndOfDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000855
Chris Lattnerf64b3522008-03-09 01:54:53 +0000856 // Okay, we're done parsing the directive.
857}
858
Chris Lattner76e68962009-01-26 06:19:46 +0000859/// GetLineValue - Convert a numeric token into an unsigned value, emitting
860/// Diagnostic DiagID if it is invalid, and returning the value in Val.
861static bool GetLineValue(Token &DigitTok, unsigned &Val,
Michael Ilsemane910cc82013-04-10 01:04:18 +0000862 unsigned DiagID, Preprocessor &PP,
863 bool IsGNULineDirective=false) {
Chris Lattner76e68962009-01-26 06:19:46 +0000864 if (DigitTok.isNot(tok::numeric_constant)) {
865 PP.Diag(DigitTok, DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000866
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000867 if (DigitTok.isNot(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000868 PP.DiscardUntilEndOfDirective();
869 return true;
870 }
Mike Stump11289f42009-09-09 15:08:12 +0000871
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000872 SmallString<64> IntegerBuffer;
Chris Lattner76e68962009-01-26 06:19:46 +0000873 IntegerBuffer.resize(DigitTok.getLength());
874 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregordc970f02010-03-16 22:30:13 +0000875 bool Invalid = false;
876 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
877 if (Invalid)
878 return true;
879
Chris Lattnerd66f1722009-04-18 18:35:15 +0000880 // Verify that we have a simple digit-sequence, and compute the value. This
881 // is always a simple digit string computed in decimal, so we do this manually
882 // here.
883 Val = 0;
884 for (unsigned i = 0; i != ActualLength; ++i) {
Richard Smith7f2707a2013-09-26 18:13:20 +0000885 // C++1y [lex.fcon]p1:
886 // Optional separating single quotes in a digit-sequence are ignored
887 if (DigitTokBegin[i] == '\'')
888 continue;
889
Jordan Rosea7d03842013-02-08 22:30:41 +0000890 if (!isDigit(DigitTokBegin[i])) {
Chris Lattnerd66f1722009-04-18 18:35:15 +0000891 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
Michael Ilsemane910cc82013-04-10 01:04:18 +0000892 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000893 PP.DiscardUntilEndOfDirective();
894 return true;
895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Chris Lattnerd66f1722009-04-18 18:35:15 +0000897 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
898 if (NextVal < Val) { // overflow.
899 PP.Diag(DigitTok, DiagID);
900 PP.DiscardUntilEndOfDirective();
901 return true;
902 }
903 Val = NextVal;
Chris Lattner76e68962009-01-26 06:19:46 +0000904 }
Mike Stump11289f42009-09-09 15:08:12 +0000905
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000906 if (DigitTokBegin[0] == '0' && Val)
Michael Ilsemane910cc82013-04-10 01:04:18 +0000907 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
908 << IsGNULineDirective;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Chris Lattner76e68962009-01-26 06:19:46 +0000910 return false;
911}
912
James Dennettf6333ac2012-06-22 05:46:07 +0000913/// \brief Handle a \#line directive: C99 6.10.4.
914///
915/// The two acceptable forms are:
916/// \verbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000917/// # line digit-sequence
918/// # line digit-sequence "s-char-sequence"
James Dennettf6333ac2012-06-22 05:46:07 +0000919/// \endverbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000920void Preprocessor::HandleLineDirective(Token &Tok) {
921 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
922 // expanded.
923 Token DigitTok;
924 Lex(DigitTok);
925
Chris Lattner100c65e2009-01-26 05:29:08 +0000926 // Validate the number and convert it to an unsigned.
Chris Lattner76e68962009-01-26 06:19:46 +0000927 unsigned LineNo;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000928 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner100c65e2009-01-26 05:29:08 +0000929 return;
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000930
931 if (LineNo == 0)
932 Diag(DigitTok, diag::ext_pp_line_zero);
Chris Lattner100c65e2009-01-26 05:29:08 +0000933
Chris Lattner76e68962009-01-26 06:19:46 +0000934 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
935 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Eli Friedman192e0342011-10-10 23:35:28 +0000936 unsigned LineLimit = 32768U;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000937 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Eli Friedman192e0342011-10-10 23:35:28 +0000938 LineLimit = 2147483648U;
Chris Lattner100c65e2009-01-26 05:29:08 +0000939 if (LineNo >= LineLimit)
940 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000941 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
Richard Smithacd4d3d2011-10-15 01:18:56 +0000942 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
Mike Stump11289f42009-09-09 15:08:12 +0000943
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000944 int FilenameID = -1;
Chris Lattner100c65e2009-01-26 05:29:08 +0000945 Token StrTok;
946 Lex(StrTok);
947
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000948 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
949 // string followed by eod.
950 if (StrTok.is(tok::eod))
Chris Lattner100c65e2009-01-26 05:29:08 +0000951 ; // ok
952 else if (StrTok.isNot(tok::string_literal)) {
953 Diag(StrTok, diag::err_pp_line_invalid_filename);
Richard Smithd67aea22012-03-06 03:21:47 +0000954 return DiscardUntilEndOfDirective();
955 } else if (StrTok.hasUDSuffix()) {
956 Diag(StrTok, diag::err_invalid_string_udl);
957 return DiscardUntilEndOfDirective();
Chris Lattner100c65e2009-01-26 05:29:08 +0000958 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000959 // Parse and validate the string, converting it into a unique ID.
960 StringLiteralParser Literal(&StrTok, 1, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000961 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000962 if (Literal.hadError)
963 return DiscardUntilEndOfDirective();
964 if (Literal.Pascal) {
965 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
966 return DiscardUntilEndOfDirective();
967 }
Jay Foad9a6b0982011-06-21 15:13:30 +0000968 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +0000969
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000970 // Verify that there is nothing after the string, other than EOD. Because
Chris Lattner0003c272009-04-17 23:30:53 +0000971 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
972 CheckEndOfDirective("line", true);
Chris Lattner100c65e2009-01-26 05:29:08 +0000973 }
Mike Stump11289f42009-09-09 15:08:12 +0000974
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000975 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner839150e2009-03-27 17:13:49 +0000977 if (Callbacks)
Chris Lattnerc745cec2010-04-14 04:28:50 +0000978 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
979 PPCallbacks::RenameFile,
Chris Lattner839150e2009-03-27 17:13:49 +0000980 SrcMgr::C_User);
Chris Lattner100c65e2009-01-26 05:29:08 +0000981}
982
Chris Lattner76e68962009-01-26 06:19:46 +0000983/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
984/// marker directive.
985static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
986 bool &IsSystemHeader, bool &IsExternCHeader,
987 Preprocessor &PP) {
988 unsigned FlagVal;
989 Token FlagTok;
990 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000991 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000992 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
993 return true;
994
995 if (FlagVal == 1) {
996 IsFileEntry = true;
Mike Stump11289f42009-09-09 15:08:12 +0000997
Chris Lattner76e68962009-01-26 06:19:46 +0000998 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000999 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001000 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1001 return true;
1002 } else if (FlagVal == 2) {
1003 IsFileExit = true;
Mike Stump11289f42009-09-09 15:08:12 +00001004
Chris Lattner1c967782009-02-04 06:25:26 +00001005 SourceManager &SM = PP.getSourceManager();
1006 // If we are leaving the current presumed file, check to make sure the
1007 // presumed include stack isn't empty!
1008 FileID CurFileID =
Chandler Carruthc7ca5212011-07-25 20:52:32 +00001009 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
Chris Lattner1c967782009-02-04 06:25:26 +00001010 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +00001011 if (PLoc.isInvalid())
1012 return true;
1013
Chris Lattner1c967782009-02-04 06:25:26 +00001014 // If there is no include loc (main file) or if the include loc is in a
1015 // different physical file, then we aren't in a "1" line marker flag region.
1016 SourceLocation IncLoc = PLoc.getIncludeLoc();
1017 if (IncLoc.isInvalid() ||
Chandler Carruthc7ca5212011-07-25 20:52:32 +00001018 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
Chris Lattner1c967782009-02-04 06:25:26 +00001019 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1020 PP.DiscardUntilEndOfDirective();
1021 return true;
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner76e68962009-01-26 06:19:46 +00001024 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001025 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001026 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1027 return true;
1028 }
1029
1030 // We must have 3 if there are still flags.
1031 if (FlagVal != 3) {
1032 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001033 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001034 return true;
1035 }
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner76e68962009-01-26 06:19:46 +00001037 IsSystemHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattner76e68962009-01-26 06:19:46 +00001039 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001040 if (FlagTok.is(tok::eod)) return false;
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001041 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner76e68962009-01-26 06:19:46 +00001042 return true;
1043
1044 // We must have 4 if there is yet another flag.
1045 if (FlagVal != 4) {
1046 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001047 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001048 return true;
1049 }
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner76e68962009-01-26 06:19:46 +00001051 IsExternCHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner76e68962009-01-26 06:19:46 +00001053 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001054 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001055
1056 // There are no more valid flags here.
1057 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001058 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001059 return true;
1060}
1061
1062/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1063/// one of the following forms:
1064///
1065/// # 42
Mike Stump11289f42009-09-09 15:08:12 +00001066/// # 42 "file" ('1' | '2')?
Chris Lattner76e68962009-01-26 06:19:46 +00001067/// # 42 "file" ('1' | '2')? '3' '4'?
1068///
1069void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1070 // Validate the number and convert it to an unsigned. GNU does not have a
1071 // line # limit other than it fit in 32-bits.
1072 unsigned LineNo;
1073 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
Michael Ilsemane910cc82013-04-10 01:04:18 +00001074 *this, true))
Chris Lattner76e68962009-01-26 06:19:46 +00001075 return;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Chris Lattner76e68962009-01-26 06:19:46 +00001077 Token StrTok;
1078 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattner76e68962009-01-26 06:19:46 +00001080 bool IsFileEntry = false, IsFileExit = false;
1081 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001082 int FilenameID = -1;
1083
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001084 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1085 // string followed by eod.
1086 if (StrTok.is(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +00001087 ; // ok
1088 else if (StrTok.isNot(tok::string_literal)) {
1089 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001090 return DiscardUntilEndOfDirective();
Richard Smithd67aea22012-03-06 03:21:47 +00001091 } else if (StrTok.hasUDSuffix()) {
1092 Diag(StrTok, diag::err_invalid_string_udl);
1093 return DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001094 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001095 // Parse and validate the string, converting it into a unique ID.
1096 StringLiteralParser Literal(&StrTok, 1, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001097 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001098 if (Literal.hadError)
1099 return DiscardUntilEndOfDirective();
1100 if (Literal.Pascal) {
1101 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1102 return DiscardUntilEndOfDirective();
1103 }
Jay Foad9a6b0982011-06-21 15:13:30 +00001104 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner76e68962009-01-26 06:19:46 +00001106 // If a filename was present, read any flags that are present.
Mike Stump11289f42009-09-09 15:08:12 +00001107 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001108 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner76e68962009-01-26 06:19:46 +00001109 return;
Chris Lattner76e68962009-01-26 06:19:46 +00001110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001112 // Create a line note with this information.
1113 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump11289f42009-09-09 15:08:12 +00001114 IsFileEntry, IsFileExit,
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001115 IsSystemHeader, IsExternCHeader);
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattner839150e2009-03-27 17:13:49 +00001117 // If the preprocessor has callbacks installed, notify them of the #line
1118 // change. This is used so that the line marker comes out in -E mode for
1119 // example.
1120 if (Callbacks) {
1121 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1122 if (IsFileEntry)
1123 Reason = PPCallbacks::EnterFile;
1124 else if (IsFileExit)
1125 Reason = PPCallbacks::ExitFile;
1126 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1127 if (IsExternCHeader)
1128 FileKind = SrcMgr::C_ExternCSystem;
1129 else if (IsSystemHeader)
1130 FileKind = SrcMgr::C_System;
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattnerc745cec2010-04-14 04:28:50 +00001132 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner839150e2009-03-27 17:13:49 +00001133 }
Chris Lattner76e68962009-01-26 06:19:46 +00001134}
1135
1136
Chris Lattner38d7fd22009-01-26 05:30:54 +00001137/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1138///
Mike Stump11289f42009-09-09 15:08:12 +00001139void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001140 bool isWarning) {
Chris Lattner38d7fd22009-01-26 05:30:54 +00001141 // PTH doesn't emit #warning or #error directives.
1142 if (CurPTHLexer)
Chris Lattner100c65e2009-01-26 05:29:08 +00001143 return CurPTHLexer->DiscardToEndOfLine();
1144
Chris Lattnerf64b3522008-03-09 01:54:53 +00001145 // Read the rest of the line raw. We do this because we don't want macros
1146 // to be expanded and we don't require that the tokens be valid preprocessing
1147 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1148 // collapse multiple consequtive white space between tokens, but this isn't
1149 // specified by the standard.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001150 SmallString<128> Message;
1151 CurLexer->ReadToEndOfLine(&Message);
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001152
1153 // Find the first non-whitespace character, so that we can make the
1154 // diagnostic more succinct.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001155 StringRef Msg = Message.str().ltrim(" ");
1156
Chris Lattner100c65e2009-01-26 05:29:08 +00001157 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001158 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001159 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001160 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001161}
1162
1163/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1164///
1165void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1166 // Yes, this directive is an extension.
1167 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001168
Chris Lattnerf64b3522008-03-09 01:54:53 +00001169 // Read the string argument.
1170 Token StrTok;
1171 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattnerf64b3522008-03-09 01:54:53 +00001173 // If the token kind isn't a string, it's a malformed directive.
1174 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001175 StrTok.isNot(tok::wide_string_literal)) {
1176 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001177 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001178 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001179 return;
1180 }
Mike Stump11289f42009-09-09 15:08:12 +00001181
Richard Smithd67aea22012-03-06 03:21:47 +00001182 if (StrTok.hasUDSuffix()) {
1183 Diag(StrTok, diag::err_invalid_string_udl);
1184 return DiscardUntilEndOfDirective();
1185 }
1186
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001187 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001188 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001189
Douglas Gregordc970f02010-03-16 22:30:13 +00001190 if (Callbacks) {
1191 bool Invalid = false;
1192 std::string Str = getSpelling(StrTok, &Invalid);
1193 if (!Invalid)
1194 Callbacks->Ident(Tok.getLocation(), Str);
1195 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001196}
1197
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001198/// \brief Handle a #public directive.
1199void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001200 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 #__public_macro line.
1208 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001209
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001210 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001211 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001212 MacroDirective *MD = getMacroDirective(II);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +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 Gregor4a69c2e2011-09-01 17:04:32 +00001217 return;
1218 }
1219
1220 // Note that this macro has now been exported.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001221 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1222 MacroNameTok.getLocation(), /*IsPublic=*/true));
Douglas Gregorebf00492011-10-17 15:32:29 +00001223}
1224
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001225/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001226void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1227 Token MacroNameTok;
1228 ReadMacroName(MacroNameTok, 2);
1229
1230 // Error reading macro name? If so, diagnostic already issued.
1231 if (MacroNameTok.is(tok::eod))
1232 return;
1233
Douglas Gregor663b48f2012-01-03 19:48:16 +00001234 // Check to see if this is the last token on the #__private_macro line.
1235 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001236
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001237 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregorebf00492011-10-17 15:32:29 +00001238 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001239 MacroDirective *MD = getMacroDirective(II);
Douglas Gregorebf00492011-10-17 15:32:29 +00001240
1241 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001242 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001243 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregorebf00492011-10-17 15:32:29 +00001244 return;
1245 }
1246
1247 // Note that this macro has now been marked private.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001248 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1249 MacroNameTok.getLocation(), /*IsPublic=*/false));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001250}
1251
Chris Lattnerf64b3522008-03-09 01:54:53 +00001252//===----------------------------------------------------------------------===//
1253// Preprocessor Include Directive Handling.
1254//===----------------------------------------------------------------------===//
1255
1256/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
James Dennettf6333ac2012-06-22 05:46:07 +00001257/// checked and spelled filename, e.g. as an operand of \#include. This returns
Chris Lattnerf64b3522008-03-09 01:54:53 +00001258/// true if the input filename was in <>'s or false if it were in ""'s. The
1259/// caller is expected to provide a buffer that is large enough to hold the
1260/// spelling of the filename, but is also expected to handle the case when
1261/// this method decides to use a different buffer.
1262bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001263 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001264 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001265 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattnerf64b3522008-03-09 01:54:53 +00001267 // Make sure the filename is <x> or "x".
1268 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001269 if (Buffer[0] == '<') {
1270 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001271 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001272 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001273 return true;
1274 }
1275 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001276 } else if (Buffer[0] == '"') {
1277 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001278 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001279 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001280 return true;
1281 }
1282 isAngled = false;
1283 } else {
1284 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001285 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001286 return true;
1287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Chris Lattnerf64b3522008-03-09 01:54:53 +00001289 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001290 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001291 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001292 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001293 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001294 }
Mike Stump11289f42009-09-09 15:08:12 +00001295
Chris Lattnerf64b3522008-03-09 01:54:53 +00001296 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001297 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001298 return isAngled;
1299}
1300
James Dennett4a4f72d2013-11-27 01:27:40 +00001301// \brief Handle cases where the \#include name is expanded from a macro
1302// as multiple tokens, which need to be glued together.
1303//
1304// This occurs for code like:
1305// \code
1306// \#define FOO <a/b.h>
1307// \#include FOO
1308// \endcode
1309// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1310//
1311// This code concatenates and consumes tokens up to the '>' token. It returns
1312// false if the > was found, otherwise it returns true if it finds and consumes
1313// the EOD marker.
1314bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001315 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001316 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001317
John Thompsonb5353522009-10-30 13:49:06 +00001318 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001319 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001320 End = CurTok.getLocation();
1321
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001322 // FIXME: Provide code completion for #includes.
1323 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001324 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001325 Lex(CurTok);
1326 continue;
1327 }
1328
Chris Lattnerf64b3522008-03-09 01:54:53 +00001329 // Append the spelling of this token to the buffer. If there was a space
1330 // before it, add it now.
1331 if (CurTok.hasLeadingSpace())
1332 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001333
Chris Lattnerf64b3522008-03-09 01:54:53 +00001334 // Get the spelling of the token, directly into FilenameBuffer if possible.
1335 unsigned PreAppendSize = FilenameBuffer.size();
1336 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001337
Chris Lattnerf64b3522008-03-09 01:54:53 +00001338 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001339 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001340
Chris Lattnerf64b3522008-03-09 01:54:53 +00001341 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1342 if (BufPtr != &FilenameBuffer[PreAppendSize])
1343 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001344
Chris Lattnerf64b3522008-03-09 01:54:53 +00001345 // Resize FilenameBuffer to the correct size.
1346 if (CurTok.getLength() != ActualLen)
1347 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001348
Chris Lattnerf64b3522008-03-09 01:54:53 +00001349 // If we found the '>' marker, return success.
1350 if (CurTok.is(tok::greater))
1351 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001352
John Thompsonb5353522009-10-30 13:49:06 +00001353 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001354 }
1355
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001356 // If we hit the eod marker, emit an error and return true so that the caller
1357 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001358 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001359 return true;
1360}
1361
Richard Smith34f30512013-11-23 04:06:09 +00001362/// \brief Push a token onto the token stream containing an annotation.
1363static void EnterAnnotationToken(Preprocessor &PP,
1364 SourceLocation Begin, SourceLocation End,
1365 tok::TokenKind Kind, void *AnnotationVal) {
1366 Token *Tok = new Token[1];
1367 Tok[0].startToken();
1368 Tok[0].setKind(Kind);
1369 Tok[0].setLocation(Begin);
1370 Tok[0].setAnnotationEndLoc(End);
1371 Tok[0].setAnnotationValue(AnnotationVal);
1372 PP.EnterTokenStream(Tok, 1, true, true);
1373}
1374
James Dennettf6333ac2012-06-22 05:46:07 +00001375/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1376/// the file to be included from the lexer, then include it! This is a common
1377/// routine with functionality shared between \#include, \#include_next and
1378/// \#import. LookupFrom is set when this is a \#include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001379/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001380void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1381 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001382 const DirectoryLookup *LookupFrom,
1383 bool isImport) {
1384
1385 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001386 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattnerf64b3522008-03-09 01:54:53 +00001388 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001389 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001390 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001391 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001392 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001393
Chris Lattnerf64b3522008-03-09 01:54:53 +00001394 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001395 case tok::eod:
1396 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001397 return;
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattnerf64b3522008-03-09 01:54:53 +00001399 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001400 case tok::string_literal:
1401 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001402 End = FilenameTok.getLocation();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001403 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001404 break;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Chris Lattnerf64b3522008-03-09 01:54:53 +00001406 case tok::less:
1407 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1408 // case, glue the tokens together into FilenameBuffer and interpret those.
1409 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001410 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001411 return; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001412 Filename = FilenameBuffer.str();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001413 CharEnd = End.getLocWithOffset(1);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001414 break;
1415 default:
1416 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1417 DiscardUntilEndOfDirective();
1418 return;
1419 }
Mike Stump11289f42009-09-09 15:08:12 +00001420
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001421 CharSourceRange FilenameRange
1422 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
Aaron Ballman611306e2012-03-02 22:51:54 +00001423 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001424 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001425 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001426 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1427 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001428 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001429 DiscardUntilEndOfDirective();
1430 return;
1431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001433 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001434 // we allow macros that expand to nothing after the filename, because this
1435 // falls into the category of "#include pp-tokens new-line" specified in
1436 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001437 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001438
1439 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001440 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1441 Diag(FilenameTok, diag::err_pp_include_too_deep);
1442 return;
1443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
John McCall32f5fe12011-09-30 05:12:12 +00001445 // Complain about attempts to #include files in an audit pragma.
1446 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1447 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1448 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1449
1450 // Immediately leave the pragma.
1451 PragmaARCCFCodeAuditedLoc = SourceLocation();
1452 }
1453
Aaron Ballman611306e2012-03-02 22:51:54 +00001454 if (HeaderInfo.HasIncludeAliasMap()) {
1455 // Map the filename with the brackets still attached. If the name doesn't
1456 // map to anything, fall back on the filename we've already gotten the
1457 // spelling for.
1458 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1459 if (!NewName.empty())
1460 Filename = NewName;
1461 }
1462
Chris Lattnerf64b3522008-03-09 01:54:53 +00001463 // Search include directories.
1464 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001465 SmallString<1024> SearchPath;
1466 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001467 // We get the raw path only if we have 'Callbacks' to which we later pass
1468 // the path.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001469 ModuleMap::KnownHeader SuggestedModule;
1470 SourceLocation FilenameLoc = FilenameTok.getLocation();
Saleem Abdulrasool729b7d32014-03-12 02:26:08 +00001471 SmallString<128> NormalizedPath;
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001472 if (LangOpts.MSVCCompat) {
1473 NormalizedPath = Filename.str();
1474 llvm::sys::fs::normalize_separators(NormalizedPath);
1475 }
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001476 const FileEntry *File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001477 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001478 isAngled, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1479 Callbacks ? &RelativePath : nullptr,
1480 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : nullptr);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001481
Douglas Gregor11729f02011-11-30 18:12:06 +00001482 if (Callbacks) {
1483 if (!File) {
1484 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001485 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001486 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1487 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1488 // Add the recovery path to the list of search paths.
Daniel Dunbarae4feb62013-01-25 01:50:28 +00001489 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Douglas Gregor11729f02011-11-30 18:12:06 +00001490 HeaderInfo.AddSearchPath(DL, isAngled);
1491
1492 // Try the lookup again, skipping the cache.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001493 File = LookupFile(FilenameLoc,
1494 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1495 : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001496 isAngled, LookupFrom, CurDir, nullptr, nullptr,
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001497 HeaderInfo.getHeaderSearchOpts().ModuleMaps
Daniel Jasper07e6c402013-08-05 20:26:17 +00001498 ? &SuggestedModule
Craig Topperd2d442c2014-05-17 23:10:59 +00001499 : nullptr,
Daniel Jasper07e6c402013-08-05 20:26:17 +00001500 /*SkipCache*/ true);
Douglas Gregor11729f02011-11-30 18:12:06 +00001501 }
1502 }
1503 }
1504
Daniel Jasper07e6c402013-08-05 20:26:17 +00001505 if (!SuggestedModule || !getLangOpts().Modules) {
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001506 // Notify the callback object that we've seen an inclusion directive.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001507 Callbacks->InclusionDirective(HashLoc, IncludeTok,
1508 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1509 : Filename,
1510 isAngled, FilenameRange, File, SearchPath,
Craig Topperd2d442c2014-05-17 23:10:59 +00001511 RelativePath, /*ImportedModule=*/nullptr);
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001512 }
Douglas Gregor11729f02011-11-30 18:12:06 +00001513 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001514
1515 if (!File) {
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001516 if (!SuppressIncludeNotFoundError) {
1517 // If the file could not be located and it was included via angle
1518 // brackets, we can attempt a lookup as though it were a quoted path to
1519 // provide the user with a possible fixit.
1520 if (isAngled) {
Daniel Jasper07e6c402013-08-05 20:26:17 +00001521 File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001522 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001523 false, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1524 Callbacks ? &RelativePath : nullptr,
1525 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule
1526 : nullptr);
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001527 if (File) {
1528 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1529 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1530 Filename <<
1531 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1532 }
1533 }
1534 // If the file is still not found, just go with the vanilla diagnostic
1535 if (!File)
1536 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1537 }
1538 if (!File)
1539 return;
Douglas Gregor11729f02011-11-30 18:12:06 +00001540 }
1541
Douglas Gregor97eec242011-09-15 22:00:41 +00001542 // If we are supposed to import a module rather than including the header,
1543 // do so now.
Daniel Jasper07e6c402013-08-05 20:26:17 +00001544 if (SuggestedModule && getLangOpts().Modules) {
Douglas Gregor71944202011-11-30 00:36:36 +00001545 // Compute the module access path corresponding to this module.
1546 // FIXME: Should we have a second loadModule() overload to avoid this
1547 // extra lookup step?
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001548 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001549 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001550 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1551 FilenameTok.getLocation()));
1552 std::reverse(Path.begin(), Path.end());
1553
Douglas Gregor41e115a2011-11-30 18:02:36 +00001554 // Warn that we're replacing the include/import with a module import.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001555 SmallString<128> PathString;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001556 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1557 if (I)
1558 PathString += '.';
1559 PathString += Path[I].first->getName();
1560 }
1561 int IncludeKind = 0;
1562
1563 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1564 case tok::pp_include:
1565 IncludeKind = 0;
1566 break;
1567
1568 case tok::pp_import:
1569 IncludeKind = 1;
1570 break;
1571
Douglas Gregor4401fbe2011-11-30 18:03:26 +00001572 case tok::pp_include_next:
1573 IncludeKind = 2;
1574 break;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001575
1576 case tok::pp___include_macros:
1577 IncludeKind = 3;
1578 break;
1579
1580 default:
1581 llvm_unreachable("unknown include directive kind");
Douglas Gregor41e115a2011-11-30 18:02:36 +00001582 }
1583
Douglas Gregor2537a362011-12-08 17:01:29 +00001584 // Determine whether we are actually building the module that this
1585 // include directive maps to.
1586 bool BuildingImportedModule
David Blaikiebbafb8a2012-03-11 07:00:24 +00001587 = Path[0].first->getName() == getLangOpts().CurrentModule;
Richard Smith34f30512013-11-23 04:06:09 +00001588
David Blaikiebbafb8a2012-03-11 07:00:24 +00001589 if (!BuildingImportedModule && getLangOpts().ObjC2) {
Douglas Gregor2537a362011-12-08 17:01:29 +00001590 // If we're not building the imported module, warn that we're going
1591 // to automatically turn this inclusion directive into a module import.
Douglas Gregorda82e702012-01-03 19:32:59 +00001592 // We only do this in Objective-C, where we have a module-import syntax.
Douglas Gregor2537a362011-12-08 17:01:29 +00001593 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1594 /*IsTokenRange=*/false);
1595 Diag(HashLoc, diag::warn_auto_module_import)
1596 << IncludeKind << PathString
1597 << FixItHint::CreateReplacement(ReplaceRange,
Douglas Gregorc50d4922012-12-11 22:11:52 +00001598 "@import " + PathString.str().str() + ";");
Douglas Gregor2537a362011-12-08 17:01:29 +00001599 }
Douglas Gregor41e115a2011-11-30 18:02:36 +00001600
Richard Smithce587f52013-11-15 04:24:58 +00001601 // Load the module. Only make macros visible. We'll make the declarations
1602 // visible when the parser gets here.
1603 Module::NameVisibilityKind Visibility = Module::MacrosVisible;
Douglas Gregor7a626572012-11-29 23:55:25 +00001604 ModuleLoadResult Imported
Douglas Gregor98a52db2011-12-20 00:28:52 +00001605 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1606 /*IsIncludeDirective=*/true);
Craig Topperd2d442c2014-05-17 23:10:59 +00001607 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
Argyrios Kyrtzidis051b4432012-09-29 01:06:01 +00001608 "the imported module is different than the suggested one");
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00001609
1610 if (!Imported && hadModuleLoaderFatalFailure()) {
1611 // With a fatal failure in the module loader, we abort parsing.
1612 Token &Result = IncludeTok;
1613 if (CurLexer) {
1614 Result.startToken();
1615 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1616 CurLexer->cutOffLexing();
1617 } else {
1618 assert(CurPTHLexer && "#include but no current lexer set!");
1619 CurPTHLexer->getEOF(Result);
1620 }
1621 return;
1622 }
Richard Smithce587f52013-11-15 04:24:58 +00001623
Douglas Gregor2537a362011-12-08 17:01:29 +00001624 // If this header isn't part of the module we're building, we're done.
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001625 if (!BuildingImportedModule && Imported) {
1626 if (Callbacks) {
1627 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1628 FilenameRange, File,
1629 SearchPath, RelativePath, Imported);
1630 }
Richard Smithce587f52013-11-15 04:24:58 +00001631
1632 if (IncludeKind != 3) {
1633 // Let the parser know that we hit a module import, and it should
1634 // make the module visible.
1635 // FIXME: Produce this as the current token directly, rather than
1636 // allocating a new token for it.
Richard Smith34f30512013-11-23 04:06:09 +00001637 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include,
1638 Imported);
Richard Smithce587f52013-11-15 04:24:58 +00001639 }
Douglas Gregor2537a362011-12-08 17:01:29 +00001640 return;
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001641 }
Douglas Gregor7a626572012-11-29 23:55:25 +00001642
1643 // If we failed to find a submodule that we expected to find, we can
1644 // continue. Otherwise, there's an error in the included file, so we
1645 // don't want to include it.
1646 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1647 return;
1648 }
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001649 }
1650
1651 if (Callbacks && SuggestedModule) {
1652 // We didn't notify the callback object that we've seen an inclusion
1653 // directive before. Now that we are parsing the include normally and not
1654 // turning it to a module import, notify the callback object.
1655 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1656 FilenameRange, File,
1657 SearchPath, RelativePath,
Craig Topperd2d442c2014-05-17 23:10:59 +00001658 /*ImportedModule=*/nullptr);
Douglas Gregor97eec242011-09-15 22:00:41 +00001659 }
1660
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001661 // The #included file will be considered to be a system header if either it is
1662 // in a system include directory, or if the #includer is a system include
1663 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001664 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001665 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001666 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001667
Chris Lattner72286d62010-04-19 20:44:31 +00001668 // Ask HeaderInfo if we should enter this #include file. If not, #including
1669 // this file will have no effect.
1670 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001671 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001672 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner72286d62010-04-19 20:44:31 +00001673 return;
1674 }
1675
Chris Lattnerf64b3522008-03-09 01:54:53 +00001676 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001677 SourceLocation IncludePos = End;
1678 // If the filename string was the result of macro expansions, set the include
1679 // position on the file where it will be included and after the expansions.
1680 if (IncludePos.isMacroID())
1681 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1682 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Peter Collingbourned395b932011-06-30 16:41:03 +00001683 assert(!FID.isInvalid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001684
Richard Smith34f30512013-11-23 04:06:09 +00001685 // Determine if we're switching to building a new submodule, and which one.
1686 ModuleMap::KnownHeader BuildingModule;
1687 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) {
1688 Module *RequestingModule = getModuleForLocation(FilenameLoc);
1689 BuildingModule =
1690 HeaderInfo.getModuleMap().findModuleForHeader(File, RequestingModule);
1691 }
1692
1693 // If all is good, enter the new file!
Richard Smith67294e22014-01-31 20:47:44 +00001694 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1695 return;
Richard Smith34f30512013-11-23 04:06:09 +00001696
1697 // If we're walking into another part of the same module, let the parser
1698 // know that any future declarations are within that other submodule.
Richard Smith67294e22014-01-31 20:47:44 +00001699 if (BuildingModule) {
1700 assert(!CurSubmodule && "should not have marked this as a module yet");
1701 CurSubmodule = BuildingModule.getModule();
1702
Richard Smith34f30512013-11-23 04:06:09 +00001703 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin,
Richard Smith67294e22014-01-31 20:47:44 +00001704 CurSubmodule);
1705 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001706}
1707
James Dennettf6333ac2012-06-22 05:46:07 +00001708/// HandleIncludeNextDirective - Implements \#include_next.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001709///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001710void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1711 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001712 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001713
Chris Lattnerf64b3522008-03-09 01:54:53 +00001714 // #include_next is like #include, except that we start searching after
1715 // the current found directory. If we can't do this, issue a
1716 // diagnostic.
1717 const DirectoryLookup *Lookup = CurDirLookup;
1718 if (isInPrimaryFile()) {
Craig Topperd2d442c2014-05-17 23:10:59 +00001719 Lookup = nullptr;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001720 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Craig Topperd2d442c2014-05-17 23:10:59 +00001721 } else if (!Lookup) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001722 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1723 } else {
1724 // Start looking up in the next directory.
1725 ++Lookup;
1726 }
Mike Stump11289f42009-09-09 15:08:12 +00001727
Douglas Gregor796d76a2010-10-20 22:00:55 +00001728 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001729}
1730
James Dennettf6333ac2012-06-22 05:46:07 +00001731/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
Aaron Ballman0467f552012-03-18 03:10:37 +00001732void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1733 // The Microsoft #import directive takes a type library and generates header
1734 // files from it, and includes those. This is beyond the scope of what clang
1735 // does, so we ignore it and error out. However, #import can optionally have
1736 // trailing attributes that span multiple lines. We're going to eat those
1737 // so we can continue processing from there.
1738 Diag(Tok, diag::err_pp_import_directive_ms );
1739
1740 // Read tokens until we get to the end of the directive. Note that the
1741 // directive can be split over multiple lines using the backslash character.
1742 DiscardUntilEndOfDirective();
1743}
1744
James Dennettf6333ac2012-06-22 05:46:07 +00001745/// HandleImportDirective - Implements \#import.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001746///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001747void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1748 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001749 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
Alp Tokerbfa39342014-01-14 12:51:41 +00001750 if (LangOpts.MSVCCompat)
Aaron Ballman0467f552012-03-18 03:10:37 +00001751 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001752 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001753 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001754 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001755}
1756
Chris Lattner58a1eb02009-04-08 18:46:40 +00001757/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1758/// pseudo directive in the predefines buffer. This handles it by sucking all
1759/// tokens through the preprocessor and discarding them (only keeping the side
1760/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001761void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1762 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001763 // This directive should only occur in the predefines buffer. If not, emit an
1764 // error and reject it.
1765 SourceLocation Loc = IncludeMacrosTok.getLocation();
1766 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1767 Diag(IncludeMacrosTok.getLocation(),
1768 diag::pp_include_macros_out_of_predefines);
1769 DiscardUntilEndOfDirective();
1770 return;
1771 }
Mike Stump11289f42009-09-09 15:08:12 +00001772
Chris Lattnere01d82b2009-04-08 20:53:24 +00001773 // Treat this as a normal #include for checking purposes. If this is
1774 // successful, it will push a new lexer onto the include stack.
Craig Topperd2d442c2014-05-17 23:10:59 +00001775 HandleIncludeDirective(HashLoc, IncludeMacrosTok, nullptr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001776
Chris Lattnere01d82b2009-04-08 20:53:24 +00001777 Token TmpTok;
1778 do {
1779 Lex(TmpTok);
1780 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1781 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001782}
1783
Chris Lattnerf64b3522008-03-09 01:54:53 +00001784//===----------------------------------------------------------------------===//
1785// Preprocessor Macro Directive Handling.
1786//===----------------------------------------------------------------------===//
1787
1788/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1789/// definition has just been read. Lex the rest of the arguments and the
1790/// closing ), updating MI with what we learn. Return true if an error occurs
1791/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001792bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001793 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001794
Chris Lattnerf64b3522008-03-09 01:54:53 +00001795 while (1) {
1796 LexUnexpandedToken(Tok);
1797 switch (Tok.getKind()) {
1798 case tok::r_paren:
1799 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001800 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001801 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001802 // Otherwise we have #define FOO(A,)
1803 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1804 return true;
1805 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001806 if (!LangOpts.C99)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001807 Diag(Tok, LangOpts.CPlusPlus11 ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001808 diag::warn_cxx98_compat_variadic_macro :
1809 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001810
Joey Gouly1d58cdb2013-01-17 17:35:00 +00001811 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1812 if (LangOpts.OpenCL) {
1813 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1814 return true;
1815 }
1816
Chris Lattnerf64b3522008-03-09 01:54:53 +00001817 // Lex the token after the identifier.
1818 LexUnexpandedToken(Tok);
1819 if (Tok.isNot(tok::r_paren)) {
1820 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1821 return true;
1822 }
1823 // Add the __VA_ARGS__ identifier as an argument.
1824 Arguments.push_back(Ident__VA_ARGS__);
1825 MI->setIsC99Varargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001826 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001827 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001828 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001829 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1830 return true;
1831 default:
1832 // Handle keywords and identifiers here to accept things like
1833 // #define Foo(for) for.
1834 IdentifierInfo *II = Tok.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +00001835 if (!II) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001836 // #define X(1
1837 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1838 return true;
1839 }
1840
1841 // If this is already used as an argument, it is used multiple times (e.g.
1842 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001843 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001844 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001845 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001846 return true;
1847 }
Mike Stump11289f42009-09-09 15:08:12 +00001848
Chris Lattnerf64b3522008-03-09 01:54:53 +00001849 // Add the argument to the macro info.
1850 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001851
Chris Lattnerf64b3522008-03-09 01:54:53 +00001852 // Lex the token after the identifier.
1853 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001854
Chris Lattnerf64b3522008-03-09 01:54:53 +00001855 switch (Tok.getKind()) {
1856 default: // #define X(A B
1857 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1858 return true;
1859 case tok::r_paren: // #define X(A)
Chris Lattner70946da2009-02-20 22:46:43 +00001860 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001861 return false;
1862 case tok::comma: // #define X(A,
1863 break;
1864 case tok::ellipsis: // #define X(A... -> GCC extension
1865 // Diagnose extension.
1866 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001867
Chris Lattnerf64b3522008-03-09 01:54:53 +00001868 // Lex the token after the identifier.
1869 LexUnexpandedToken(Tok);
1870 if (Tok.isNot(tok::r_paren)) {
1871 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1872 return true;
1873 }
Mike Stump11289f42009-09-09 15:08:12 +00001874
Chris Lattnerf64b3522008-03-09 01:54:53 +00001875 MI->setIsGNUVarargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001876 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001877 return false;
1878 }
1879 }
1880 }
1881}
1882
James Dennettf6333ac2012-06-22 05:46:07 +00001883/// HandleDefineDirective - Implements \#define. This consumes the entire macro
Chris Lattnerf64b3522008-03-09 01:54:53 +00001884/// line then lets the caller lex the next real token.
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001885void Preprocessor::HandleDefineDirective(Token &DefineTok,
1886 bool ImmediatelyAfterHeaderGuard) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001887 ++NumDefined;
1888
1889 Token MacroNameTok;
1890 ReadMacroName(MacroNameTok, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001891
Chris Lattnerf64b3522008-03-09 01:54:53 +00001892 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001893 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001894 return;
1895
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001896 Token LastTok = MacroNameTok;
1897
Chris Lattnerf64b3522008-03-09 01:54:53 +00001898 // If we are supposed to keep comments in #defines, reenable comment saving
1899 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00001900 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00001901
Chris Lattnerf64b3522008-03-09 01:54:53 +00001902 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001903 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001904
Chris Lattnerf64b3522008-03-09 01:54:53 +00001905 Token Tok;
1906 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001907
Chris Lattnerf64b3522008-03-09 01:54:53 +00001908 // If this is a function-like macro definition, parse the argument list,
1909 // marking each of the identifiers as being used as macro arguments. Also,
1910 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001911 if (Tok.is(tok::eod)) {
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001912 if (ImmediatelyAfterHeaderGuard) {
1913 // Save this macro information since it may part of a header guard.
1914 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
1915 MacroNameTok.getLocation());
1916 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001917 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00001918 } else if (Tok.hasLeadingSpace()) {
1919 // This is a normal token with leading space. Clear the leading space
1920 // marker on the first token to get proper expansion.
1921 Tok.clearFlag(Token::LeadingSpace);
1922 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001923 // This is a function-like macro definition. Read the argument list.
1924 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001925 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001926 // Forget about MI.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001927 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001928 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001929 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00001930 DiscardUntilEndOfDirective();
1931 return;
1932 }
1933
Chris Lattner249c38b2009-04-19 18:26:34 +00001934 // If this is a definition of a variadic C99 function-like macro, not using
1935 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00001936
Chris Lattner249c38b2009-04-19 18:26:34 +00001937 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1938 // This gets unpoisoned where it is allowed.
1939 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1940 if (MI->isC99Varargs())
1941 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00001942
Chris Lattnerf64b3522008-03-09 01:54:53 +00001943 // Read the first token after the arg list for down below.
1944 LexUnexpandedToken(Tok);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001945 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001946 // C99 requires whitespace between the macro definition and the body. Emit
1947 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00001948 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001949 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00001950 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1951 // first character of a replacement list is not a character required by
1952 // subclause 5.2.1, then there shall be white-space separation between the
1953 // identifier and the replacement list.". 5.2.1 lists this set:
1954 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1955 // is irrelevant here.
1956 bool isInvalid = false;
1957 if (Tok.is(tok::at)) // @ is not in the list above.
1958 isInvalid = true;
1959 else if (Tok.is(tok::unknown)) {
1960 // If we have an unknown token, it is something strange like "`". Since
1961 // all of valid characters would have lexed into a single character
1962 // token of some sort, we know this is not a valid case.
1963 isInvalid = true;
1964 }
1965 if (isInvalid)
1966 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1967 else
1968 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001969 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001970
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001971 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001972 LastTok = Tok;
1973
Chris Lattnerf64b3522008-03-09 01:54:53 +00001974 // Read the rest of the macro body.
1975 if (MI->isObjectLike()) {
1976 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001977 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001978 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001979 MI->AddTokenToBody(Tok);
1980 // Get the next token of the macro.
1981 LexUnexpandedToken(Tok);
1982 }
Mike Stump11289f42009-09-09 15:08:12 +00001983
Chris Lattnerf64b3522008-03-09 01:54:53 +00001984 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00001985 // Otherwise, read the body of a function-like macro. While we are at it,
1986 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1987 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001988 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001989 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001990
Eli Friedman14d3c792012-11-14 02:18:46 +00001991 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001992 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001993
Chris Lattnerf64b3522008-03-09 01:54:53 +00001994 // Get the next token of the macro.
1995 LexUnexpandedToken(Tok);
1996 continue;
1997 }
Mike Stump11289f42009-09-09 15:08:12 +00001998
Richard Smith701a3522013-07-09 01:00:29 +00001999 // If we're in -traditional mode, then we should ignore stringification
2000 // and token pasting. Mark the tokens as unknown so as not to confuse
2001 // things.
2002 if (getLangOpts().TraditionalCPP) {
2003 Tok.setKind(tok::unknown);
2004 MI->AddTokenToBody(Tok);
2005
2006 // Get the next token of the macro.
2007 LexUnexpandedToken(Tok);
2008 continue;
2009 }
2010
Eli Friedman14d3c792012-11-14 02:18:46 +00002011 if (Tok.is(tok::hashhash)) {
2012
2013 // If we see token pasting, check if it looks like the gcc comma
2014 // pasting extension. We'll use this information to suppress
2015 // diagnostics later on.
2016
2017 // Get the next token of the macro.
2018 LexUnexpandedToken(Tok);
2019
2020 if (Tok.is(tok::eod)) {
2021 MI->AddTokenToBody(LastTok);
2022 break;
2023 }
2024
2025 unsigned NumTokens = MI->getNumTokens();
2026 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2027 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2028 MI->setHasCommaPasting();
2029
David Majnemer76faf1f2013-11-05 09:30:17 +00002030 // Things look ok, add the '##' token to the macro.
Eli Friedman14d3c792012-11-14 02:18:46 +00002031 MI->AddTokenToBody(LastTok);
Eli Friedman14d3c792012-11-14 02:18:46 +00002032 continue;
2033 }
2034
Chris Lattnerf64b3522008-03-09 01:54:53 +00002035 // Get the next token of the macro.
2036 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002037
Chris Lattner83bd8282009-05-25 17:16:10 +00002038 // Check for a valid macro arg identifier.
Craig Topperd2d442c2014-05-17 23:10:59 +00002039 if (Tok.getIdentifierInfo() == nullptr ||
Chris Lattner83bd8282009-05-25 17:16:10 +00002040 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2041
2042 // If this is assembler-with-cpp mode, we accept random gibberish after
2043 // the '#' because '#' is often a comment character. However, change
2044 // the kind of the token to tok::unknown so that the preprocessor isn't
2045 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002046 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00002047 LastTok.setKind(tok::unknown);
Eli Friedmancdf8b882013-06-18 21:33:38 +00002048 MI->AddTokenToBody(LastTok);
2049 continue;
Chris Lattner83bd8282009-05-25 17:16:10 +00002050 } else {
2051 Diag(Tok, diag::err_pp_stringize_not_parameter);
2052 ReleaseMacroInfo(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002053
Chris Lattner83bd8282009-05-25 17:16:10 +00002054 // Disable __VA_ARGS__ again.
2055 Ident__VA_ARGS__->setIsPoisoned(true);
2056 return;
2057 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002058 }
Mike Stump11289f42009-09-09 15:08:12 +00002059
Chris Lattner83bd8282009-05-25 17:16:10 +00002060 // Things look ok, add the '#' and param name tokens to the macro.
2061 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002062 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00002063 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00002064
Chris Lattnerf64b3522008-03-09 01:54:53 +00002065 // Get the next token of the macro.
2066 LexUnexpandedToken(Tok);
2067 }
2068 }
Mike Stump11289f42009-09-09 15:08:12 +00002069
2070
Chris Lattnerf64b3522008-03-09 01:54:53 +00002071 // Disable __VA_ARGS__ again.
2072 Ident__VA_ARGS__->setIsPoisoned(true);
2073
Chris Lattner57540c52011-04-15 05:22:18 +00002074 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00002075 // replacement list.
2076 unsigned NumTokens = MI->getNumTokens();
2077 if (NumTokens != 0) {
2078 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2079 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002080 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002081 return;
2082 }
2083 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2084 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002085 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002086 return;
2087 }
2088 }
Mike Stump11289f42009-09-09 15:08:12 +00002089
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002090 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00002091
Chris Lattnerf64b3522008-03-09 01:54:53 +00002092 // Finally, if this identifier already had a macro defined for it, verify that
Alexander Kornienko8b3f6232012-08-29 00:20:03 +00002093 // the macro bodies are identical, and issue diagnostics if they are not.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002094 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner5244f342009-01-16 19:50:11 +00002095 // It is very common for system headers to have tons of macro redefinitions
2096 // and for warnings to be disabled in system headers. If this is the case,
2097 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00002098 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00002099 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002100 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00002101 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002102
Richard Smith7b242542013-03-06 00:46:00 +00002103 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2104 // C++ [cpp.predefined]p4, but allow it as an extension.
2105 if (OtherMI->isBuiltinMacro())
2106 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
Chris Lattnerc0a585d2010-08-17 15:55:45 +00002107 // Macros must be identical. This means all tokens and whitespace
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002108 // separation must be the same. C99 6.10.3p2.
Richard Smith7b242542013-03-06 00:46:00 +00002109 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002110 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
Chris Lattner5244f342009-01-16 19:50:11 +00002111 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2112 << MacroNameTok.getIdentifierInfo();
2113 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2114 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002115 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002116 if (OtherMI->isWarnIfUnused())
2117 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002118 }
Mike Stump11289f42009-09-09 15:08:12 +00002119
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002120 DefMacroDirective *MD =
2121 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00002122
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002123 assert(!MI->isUsed());
2124 // If we need warning for not using the macro, add its location in the
2125 // warn-because-unused-macro set. If it gets used it will be removed from set.
Eli Friedman5ba37d52013-08-22 00:27:10 +00002126 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002127 Diags->getDiagnosticLevel(diag::pp_macro_not_used,
David Blaikie9c902b52011-09-25 23:23:43 +00002128 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002129 MI->setIsWarnIfUnused(true);
2130 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2131 }
2132
Chris Lattner928e9092009-04-12 01:39:54 +00002133 // If the callbacks want to know, tell them about the macro definition.
2134 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002135 Callbacks->MacroDefined(MacroNameTok, MD);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002136}
2137
James Dennettf6333ac2012-06-22 05:46:07 +00002138/// HandleUndefDirective - Implements \#undef.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002139///
2140void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2141 ++NumUndefined;
2142
2143 Token MacroNameTok;
2144 ReadMacroName(MacroNameTok, 2);
Mike Stump11289f42009-09-09 15:08:12 +00002145
Chris Lattnerf64b3522008-03-09 01:54:53 +00002146 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002147 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00002148 return;
Mike Stump11289f42009-09-09 15:08:12 +00002149
Chris Lattnerf64b3522008-03-09 01:54:53 +00002150 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002151 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00002152
Chris Lattnerf64b3522008-03-09 01:54:53 +00002153 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002154 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
Craig Topperd2d442c2014-05-17 23:10:59 +00002155 const MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002156
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002157 // If the callbacks want to know, tell them about the macro #undef.
2158 // Note: no matter if the macro was defined or not.
2159 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002160 Callbacks->MacroUndefined(MacroNameTok, MD);
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002161
Chris Lattnerf64b3522008-03-09 01:54:53 +00002162 // If the macro is not defined, this is a noop undef, just return.
Craig Topperd2d442c2014-05-17 23:10:59 +00002163 if (!MI)
2164 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002165
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00002166 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00002167 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00002168
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002169 if (MI->isWarnIfUnused())
2170 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2171
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002172 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2173 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
Chris Lattnerf64b3522008-03-09 01:54:53 +00002174}
2175
2176
2177//===----------------------------------------------------------------------===//
2178// Preprocessor Conditional Directive Handling.
2179//===----------------------------------------------------------------------===//
2180
James Dennettf6333ac2012-06-22 05:46:07 +00002181/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2182/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2183/// true if any tokens have been returned or pp-directives activated before this
2184/// \#ifndef has been lexed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002185///
2186void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2187 bool ReadAnyTokensBeforeDirective) {
2188 ++NumIf;
2189 Token DirectiveTok = Result;
2190
2191 Token MacroNameTok;
2192 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00002193
Chris Lattnerf64b3522008-03-09 01:54:53 +00002194 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002195 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002196 // Skip code until we get to #endif. This helps with recovery by not
2197 // emitting an error when the #endif is reached.
2198 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2199 /*Foundnonskip*/false, /*FoundElse*/false);
2200 return;
2201 }
Mike Stump11289f42009-09-09 15:08:12 +00002202
Chris Lattnerf64b3522008-03-09 01:54:53 +00002203 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002204 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00002205
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002206 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002207 MacroDirective *MD = getMacroDirective(MII);
Craig Topperd2d442c2014-05-17 23:10:59 +00002208 MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002209
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002210 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002211 // If the start of a top-level #ifdef and if the macro is not defined,
2212 // inform MIOpt that this might be the start of a proper include guard.
2213 // Otherwise it is some other form of unknown conditional which we can't
2214 // handle.
Craig Topperd2d442c2014-05-17 23:10:59 +00002215 if (!ReadAnyTokensBeforeDirective && !MI) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002216 assert(isIfndef && "#ifdef shouldn't reach here");
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002217 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002218 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002219 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002220 }
2221
Chris Lattnerf64b3522008-03-09 01:54:53 +00002222 // If there is a macro, process it.
2223 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002224 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002225
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002226 if (Callbacks) {
2227 if (isIfndef)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002228 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002229 else
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002230 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002231 }
2232
Chris Lattnerf64b3522008-03-09 01:54:53 +00002233 // Should we include the stuff contained by this directive?
2234 if (!MI == isIfndef) {
2235 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00002236 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2237 /*wasskip*/false, /*foundnonskip*/true,
2238 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002239 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002240 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002241 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00002242 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002243 /*FoundElse*/false);
2244 }
2245}
2246
James Dennettf6333ac2012-06-22 05:46:07 +00002247/// HandleIfDirective - Implements the \#if directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002248///
2249void Preprocessor::HandleIfDirective(Token &IfToken,
2250 bool ReadAnyTokensBeforeDirective) {
2251 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00002252
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002253 // Parse and evaluate the conditional expression.
Craig Topperd2d442c2014-05-17 23:10:59 +00002254 IdentifierInfo *IfNDefMacro = nullptr;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002255 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2256 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2257 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00002258
2259 // If this condition is equivalent to #ifndef X, and if this is the first
2260 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002261 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002262 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Richard Smith089ee152013-06-16 05:05:39 +00002263 // FIXME: Pass in the location of the macro name, not the 'if' token.
2264 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
Nuno Lopes363212b2008-06-01 18:31:24 +00002265 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002266 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00002267 }
2268
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002269 if (Callbacks)
2270 Callbacks->If(IfToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002271 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002272 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002273
Chris Lattnerf64b3522008-03-09 01:54:53 +00002274 // Should we include the stuff contained by this directive?
2275 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002276 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002277 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002278 /*foundnonskip*/true, /*foundelse*/false);
2279 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002280 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00002281 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002282 /*FoundElse*/false);
2283 }
2284}
2285
James Dennettf6333ac2012-06-22 05:46:07 +00002286/// HandleEndifDirective - Implements the \#endif directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002287///
2288void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2289 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattnerf64b3522008-03-09 01:54:53 +00002291 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002292 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00002293
Chris Lattnerf64b3522008-03-09 01:54:53 +00002294 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002295 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002296 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00002297 Diag(EndifToken, diag::err_pp_endif_without_if);
2298 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002299 }
Mike Stump11289f42009-09-09 15:08:12 +00002300
Chris Lattnerf64b3522008-03-09 01:54:53 +00002301 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002302 if (CurPPLexer->getConditionalStackDepth() == 0)
2303 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002304
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002305 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002306 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002307
2308 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002309 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002310}
2311
James Dennettf6333ac2012-06-22 05:46:07 +00002312/// HandleElseDirective - Implements the \#else directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002313///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002314void Preprocessor::HandleElseDirective(Token &Result) {
2315 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002316
Chris Lattnerf64b3522008-03-09 01:54:53 +00002317 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002318 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002319
Chris Lattnerf64b3522008-03-09 01:54:53 +00002320 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002321 if (CurPPLexer->popConditionalLevel(CI)) {
2322 Diag(Result, diag::pp_err_else_without_if);
2323 return;
2324 }
Mike Stump11289f42009-09-09 15:08:12 +00002325
Chris Lattnerf64b3522008-03-09 01:54:53 +00002326 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002327 if (CurPPLexer->getConditionalStackDepth() == 0)
2328 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002329
2330 // If this is a #else with a #else before it, report the error.
2331 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002332
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002333 if (Callbacks)
2334 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2335
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002336 // Finally, skip the rest of the contents of this block.
2337 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002338 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002339}
2340
James Dennettf6333ac2012-06-22 05:46:07 +00002341/// HandleElifDirective - Implements the \#elif directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002342///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002343void Preprocessor::HandleElifDirective(Token &ElifToken) {
2344 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002345
Chris Lattnerf64b3522008-03-09 01:54:53 +00002346 // #elif directive in a non-skipping conditional... start skipping.
2347 // We don't care what the condition is, because we will always skip it (since
2348 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002349 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002350 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002351 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002352
2353 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002354 if (CurPPLexer->popConditionalLevel(CI)) {
2355 Diag(ElifToken, diag::pp_err_elif_without_if);
2356 return;
2357 }
Mike Stump11289f42009-09-09 15:08:12 +00002358
Chris Lattnerf64b3522008-03-09 01:54:53 +00002359 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002360 if (CurPPLexer->getConditionalStackDepth() == 0)
2361 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002362
Chris Lattnerf64b3522008-03-09 01:54:53 +00002363 // If this is a #elif with a #else before it, report the error.
2364 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002365
2366 if (Callbacks)
2367 Callbacks->Elif(ElifToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002368 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002369 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002370
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002371 // Finally, skip the rest of the contents of this block.
2372 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002373 /*FoundElse*/CI.FoundElse,
2374 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002375}