blob: b3d766a759caec76e6fbe913fefafba52e3af9d6 [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;
48 MIChain->Prev = 0;
49 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;
114 MIChainHead->Prev = 0;
115 }
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
James Dennettf6333ac2012-06-22 05:46:07 +0000132/// \brief Lex and validate a macro name, which occurs after a
133/// \#define or \#undef.
134///
135/// This sets the token kind to eod and discards the rest
136/// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
137/// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
138/// else (e.g. \#ifdef).
Chris Lattnerf64b3522008-03-09 01:54:53 +0000139void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
140 // Read the token, don't allow macro expansion on it.
141 LexUnexpandedToken(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Douglas Gregor12785102010-08-24 20:21:13 +0000143 if (MacroNameTok.is(tok::code_completion)) {
144 if (CodeComplete)
145 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000146 setCodeCompletionReached();
Douglas Gregor12785102010-08-24 20:21:13 +0000147 LexUnexpandedToken(MacroNameTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000148 }
149
Chris Lattnerf64b3522008-03-09 01:54:53 +0000150 // Missing macro name?
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000151 if (MacroNameTok.is(tok::eod)) {
Chris Lattner907dfe92008-11-18 07:59:24 +0000152 Diag(MacroNameTok, diag::err_pp_missing_macro_name);
153 return;
154 }
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnerf64b3522008-03-09 01:54:53 +0000156 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
157 if (II == 0) {
Douglas Gregordc970f02010-03-16 22:30:13 +0000158 bool Invalid = false;
159 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
160 if (Invalid)
161 return;
Nico Weber2e686202012-02-29 22:54:43 +0000162
Chris Lattner77c76ae2008-12-13 20:12:40 +0000163 const IdentifierInfo &Info = Identifiers.get(Spelling);
Nico Weber2e686202012-02-29 22:54:43 +0000164
165 // Allow #defining |and| and friends in microsoft mode.
Alp Tokerbfa39342014-01-14 12:51:41 +0000166 if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MSVCCompat) {
Nico Weber2e686202012-02-29 22:54:43 +0000167 MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling));
168 return;
169 }
170
Chris Lattner77c76ae2008-12-13 20:12:40 +0000171 if (Info.isCPlusPlusOperatorKeyword())
Chris Lattnerf64b3522008-03-09 01:54:53 +0000172 // C++ 2.5p2: Alternative tokens behave the same as its primary token
173 // except for their spellings.
Chris Lattner97b8e842008-11-18 08:02:48 +0000174 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000175 else
176 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
177 // Fall through on error.
178 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
Richard Smith7b242542013-03-06 00:46:00 +0000179 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000180 Diag(MacroNameTok, diag::err_defined_macro_name);
Richard Smith7b242542013-03-06 00:46:00 +0000181 } else if (isDefineUndef == 2 && II->hasMacroDefinition() &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000182 getMacroInfo(II)->isBuiltinMacro()) {
Richard Smith7b242542013-03-06 00:46:00 +0000183 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
184 // and C++ [cpp.predefined]p4], but allow it as an extension.
185 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
186 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000187 } else {
188 // Okay, we got a good identifier node. Return it.
189 return;
190 }
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattnerf64b3522008-03-09 01:54:53 +0000192 // Invalid macro name, read and discard the rest of the line. Then set the
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000193 // token kind to tok::eod.
194 MacroNameTok.setKind(tok::eod);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000195 return DiscardUntilEndOfDirective();
196}
197
James Dennettf6333ac2012-06-22 05:46:07 +0000198/// \brief Ensure that the next token is a tok::eod token.
199///
200/// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
Chris Lattner0003c272009-04-17 23:30:53 +0000201/// true, then we consider macros that expand to zero tokens as being ok.
202void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000203 Token Tmp;
Chris Lattner0003c272009-04-17 23:30:53 +0000204 // Lex unexpanded tokens for most directives: macros might expand to zero
205 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
206 // #line) allow empty macros.
207 if (EnableMacros)
208 Lex(Tmp);
209 else
210 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattnerf64b3522008-03-09 01:54:53 +0000212 // There should be no tokens after the directive, but we allow them as an
213 // extension.
214 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
215 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000216
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000217 if (Tmp.isNot(tok::eod)) {
Chris Lattner825676a2009-04-14 05:15:20 +0000218 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000219 // or if this is a macro-style preprocessing directive, because it is more
220 // trouble than it is worth to insert /**/ and check that there is no /**/
221 // in the range also.
Douglas Gregora771f462010-03-31 17:46:05 +0000222 FixItHint Hint;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000223 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000224 !CurTokenLexer)
Douglas Gregora771f462010-03-31 17:46:05 +0000225 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
226 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000227 DiscardUntilEndOfDirective();
228 }
229}
230
231
232
James Dennettf6333ac2012-06-22 05:46:07 +0000233/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
234/// decided that the subsequent tokens are in the \#if'd out portion of the
235/// file. Lex the rest of the file, until we see an \#endif. If
Chris Lattnerf64b3522008-03-09 01:54:53 +0000236/// FoundNonSkipPortion is true, then we have already emitted code for part of
James Dennettf6333ac2012-06-22 05:46:07 +0000237/// this \#if directive, so \#else/\#elif blocks should never be entered.
238/// If ElseOk is true, then \#else directives are ok, if not, then we have
239/// already seen one so a \#else directive is a duplicate. When this returns,
240/// the caller can lex the first valid token.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000241void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
242 bool FoundNonSkipPortion,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000243 bool FoundElse,
244 SourceLocation ElseLoc) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000245 ++NumSkipped;
David Blaikie7d170102013-05-15 07:37:26 +0000246 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000247
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000248 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000249 FoundNonSkipPortion, FoundElse);
Mike Stump11289f42009-09-09 15:08:12 +0000250
Ted Kremenek56572ab2008-12-12 18:34:08 +0000251 if (CurPTHLexer) {
252 PTHSkipExcludedConditionalBlock();
253 return;
254 }
Mike Stump11289f42009-09-09 15:08:12 +0000255
Chris Lattnerf64b3522008-03-09 01:54:53 +0000256 // Enter raw mode to disable identifier lookup (and thus macro expansion),
257 // disabling warnings, etc.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000258 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000259 Token Tok;
260 while (1) {
Chris Lattnerf406b242010-01-18 22:33:01 +0000261 CurLexer->Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000263 if (Tok.is(tok::code_completion)) {
264 if (CodeComplete)
265 CodeComplete->CodeCompleteInConditionalExclusion();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000266 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000267 continue;
268 }
269
Chris Lattnerf64b3522008-03-09 01:54:53 +0000270 // If this is the end of the buffer, we have an error.
271 if (Tok.is(tok::eof)) {
272 // Emit errors for each unterminated conditional on the stack, including
273 // the current one.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000274 while (!CurPPLexer->ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000275 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000276 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
277 diag::err_pp_unterminated_conditional);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000278 CurPPLexer->ConditionalStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000279 }
280
Chris Lattnerf64b3522008-03-09 01:54:53 +0000281 // Just return and let the caller lex after this #include.
282 break;
283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattnerf64b3522008-03-09 01:54:53 +0000285 // If this token is not a preprocessor directive, just skip it.
286 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
287 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerf64b3522008-03-09 01:54:53 +0000289 // We just parsed a # character at the start of a line, so we're in
290 // directive mode. Tell the lexer this so any newlines we see will be
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000291 // converted into an EOD token (this terminates the macro).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000292 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rose176057b2013-02-22 00:32:00 +0000293 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000294
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattnerf64b3522008-03-09 01:54:53 +0000296 // Read the next token, the directive flavor.
297 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerf64b3522008-03-09 01:54:53 +0000299 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
300 // something bogus), skip it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000301 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000302 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000303 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000304 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000305 continue;
306 }
307
308 // If the first letter isn't i or e, it isn't intesting to us. We know that
309 // this is safe in the face of spelling differences, because there is no way
310 // to spell an i/e in a strange way that is another letter. Skipping this
311 // allows us to avoid looking up the identifier info for #define/#undef and
312 // other common directives.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000313 const char *RawCharData = Tok.getRawIdentifierData();
314
Chris Lattnerf64b3522008-03-09 01:54:53 +0000315 char FirstChar = RawCharData[0];
Mike Stump11289f42009-09-09 15:08:12 +0000316 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000317 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000318 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000319 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000320 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000321 continue;
322 }
Mike Stump11289f42009-09-09 15:08:12 +0000323
Chris Lattnerf64b3522008-03-09 01:54:53 +0000324 // Get the identifier name without trigraphs or embedded newlines. Note
325 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
326 // when skipping.
Benjamin Kramer144884642009-12-31 13:32:38 +0000327 char DirectiveBuf[20];
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000328 StringRef Directive;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000329 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000330 Directive = StringRef(RawCharData, Tok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +0000331 } else {
332 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramer144884642009-12-31 13:32:38 +0000333 unsigned IdLen = DirectiveStr.size();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000334 if (IdLen >= 20) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000335 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000336 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000337 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000338 continue;
339 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000340 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000341 Directive = StringRef(DirectiveBuf, IdLen);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Benjamin Kramer144884642009-12-31 13:32:38 +0000344 if (Directive.startswith("if")) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000345 StringRef Sub = Directive.substr(2);
Benjamin Kramer144884642009-12-31 13:32:38 +0000346 if (Sub.empty() || // "if"
347 Sub == "def" || // "ifdef"
348 Sub == "ndef") { // "ifndef"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000349 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
350 // bother parsing the condition.
351 DiscardUntilEndOfDirective();
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000352 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000353 /*foundnonskip*/false,
Chandler Carruth540960f2011-01-03 17:40:17 +0000354 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000355 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000356 } else if (Directive[0] == 'e') {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000357 StringRef Sub = Directive.substr(1);
Benjamin Kramer144884642009-12-31 13:32:38 +0000358 if (Sub == "ndif") { // "endif"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000359 PPConditionalInfo CondInfo;
360 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000361 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000362 (void)InCond; // Silence warning in no-asserts mode.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000363 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattnerf64b3522008-03-09 01:54:53 +0000365 // If we popped the outermost skipping block, we're done skipping!
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000366 if (!CondInfo.WasSkipping) {
Richard Smith87d8fb92012-06-24 23:56:26 +0000367 // Restore the value of LexingRawMode so that trailing comments
368 // are handled correctly, if we've reached the outermost block.
369 CurPPLexer->LexingRawMode = false;
Richard Smithd0124572012-06-21 00:35:03 +0000370 CheckEndOfDirective("endif");
Richard Smith87d8fb92012-06-24 23:56:26 +0000371 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000372 if (Callbacks)
373 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000374 break;
Richard Smithd0124572012-06-21 00:35:03 +0000375 } else {
376 DiscardUntilEndOfDirective();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000377 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000378 } else if (Sub == "lse") { // "else".
Chris Lattnerf64b3522008-03-09 01:54:53 +0000379 // #else directive in a skipping conditional. If not in some other
380 // skipping conditional, and if #else hasn't already been seen, enter it
381 // as a non-skipping conditional.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000382 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump11289f42009-09-09 15:08:12 +0000383
Chris Lattnerf64b3522008-03-09 01:54:53 +0000384 // If this is a #else with a #else before it, report the error.
385 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattnerf64b3522008-03-09 01:54:53 +0000387 // Note that we've seen a #else in this conditional.
388 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattnerf64b3522008-03-09 01:54:53 +0000390 // If the conditional is at the top level, and the #if block wasn't
391 // entered, enter the #else block now.
392 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
393 CondInfo.FoundNonSkip = true;
Richard Smith87d8fb92012-06-24 23:56:26 +0000394 // Restore the value of LexingRawMode so that trailing comments
395 // are handled correctly.
396 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000397 CheckEndOfDirective("else");
Richard Smith87d8fb92012-06-24 23:56:26 +0000398 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000399 if (Callbacks)
400 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000401 break;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000402 } else {
403 DiscardUntilEndOfDirective(); // C99 6.10p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000404 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000405 } else if (Sub == "lif") { // "elif".
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000406 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000407
John Thompson17c35732013-12-04 20:19:30 +0000408 // If this is a #elif with a #else before it, report the error.
409 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
410
Chris Lattnerf64b3522008-03-09 01:54:53 +0000411 // If this is in a skipping block or if we're already handled this #if
412 // block, don't bother parsing the condition.
413 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
414 DiscardUntilEndOfDirective();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000415 } else {
John Thompson17c35732013-12-04 20:19:30 +0000416 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000417 // Restore the value of LexingRawMode so that identifiers are
418 // looked up, etc, inside the #elif expression.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000419 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
420 CurPPLexer->LexingRawMode = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000421 IdentifierInfo *IfNDefMacro = 0;
John Thompson17c35732013-12-04 20:19:30 +0000422 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000423 CurPPLexer->LexingRawMode = true;
John Thompson17c35732013-12-04 20:19:30 +0000424 if (Callbacks) {
425 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000426 Callbacks->Elif(Tok.getLocation(),
John Thompson17c35732013-12-04 20:19:30 +0000427 SourceRange(CondBegin, CondEnd),
John Thompson87f9fef2013-12-07 08:41:15 +0000428 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
John Thompson17c35732013-12-04 20:19:30 +0000429 }
430 // If this condition is true, enter it!
431 if (CondValue) {
432 CondInfo.FoundNonSkip = true;
433 break;
434 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000435 }
436 }
437 }
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000439 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000440 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000441 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000442 }
443
444 // Finally, if we are out of the conditional (saw an #endif or ran off the end
445 // of the file, just stop skipping and return to lexing whatever came after
446 // the #if block.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000447 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000448
449 if (Callbacks) {
450 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
451 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
452 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000453}
454
Ted Kremenek56572ab2008-12-12 18:34:08 +0000455void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump11289f42009-09-09 15:08:12 +0000456
457 while (1) {
Ted Kremenek56572ab2008-12-12 18:34:08 +0000458 assert(CurPTHLexer);
459 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek56572ab2008-12-12 18:34:08 +0000461 // Skip to the next '#else', '#elif', or #endif.
462 if (CurPTHLexer->SkipBlock()) {
463 // We have reached an #endif. Both the '#' and 'endif' tokens
464 // have been consumed by the PTHLexer. Just pop off the condition level.
465 PPConditionalInfo CondInfo;
466 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000467 (void)InCond; // Silence warning in no-asserts mode.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000468 assert(!InCond && "Can't be skipping if not in a conditional!");
469 break;
470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Ted Kremenek56572ab2008-12-12 18:34:08 +0000472 // We have reached a '#else' or '#elif'. Lex the next token to get
473 // the directive flavor.
474 Token Tok;
475 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000476
Ted Kremenek56572ab2008-12-12 18:34:08 +0000477 // We can actually look up the IdentifierInfo here since we aren't in
478 // raw mode.
479 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
480
481 if (K == tok::pp_else) {
482 // #else: Enter the else condition. We aren't in a nested condition
483 // since we skip those. We're always in the one matching the last
484 // blocked we skipped.
485 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
486 // Note that we've seen a #else in this conditional.
487 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000488
Ted Kremenek56572ab2008-12-12 18:34:08 +0000489 // If the #if block wasn't entered then enter the #else block now.
490 if (!CondInfo.FoundNonSkip) {
491 CondInfo.FoundNonSkip = true;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000493 // Scan until the eod token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000494 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar2cba6be2009-04-13 17:57:49 +0000495 DiscardUntilEndOfDirective();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000496 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000497
Ted Kremenek56572ab2008-12-12 18:34:08 +0000498 break;
499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremenek56572ab2008-12-12 18:34:08 +0000501 // Otherwise skip this block.
502 continue;
503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek56572ab2008-12-12 18:34:08 +0000505 assert(K == tok::pp_elif);
506 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
507
508 // If this is a #elif with a #else before it, report the error.
509 if (CondInfo.FoundElse)
510 Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Ted Kremenek56572ab2008-12-12 18:34:08 +0000512 // If this is in a skipping block or if we're already handled this #if
Mike Stump11289f42009-09-09 15:08:12 +0000513 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000514 if (CondInfo.FoundNonSkip)
515 continue;
516
517 // Evaluate the condition of the #elif.
518 IdentifierInfo *IfNDefMacro = 0;
519 CurPTHLexer->ParsingPreprocessorDirective = true;
520 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
521 CurPTHLexer->ParsingPreprocessorDirective = false;
522
523 // If this condition is true, enter it!
524 if (ShouldEnter) {
525 CondInfo.FoundNonSkip = true;
526 break;
527 }
528
529 // Otherwise, skip this block and go to the next one.
530 continue;
531 }
532}
533
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000534Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) {
535 ModuleMap &ModMap = HeaderInfo.getModuleMap();
536 if (SourceMgr.isInMainFile(FilenameLoc)) {
537 if (Module *CurMod = getCurrentModule())
538 return CurMod; // Compiling a module.
539 return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
540 }
541 // Try to determine the module of the include directive.
Daniel Jasper88d86952013-12-03 20:30:36 +0000542 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
543 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getSpellingLoc(FilenameLoc));
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000544 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
545 // The include comes from a file.
546 return ModMap.findModuleForHeader(EntryOfIncl).getModule();
547 } else {
548 // The include does not come from a file,
549 // so it is probably a module compilation.
550 return getCurrentModule();
551 }
552}
553
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000554const FileEntry *Preprocessor::LookupFile(
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000555 SourceLocation FilenameLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000556 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000557 bool isAngled,
558 const DirectoryLookup *FromDir,
559 const DirectoryLookup *&CurDir,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000560 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000561 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000562 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000563 bool SkipCache) {
Will Wilson0fafd342013-12-27 19:46:16 +0000564 // If the header lookup mechanism may be relative to the current inclusion
565 // stack, record the parent #includes.
566 SmallVector<const FileEntry *, 16> Includers;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000567 if (!FromDir) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000568 FileID FID = getCurrentFileLexer()->getFileID();
Will Wilson0fafd342013-12-27 19:46:16 +0000569 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000570
Chris Lattner022923a2009-02-04 19:45:07 +0000571 // If there is no file entry associated with this file, it must be the
572 // predefines buffer. Any other file is not lexed with a normal lexer, so
Douglas Gregor618e64a2010-08-08 07:49:23 +0000573 // it won't be scanned for preprocessor directives. If we have the
574 // predefines buffer, resolve #include references (which come from the
575 // -include command line argument) as if they came from the main file, this
576 // affects file lookup etc.
Will Wilson0fafd342013-12-27 19:46:16 +0000577 if (!FileEnt)
578 FileEnt = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
579
580 if (FileEnt)
581 Includers.push_back(FileEnt);
582
583 // MSVC searches the current include stack from top to bottom for
584 // headers included by quoted include directives.
585 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
Alp Tokerbfa39342014-01-14 12:51:41 +0000586 if (LangOpts.MSVCCompat && !isAngled) {
Will Wilson0fafd342013-12-27 19:46:16 +0000587 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
588 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
589 if (IsFileLexer(ISEntry))
590 if ((FileEnt = SourceMgr.getFileEntryForID(
591 ISEntry.ThePPLexer->getFileID())))
592 Includers.push_back(FileEnt);
593 }
Chris Lattner022923a2009-02-04 19:45:07 +0000594 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000597 // Do a standard file entry lookup.
598 CurDir = CurDirLookup;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000599 const FileEntry *FE = HeaderInfo.LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000600 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
601 RelativePath, SuggestedModule, SkipCache);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000602 if (FE) {
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000603 if (SuggestedModule)
Daniel Jasper92669ee2013-12-20 12:09:36 +0000604 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
605 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000606 return FE;
607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Will Wilson0fafd342013-12-27 19:46:16 +0000609 const FileEntry *CurFileEnt;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000610 // Otherwise, see if this is a subframework header. If so, this is relative
611 // to one of the headers on the #include stack. Walk the list of the current
612 // headers on the #include stack and pass them to HeaderInfo.
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000613 if (IsFileLexer()) {
Ted Kremenek45245212008-11-19 21:57:25 +0000614 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000615 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000616 SearchPath, RelativePath,
617 SuggestedModule)))
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000618 return FE;
619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000621 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
622 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000623 if (IsFileLexer(ISEntry)) {
Mike Stump11289f42009-09-09 15:08:12 +0000624 if ((CurFileEnt =
Ted Kremenek45245212008-11-19 21:57:25 +0000625 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000626 if ((FE = HeaderInfo.LookupSubframeworkHeader(
Douglas Gregorf5f94522013-02-08 00:10:48 +0000627 Filename, CurFileEnt, SearchPath, RelativePath,
628 SuggestedModule)))
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000629 return FE;
630 }
631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000633 // Otherwise, we really couldn't find the file.
634 return 0;
635}
636
Chris Lattnerf64b3522008-03-09 01:54:53 +0000637
638//===----------------------------------------------------------------------===//
639// Preprocessor Directive Handling.
640//===----------------------------------------------------------------------===//
641
David Blaikied5321242012-06-06 18:52:13 +0000642class Preprocessor::ResetMacroExpansionHelper {
643public:
644 ResetMacroExpansionHelper(Preprocessor *pp)
645 : PP(pp), save(pp->DisableMacroExpansion) {
646 if (pp->MacroExpansionInDirectivesOverride)
647 pp->DisableMacroExpansion = false;
648 }
649 ~ResetMacroExpansionHelper() {
650 PP->DisableMacroExpansion = save;
651 }
652private:
653 Preprocessor *PP;
654 bool save;
655};
656
Chris Lattnerf64b3522008-03-09 01:54:53 +0000657/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump11289f42009-09-09 15:08:12 +0000658/// at the start of a line. This consumes the directive, modifies the
Chris Lattnerf64b3522008-03-09 01:54:53 +0000659/// lexer/preprocessor state, and advances the lexer(s) so that the next token
660/// read is the correct one.
661void Preprocessor::HandleDirective(Token &Result) {
662 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattnerf64b3522008-03-09 01:54:53 +0000664 // We just parsed a # character at the start of a line, so we're in directive
665 // mode. Tell the lexer this so any newlines we see will be converted into an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000666 // EOD token (which terminates the directive).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000667 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000668 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000670 bool ImmediatelyAfterTopLevelIfndef =
671 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
672 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
673
Chris Lattnerf64b3522008-03-09 01:54:53 +0000674 ++NumDirectives;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000675
Chris Lattnerf64b3522008-03-09 01:54:53 +0000676 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump11289f42009-09-09 15:08:12 +0000677 // work, we have to remember if we had read any tokens *before* this
Chris Lattnerf64b3522008-03-09 01:54:53 +0000678 // pp-directive.
Chris Lattner8cf1f932009-12-14 04:54:40 +0000679 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner2d17ab72009-03-18 21:00:25 +0000681 // Save the '#' token in case we need to return it later.
682 Token SavedHash = Result;
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattnerf64b3522008-03-09 01:54:53 +0000684 // Read the next token, the directive flavor. This isn't expanded due to
685 // C99 6.10.3p8.
686 LexUnexpandedToken(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattnerf64b3522008-03-09 01:54:53 +0000688 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
689 // #define A(x) #x
690 // A(abc
691 // #warning blah
692 // def)
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000693 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
694 // not support this for #include-like directives, since that can result in
695 // terrible diagnostics, and does not work in GCC.
696 if (InMacroArgs) {
697 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
698 switch (II->getPPKeywordID()) {
699 case tok::pp_include:
700 case tok::pp_import:
701 case tok::pp_include_next:
702 case tok::pp___include_macros:
703 Diag(Result, diag::err_embedded_include) << II->getName();
704 DiscardUntilEndOfDirective();
705 return;
706 default:
707 break;
708 }
709 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000710 Diag(Result, diag::ext_embedded_directive);
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
David Blaikied5321242012-06-06 18:52:13 +0000713 // Temporarily enable macro expansion if set so
714 // and reset to previous state when returning from this function.
715 ResetMacroExpansionHelper helper(this);
716
Chris Lattnerf64b3522008-03-09 01:54:53 +0000717 switch (Result.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000718 case tok::eod:
Chris Lattnerf64b3522008-03-09 01:54:53 +0000719 return; // null directive.
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000720 case tok::code_completion:
721 if (CodeComplete)
722 CodeComplete->CodeCompleteDirective(
723 CurPPLexer->getConditionalStackDepth() > 0);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000724 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000725 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000726 case tok::numeric_constant: // # 7 GNU line marker directive.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000727 if (getLangOpts().AsmPreprocessor)
Chris Lattner5eb8ae22009-03-18 20:41:10 +0000728 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner76e68962009-01-26 06:19:46 +0000729 return HandleDigitDirective(Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000730 default:
731 IdentifierInfo *II = Result.getIdentifierInfo();
732 if (II == 0) break; // Not an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerf64b3522008-03-09 01:54:53 +0000734 // Ask what the preprocessor keyword ID is.
735 switch (II->getPPKeywordID()) {
736 default: break;
737 // C99 6.10.1 - Conditional Inclusion.
738 case tok::pp_if:
739 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
740 case tok::pp_ifdef:
741 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
742 case tok::pp_ifndef:
743 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
744 case tok::pp_elif:
745 return HandleElifDirective(Result);
746 case tok::pp_else:
747 return HandleElseDirective(Result);
748 case tok::pp_endif:
749 return HandleEndifDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerf64b3522008-03-09 01:54:53 +0000751 // C99 6.10.2 - Source File Inclusion.
752 case tok::pp_include:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000753 // Handle #include.
754 return HandleIncludeDirective(SavedHash.getLocation(), Result);
Chris Lattner14a7f392009-04-08 18:24:34 +0000755 case tok::pp___include_macros:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000756 // Handle -imacros.
757 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000758
Chris Lattnerf64b3522008-03-09 01:54:53 +0000759 // C99 6.10.3 - Macro Replacement.
760 case tok::pp_define:
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000761 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000762 case tok::pp_undef:
763 return HandleUndefDirective(Result);
764
765 // C99 6.10.4 - Line Control.
766 case tok::pp_line:
Chris Lattner100c65e2009-01-26 05:29:08 +0000767 return HandleLineDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerf64b3522008-03-09 01:54:53 +0000769 // C99 6.10.5 - Error Directive.
770 case tok::pp_error:
771 return HandleUserDiagnosticDirective(Result, false);
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattnerf64b3522008-03-09 01:54:53 +0000773 // C99 6.10.6 - Pragma Directive.
774 case tok::pp_pragma:
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000775 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnerf64b3522008-03-09 01:54:53 +0000777 // GNU Extensions.
778 case tok::pp_import:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000779 return HandleImportDirective(SavedHash.getLocation(), Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000780 case tok::pp_include_next:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000781 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnerf64b3522008-03-09 01:54:53 +0000783 case tok::pp_warning:
784 Diag(Result, diag::ext_pp_warning_directive);
785 return HandleUserDiagnosticDirective(Result, true);
786 case tok::pp_ident:
787 return HandleIdentSCCSDirective(Result);
788 case tok::pp_sccs:
789 return HandleIdentSCCSDirective(Result);
790 case tok::pp_assert:
791 //isExtension = true; // FIXME: implement #assert
792 break;
793 case tok::pp_unassert:
794 //isExtension = true; // FIXME: implement #unassert
795 break;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +0000796
Douglas Gregor663b48f2012-01-03 19:48:16 +0000797 case tok::pp___public_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000798 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000799 return HandleMacroPublicDirective(Result);
800 break;
801
Douglas Gregor663b48f2012-01-03 19:48:16 +0000802 case tok::pp___private_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000803 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000804 return HandleMacroPrivateDirective(Result);
805 break;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000806 }
807 break;
808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner2d17ab72009-03-18 21:00:25 +0000810 // If this is a .S file, treat unknown # directives as non-preprocessor
811 // directives. This is important because # may be a comment or introduce
812 // various pseudo-ops. Just return the # token and push back the following
813 // token to be lexed next time.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000814 if (getLangOpts().AsmPreprocessor) {
Daniel Dunbar48b4d1e2009-07-13 21:48:50 +0000815 Token *Toks = new Token[2];
Chris Lattner2d17ab72009-03-18 21:00:25 +0000816 // Return the # and the token after it.
Mike Stump11289f42009-09-09 15:08:12 +0000817 Toks[0] = SavedHash;
Chris Lattner2d17ab72009-03-18 21:00:25 +0000818 Toks[1] = Result;
Chris Lattner56f64c12011-01-06 05:01:51 +0000819
820 // If the second token is a hashhash token, then we need to translate it to
821 // unknown so the token lexer doesn't try to perform token pasting.
822 if (Result.is(tok::hashhash))
823 Toks[1].setKind(tok::unknown);
824
Chris Lattner2d17ab72009-03-18 21:00:25 +0000825 // Enter this token stream so that we re-lex the tokens. Make sure to
826 // enable macro expansion, in case the token after the # is an identifier
827 // that is expanded.
828 EnterTokenStream(Toks, 2, false, true);
829 return;
830 }
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattnerf64b3522008-03-09 01:54:53 +0000832 // If we reached here, the preprocessing token is not valid!
833 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattnerf64b3522008-03-09 01:54:53 +0000835 // Read the rest of the PP line.
836 DiscardUntilEndOfDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattnerf64b3522008-03-09 01:54:53 +0000838 // Okay, we're done parsing the directive.
839}
840
Chris Lattner76e68962009-01-26 06:19:46 +0000841/// GetLineValue - Convert a numeric token into an unsigned value, emitting
842/// Diagnostic DiagID if it is invalid, and returning the value in Val.
843static bool GetLineValue(Token &DigitTok, unsigned &Val,
Michael Ilsemane910cc82013-04-10 01:04:18 +0000844 unsigned DiagID, Preprocessor &PP,
845 bool IsGNULineDirective=false) {
Chris Lattner76e68962009-01-26 06:19:46 +0000846 if (DigitTok.isNot(tok::numeric_constant)) {
847 PP.Diag(DigitTok, DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000848
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000849 if (DigitTok.isNot(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000850 PP.DiscardUntilEndOfDirective();
851 return true;
852 }
Mike Stump11289f42009-09-09 15:08:12 +0000853
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000854 SmallString<64> IntegerBuffer;
Chris Lattner76e68962009-01-26 06:19:46 +0000855 IntegerBuffer.resize(DigitTok.getLength());
856 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregordc970f02010-03-16 22:30:13 +0000857 bool Invalid = false;
858 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
859 if (Invalid)
860 return true;
861
Chris Lattnerd66f1722009-04-18 18:35:15 +0000862 // Verify that we have a simple digit-sequence, and compute the value. This
863 // is always a simple digit string computed in decimal, so we do this manually
864 // here.
865 Val = 0;
866 for (unsigned i = 0; i != ActualLength; ++i) {
Richard Smith7f2707a2013-09-26 18:13:20 +0000867 // C++1y [lex.fcon]p1:
868 // Optional separating single quotes in a digit-sequence are ignored
869 if (DigitTokBegin[i] == '\'')
870 continue;
871
Jordan Rosea7d03842013-02-08 22:30:41 +0000872 if (!isDigit(DigitTokBegin[i])) {
Chris Lattnerd66f1722009-04-18 18:35:15 +0000873 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
Michael Ilsemane910cc82013-04-10 01:04:18 +0000874 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000875 PP.DiscardUntilEndOfDirective();
876 return true;
877 }
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattnerd66f1722009-04-18 18:35:15 +0000879 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
880 if (NextVal < Val) { // overflow.
881 PP.Diag(DigitTok, DiagID);
882 PP.DiscardUntilEndOfDirective();
883 return true;
884 }
885 Val = NextVal;
Chris Lattner76e68962009-01-26 06:19:46 +0000886 }
Mike Stump11289f42009-09-09 15:08:12 +0000887
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000888 if (DigitTokBegin[0] == '0' && Val)
Michael Ilsemane910cc82013-04-10 01:04:18 +0000889 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
890 << IsGNULineDirective;
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner76e68962009-01-26 06:19:46 +0000892 return false;
893}
894
James Dennettf6333ac2012-06-22 05:46:07 +0000895/// \brief Handle a \#line directive: C99 6.10.4.
896///
897/// The two acceptable forms are:
898/// \verbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000899/// # line digit-sequence
900/// # line digit-sequence "s-char-sequence"
James Dennettf6333ac2012-06-22 05:46:07 +0000901/// \endverbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000902void Preprocessor::HandleLineDirective(Token &Tok) {
903 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
904 // expanded.
905 Token DigitTok;
906 Lex(DigitTok);
907
Chris Lattner100c65e2009-01-26 05:29:08 +0000908 // Validate the number and convert it to an unsigned.
Chris Lattner76e68962009-01-26 06:19:46 +0000909 unsigned LineNo;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000910 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner100c65e2009-01-26 05:29:08 +0000911 return;
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000912
913 if (LineNo == 0)
914 Diag(DigitTok, diag::ext_pp_line_zero);
Chris Lattner100c65e2009-01-26 05:29:08 +0000915
Chris Lattner76e68962009-01-26 06:19:46 +0000916 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
917 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Eli Friedman192e0342011-10-10 23:35:28 +0000918 unsigned LineLimit = 32768U;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000919 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Eli Friedman192e0342011-10-10 23:35:28 +0000920 LineLimit = 2147483648U;
Chris Lattner100c65e2009-01-26 05:29:08 +0000921 if (LineNo >= LineLimit)
922 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000923 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
Richard Smithacd4d3d2011-10-15 01:18:56 +0000924 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000926 int FilenameID = -1;
Chris Lattner100c65e2009-01-26 05:29:08 +0000927 Token StrTok;
928 Lex(StrTok);
929
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000930 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
931 // string followed by eod.
932 if (StrTok.is(tok::eod))
Chris Lattner100c65e2009-01-26 05:29:08 +0000933 ; // ok
934 else if (StrTok.isNot(tok::string_literal)) {
935 Diag(StrTok, diag::err_pp_line_invalid_filename);
Richard Smithd67aea22012-03-06 03:21:47 +0000936 return DiscardUntilEndOfDirective();
937 } else if (StrTok.hasUDSuffix()) {
938 Diag(StrTok, diag::err_invalid_string_udl);
939 return DiscardUntilEndOfDirective();
Chris Lattner100c65e2009-01-26 05:29:08 +0000940 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000941 // Parse and validate the string, converting it into a unique ID.
942 StringLiteralParser Literal(&StrTok, 1, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000943 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000944 if (Literal.hadError)
945 return DiscardUntilEndOfDirective();
946 if (Literal.Pascal) {
947 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
948 return DiscardUntilEndOfDirective();
949 }
Jay Foad9a6b0982011-06-21 15:13:30 +0000950 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +0000951
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000952 // Verify that there is nothing after the string, other than EOD. Because
Chris Lattner0003c272009-04-17 23:30:53 +0000953 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
954 CheckEndOfDirective("line", true);
Chris Lattner100c65e2009-01-26 05:29:08 +0000955 }
Mike Stump11289f42009-09-09 15:08:12 +0000956
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000957 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump11289f42009-09-09 15:08:12 +0000958
Chris Lattner839150e2009-03-27 17:13:49 +0000959 if (Callbacks)
Chris Lattnerc745cec2010-04-14 04:28:50 +0000960 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
961 PPCallbacks::RenameFile,
Chris Lattner839150e2009-03-27 17:13:49 +0000962 SrcMgr::C_User);
Chris Lattner100c65e2009-01-26 05:29:08 +0000963}
964
Chris Lattner76e68962009-01-26 06:19:46 +0000965/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
966/// marker directive.
967static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
968 bool &IsSystemHeader, bool &IsExternCHeader,
969 Preprocessor &PP) {
970 unsigned FlagVal;
971 Token FlagTok;
972 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000973 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000974 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
975 return true;
976
977 if (FlagVal == 1) {
978 IsFileEntry = true;
Mike Stump11289f42009-09-09 15:08:12 +0000979
Chris Lattner76e68962009-01-26 06:19:46 +0000980 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000981 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000982 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
983 return true;
984 } else if (FlagVal == 2) {
985 IsFileExit = true;
Mike Stump11289f42009-09-09 15:08:12 +0000986
Chris Lattner1c967782009-02-04 06:25:26 +0000987 SourceManager &SM = PP.getSourceManager();
988 // If we are leaving the current presumed file, check to make sure the
989 // presumed include stack isn't empty!
990 FileID CurFileID =
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000991 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
Chris Lattner1c967782009-02-04 06:25:26 +0000992 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000993 if (PLoc.isInvalid())
994 return true;
995
Chris Lattner1c967782009-02-04 06:25:26 +0000996 // If there is no include loc (main file) or if the include loc is in a
997 // different physical file, then we aren't in a "1" line marker flag region.
998 SourceLocation IncLoc = PLoc.getIncludeLoc();
999 if (IncLoc.isInvalid() ||
Chandler Carruthc7ca5212011-07-25 20:52:32 +00001000 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
Chris Lattner1c967782009-02-04 06:25:26 +00001001 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1002 PP.DiscardUntilEndOfDirective();
1003 return true;
1004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Chris Lattner76e68962009-01-26 06:19:46 +00001006 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001007 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001008 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1009 return true;
1010 }
1011
1012 // We must have 3 if there are still flags.
1013 if (FlagVal != 3) {
1014 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001015 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001016 return true;
1017 }
Mike Stump11289f42009-09-09 15:08:12 +00001018
Chris Lattner76e68962009-01-26 06:19:46 +00001019 IsSystemHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001020
Chris Lattner76e68962009-01-26 06:19:46 +00001021 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001022 if (FlagTok.is(tok::eod)) return false;
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001023 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner76e68962009-01-26 06:19:46 +00001024 return true;
1025
1026 // We must have 4 if there is yet another flag.
1027 if (FlagVal != 4) {
1028 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001029 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001030 return true;
1031 }
Mike Stump11289f42009-09-09 15:08:12 +00001032
Chris Lattner76e68962009-01-26 06:19:46 +00001033 IsExternCHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001034
Chris Lattner76e68962009-01-26 06:19:46 +00001035 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001036 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001037
1038 // There are no more valid flags here.
1039 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001040 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001041 return true;
1042}
1043
1044/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1045/// one of the following forms:
1046///
1047/// # 42
Mike Stump11289f42009-09-09 15:08:12 +00001048/// # 42 "file" ('1' | '2')?
Chris Lattner76e68962009-01-26 06:19:46 +00001049/// # 42 "file" ('1' | '2')? '3' '4'?
1050///
1051void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1052 // Validate the number and convert it to an unsigned. GNU does not have a
1053 // line # limit other than it fit in 32-bits.
1054 unsigned LineNo;
1055 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
Michael Ilsemane910cc82013-04-10 01:04:18 +00001056 *this, true))
Chris Lattner76e68962009-01-26 06:19:46 +00001057 return;
Mike Stump11289f42009-09-09 15:08:12 +00001058
Chris Lattner76e68962009-01-26 06:19:46 +00001059 Token StrTok;
1060 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001061
Chris Lattner76e68962009-01-26 06:19:46 +00001062 bool IsFileEntry = false, IsFileExit = false;
1063 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001064 int FilenameID = -1;
1065
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001066 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1067 // string followed by eod.
1068 if (StrTok.is(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +00001069 ; // ok
1070 else if (StrTok.isNot(tok::string_literal)) {
1071 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001072 return DiscardUntilEndOfDirective();
Richard Smithd67aea22012-03-06 03:21:47 +00001073 } else if (StrTok.hasUDSuffix()) {
1074 Diag(StrTok, diag::err_invalid_string_udl);
1075 return DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001076 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001077 // Parse and validate the string, converting it into a unique ID.
1078 StringLiteralParser Literal(&StrTok, 1, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001079 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001080 if (Literal.hadError)
1081 return DiscardUntilEndOfDirective();
1082 if (Literal.Pascal) {
1083 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1084 return DiscardUntilEndOfDirective();
1085 }
Jay Foad9a6b0982011-06-21 15:13:30 +00001086 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +00001087
Chris Lattner76e68962009-01-26 06:19:46 +00001088 // If a filename was present, read any flags that are present.
Mike Stump11289f42009-09-09 15:08:12 +00001089 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001090 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner76e68962009-01-26 06:19:46 +00001091 return;
Chris Lattner76e68962009-01-26 06:19:46 +00001092 }
Mike Stump11289f42009-09-09 15:08:12 +00001093
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001094 // Create a line note with this information.
1095 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump11289f42009-09-09 15:08:12 +00001096 IsFileEntry, IsFileExit,
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001097 IsSystemHeader, IsExternCHeader);
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattner839150e2009-03-27 17:13:49 +00001099 // If the preprocessor has callbacks installed, notify them of the #line
1100 // change. This is used so that the line marker comes out in -E mode for
1101 // example.
1102 if (Callbacks) {
1103 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1104 if (IsFileEntry)
1105 Reason = PPCallbacks::EnterFile;
1106 else if (IsFileExit)
1107 Reason = PPCallbacks::ExitFile;
1108 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1109 if (IsExternCHeader)
1110 FileKind = SrcMgr::C_ExternCSystem;
1111 else if (IsSystemHeader)
1112 FileKind = SrcMgr::C_System;
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattnerc745cec2010-04-14 04:28:50 +00001114 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner839150e2009-03-27 17:13:49 +00001115 }
Chris Lattner76e68962009-01-26 06:19:46 +00001116}
1117
1118
Chris Lattner38d7fd22009-01-26 05:30:54 +00001119/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1120///
Mike Stump11289f42009-09-09 15:08:12 +00001121void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001122 bool isWarning) {
Chris Lattner38d7fd22009-01-26 05:30:54 +00001123 // PTH doesn't emit #warning or #error directives.
1124 if (CurPTHLexer)
Chris Lattner100c65e2009-01-26 05:29:08 +00001125 return CurPTHLexer->DiscardToEndOfLine();
1126
Chris Lattnerf64b3522008-03-09 01:54:53 +00001127 // Read the rest of the line raw. We do this because we don't want macros
1128 // to be expanded and we don't require that the tokens be valid preprocessing
1129 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1130 // collapse multiple consequtive white space between tokens, but this isn't
1131 // specified by the standard.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001132 SmallString<128> Message;
1133 CurLexer->ReadToEndOfLine(&Message);
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001134
1135 // Find the first non-whitespace character, so that we can make the
1136 // diagnostic more succinct.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001137 StringRef Msg = Message.str().ltrim(" ");
1138
Chris Lattner100c65e2009-01-26 05:29:08 +00001139 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001140 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001141 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001142 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001143}
1144
1145/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1146///
1147void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1148 // Yes, this directive is an extension.
1149 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattnerf64b3522008-03-09 01:54:53 +00001151 // Read the string argument.
1152 Token StrTok;
1153 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattnerf64b3522008-03-09 01:54:53 +00001155 // If the token kind isn't a string, it's a malformed directive.
1156 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001157 StrTok.isNot(tok::wide_string_literal)) {
1158 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001159 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001160 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001161 return;
1162 }
Mike Stump11289f42009-09-09 15:08:12 +00001163
Richard Smithd67aea22012-03-06 03:21:47 +00001164 if (StrTok.hasUDSuffix()) {
1165 Diag(StrTok, diag::err_invalid_string_udl);
1166 return DiscardUntilEndOfDirective();
1167 }
1168
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001169 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001170 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001171
Douglas Gregordc970f02010-03-16 22:30:13 +00001172 if (Callbacks) {
1173 bool Invalid = false;
1174 std::string Str = getSpelling(StrTok, &Invalid);
1175 if (!Invalid)
1176 Callbacks->Ident(Tok.getLocation(), Str);
1177 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001178}
1179
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001180/// \brief Handle a #public directive.
1181void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001182 Token MacroNameTok;
1183 ReadMacroName(MacroNameTok, 2);
1184
1185 // Error reading macro name? If so, diagnostic already issued.
1186 if (MacroNameTok.is(tok::eod))
1187 return;
1188
Douglas Gregor663b48f2012-01-03 19:48:16 +00001189 // Check to see if this is the last token on the #__public_macro line.
1190 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001191
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001192 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001193 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001194 MacroDirective *MD = getMacroDirective(II);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001195
1196 // If the macro is not defined, this is an error.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001197 if (MD == 0) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001198 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001199 return;
1200 }
1201
1202 // Note that this macro has now been exported.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001203 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1204 MacroNameTok.getLocation(), /*IsPublic=*/true));
Douglas Gregorebf00492011-10-17 15:32:29 +00001205}
1206
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001207/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001208void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1209 Token MacroNameTok;
1210 ReadMacroName(MacroNameTok, 2);
1211
1212 // Error reading macro name? If so, diagnostic already issued.
1213 if (MacroNameTok.is(tok::eod))
1214 return;
1215
Douglas Gregor663b48f2012-01-03 19:48:16 +00001216 // Check to see if this is the last token on the #__private_macro line.
1217 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001218
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001219 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregorebf00492011-10-17 15:32:29 +00001220 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001221 MacroDirective *MD = getMacroDirective(II);
Douglas Gregorebf00492011-10-17 15:32:29 +00001222
1223 // If the macro is not defined, this is an error.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001224 if (MD == 0) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001225 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregorebf00492011-10-17 15:32:29 +00001226 return;
1227 }
1228
1229 // Note that this macro has now been marked private.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001230 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1231 MacroNameTok.getLocation(), /*IsPublic=*/false));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001232}
1233
Chris Lattnerf64b3522008-03-09 01:54:53 +00001234//===----------------------------------------------------------------------===//
1235// Preprocessor Include Directive Handling.
1236//===----------------------------------------------------------------------===//
1237
1238/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
James Dennettf6333ac2012-06-22 05:46:07 +00001239/// checked and spelled filename, e.g. as an operand of \#include. This returns
Chris Lattnerf64b3522008-03-09 01:54:53 +00001240/// true if the input filename was in <>'s or false if it were in ""'s. The
1241/// caller is expected to provide a buffer that is large enough to hold the
1242/// spelling of the filename, but is also expected to handle the case when
1243/// this method decides to use a different buffer.
1244bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001245 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001246 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001247 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattnerf64b3522008-03-09 01:54:53 +00001249 // Make sure the filename is <x> or "x".
1250 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001251 if (Buffer[0] == '<') {
1252 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001253 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001254 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001255 return true;
1256 }
1257 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001258 } else if (Buffer[0] == '"') {
1259 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001260 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001261 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001262 return true;
1263 }
1264 isAngled = false;
1265 } else {
1266 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001267 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001268 return true;
1269 }
Mike Stump11289f42009-09-09 15:08:12 +00001270
Chris Lattnerf64b3522008-03-09 01:54:53 +00001271 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001272 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001273 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001274 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001275 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattnerf64b3522008-03-09 01:54:53 +00001278 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001279 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001280 return isAngled;
1281}
1282
James Dennett4a4f72d2013-11-27 01:27:40 +00001283// \brief Handle cases where the \#include name is expanded from a macro
1284// as multiple tokens, which need to be glued together.
1285//
1286// This occurs for code like:
1287// \code
1288// \#define FOO <a/b.h>
1289// \#include FOO
1290// \endcode
1291// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1292//
1293// This code concatenates and consumes tokens up to the '>' token. It returns
1294// false if the > was found, otherwise it returns true if it finds and consumes
1295// the EOD marker.
1296bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001297 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001298 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001299
John Thompsonb5353522009-10-30 13:49:06 +00001300 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001301 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001302 End = CurTok.getLocation();
1303
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001304 // FIXME: Provide code completion for #includes.
1305 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001306 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001307 Lex(CurTok);
1308 continue;
1309 }
1310
Chris Lattnerf64b3522008-03-09 01:54:53 +00001311 // Append the spelling of this token to the buffer. If there was a space
1312 // before it, add it now.
1313 if (CurTok.hasLeadingSpace())
1314 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001315
Chris Lattnerf64b3522008-03-09 01:54:53 +00001316 // Get the spelling of the token, directly into FilenameBuffer if possible.
1317 unsigned PreAppendSize = FilenameBuffer.size();
1318 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattnerf64b3522008-03-09 01:54:53 +00001320 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001321 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001322
Chris Lattnerf64b3522008-03-09 01:54:53 +00001323 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1324 if (BufPtr != &FilenameBuffer[PreAppendSize])
1325 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001326
Chris Lattnerf64b3522008-03-09 01:54:53 +00001327 // Resize FilenameBuffer to the correct size.
1328 if (CurTok.getLength() != ActualLen)
1329 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001330
Chris Lattnerf64b3522008-03-09 01:54:53 +00001331 // If we found the '>' marker, return success.
1332 if (CurTok.is(tok::greater))
1333 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001334
John Thompsonb5353522009-10-30 13:49:06 +00001335 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001336 }
1337
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001338 // If we hit the eod marker, emit an error and return true so that the caller
1339 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001340 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001341 return true;
1342}
1343
Richard Smith34f30512013-11-23 04:06:09 +00001344/// \brief Push a token onto the token stream containing an annotation.
1345static void EnterAnnotationToken(Preprocessor &PP,
1346 SourceLocation Begin, SourceLocation End,
1347 tok::TokenKind Kind, void *AnnotationVal) {
1348 Token *Tok = new Token[1];
1349 Tok[0].startToken();
1350 Tok[0].setKind(Kind);
1351 Tok[0].setLocation(Begin);
1352 Tok[0].setAnnotationEndLoc(End);
1353 Tok[0].setAnnotationValue(AnnotationVal);
1354 PP.EnterTokenStream(Tok, 1, true, true);
1355}
1356
James Dennettf6333ac2012-06-22 05:46:07 +00001357/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1358/// the file to be included from the lexer, then include it! This is a common
1359/// routine with functionality shared between \#include, \#include_next and
1360/// \#import. LookupFrom is set when this is a \#include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001361/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001362void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1363 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001364 const DirectoryLookup *LookupFrom,
1365 bool isImport) {
1366
1367 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001368 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattnerf64b3522008-03-09 01:54:53 +00001370 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001371 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001372 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001373 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001374 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001375
Chris Lattnerf64b3522008-03-09 01:54:53 +00001376 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001377 case tok::eod:
1378 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001379 return;
Mike Stump11289f42009-09-09 15:08:12 +00001380
Chris Lattnerf64b3522008-03-09 01:54:53 +00001381 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001382 case tok::string_literal:
1383 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001384 End = FilenameTok.getLocation();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001385 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001386 break;
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattnerf64b3522008-03-09 01:54:53 +00001388 case tok::less:
1389 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1390 // case, glue the tokens together into FilenameBuffer and interpret those.
1391 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001392 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001393 return; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001394 Filename = FilenameBuffer.str();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001395 CharEnd = End.getLocWithOffset(1);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001396 break;
1397 default:
1398 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1399 DiscardUntilEndOfDirective();
1400 return;
1401 }
Mike Stump11289f42009-09-09 15:08:12 +00001402
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001403 CharSourceRange FilenameRange
1404 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
Aaron Ballman611306e2012-03-02 22:51:54 +00001405 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001406 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001407 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001408 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1409 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001410 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001411 DiscardUntilEndOfDirective();
1412 return;
1413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001415 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001416 // we allow macros that expand to nothing after the filename, because this
1417 // falls into the category of "#include pp-tokens new-line" specified in
1418 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001419 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001420
1421 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001422 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1423 Diag(FilenameTok, diag::err_pp_include_too_deep);
1424 return;
1425 }
Mike Stump11289f42009-09-09 15:08:12 +00001426
John McCall32f5fe12011-09-30 05:12:12 +00001427 // Complain about attempts to #include files in an audit pragma.
1428 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1429 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1430 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1431
1432 // Immediately leave the pragma.
1433 PragmaARCCFCodeAuditedLoc = SourceLocation();
1434 }
1435
Aaron Ballman611306e2012-03-02 22:51:54 +00001436 if (HeaderInfo.HasIncludeAliasMap()) {
1437 // Map the filename with the brackets still attached. If the name doesn't
1438 // map to anything, fall back on the filename we've already gotten the
1439 // spelling for.
1440 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1441 if (!NewName.empty())
1442 Filename = NewName;
1443 }
1444
Chris Lattnerf64b3522008-03-09 01:54:53 +00001445 // Search include directories.
1446 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001447 SmallString<1024> SearchPath;
1448 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001449 // We get the raw path only if we have 'Callbacks' to which we later pass
1450 // the path.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001451 ModuleMap::KnownHeader SuggestedModule;
1452 SourceLocation FilenameLoc = FilenameTok.getLocation();
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001453 SmallString<1024> NormalizedPath;
1454 if (LangOpts.MSVCCompat) {
1455 NormalizedPath = Filename.str();
1456 llvm::sys::fs::normalize_separators(NormalizedPath);
1457 }
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001458 const FileEntry *File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001459 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1460 isAngled, LookupFrom, CurDir, Callbacks ? &SearchPath : NULL,
1461 Callbacks ? &RelativePath : NULL,
Daniel Jasper07e6c402013-08-05 20:26:17 +00001462 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : 0);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001463
Douglas Gregor11729f02011-11-30 18:12:06 +00001464 if (Callbacks) {
1465 if (!File) {
1466 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001467 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001468 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1469 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1470 // Add the recovery path to the list of search paths.
Daniel Dunbarae4feb62013-01-25 01:50:28 +00001471 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Douglas Gregor11729f02011-11-30 18:12:06 +00001472 HeaderInfo.AddSearchPath(DL, isAngled);
1473
1474 // Try the lookup again, skipping the cache.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001475 File = LookupFile(FilenameLoc,
1476 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1477 : Filename,
1478 isAngled, LookupFrom, CurDir, 0, 0,
1479 HeaderInfo.getHeaderSearchOpts().ModuleMaps
Daniel Jasper07e6c402013-08-05 20:26:17 +00001480 ? &SuggestedModule
1481 : 0,
1482 /*SkipCache*/ true);
Douglas Gregor11729f02011-11-30 18:12:06 +00001483 }
1484 }
1485 }
1486
Daniel Jasper07e6c402013-08-05 20:26:17 +00001487 if (!SuggestedModule || !getLangOpts().Modules) {
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001488 // Notify the callback object that we've seen an inclusion directive.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001489 Callbacks->InclusionDirective(HashLoc, IncludeTok,
1490 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1491 : Filename,
1492 isAngled, FilenameRange, File, SearchPath,
1493 RelativePath, /*ImportedModule=*/0);
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001494 }
Douglas Gregor11729f02011-11-30 18:12:06 +00001495 }
1496
1497 if (File == 0) {
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001498 if (!SuppressIncludeNotFoundError) {
1499 // If the file could not be located and it was included via angle
1500 // brackets, we can attempt a lookup as though it were a quoted path to
1501 // provide the user with a possible fixit.
1502 if (isAngled) {
Daniel Jasper07e6c402013-08-05 20:26:17 +00001503 File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001504 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1505 false, LookupFrom, CurDir, Callbacks ? &SearchPath : 0,
1506 Callbacks ? &RelativePath : 0,
Daniel Jasper07e6c402013-08-05 20:26:17 +00001507 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : 0);
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001508 if (File) {
1509 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1510 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1511 Filename <<
1512 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1513 }
1514 }
1515 // If the file is still not found, just go with the vanilla diagnostic
1516 if (!File)
1517 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1518 }
1519 if (!File)
1520 return;
Douglas Gregor11729f02011-11-30 18:12:06 +00001521 }
1522
Douglas Gregor97eec242011-09-15 22:00:41 +00001523 // If we are supposed to import a module rather than including the header,
1524 // do so now.
Daniel Jasper07e6c402013-08-05 20:26:17 +00001525 if (SuggestedModule && getLangOpts().Modules) {
Douglas Gregor71944202011-11-30 00:36:36 +00001526 // Compute the module access path corresponding to this module.
1527 // FIXME: Should we have a second loadModule() overload to avoid this
1528 // extra lookup step?
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001529 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001530 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001531 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1532 FilenameTok.getLocation()));
1533 std::reverse(Path.begin(), Path.end());
1534
Douglas Gregor41e115a2011-11-30 18:02:36 +00001535 // Warn that we're replacing the include/import with a module import.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001536 SmallString<128> PathString;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001537 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1538 if (I)
1539 PathString += '.';
1540 PathString += Path[I].first->getName();
1541 }
1542 int IncludeKind = 0;
1543
1544 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1545 case tok::pp_include:
1546 IncludeKind = 0;
1547 break;
1548
1549 case tok::pp_import:
1550 IncludeKind = 1;
1551 break;
1552
Douglas Gregor4401fbe2011-11-30 18:03:26 +00001553 case tok::pp_include_next:
1554 IncludeKind = 2;
1555 break;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001556
1557 case tok::pp___include_macros:
1558 IncludeKind = 3;
1559 break;
1560
1561 default:
1562 llvm_unreachable("unknown include directive kind");
Douglas Gregor41e115a2011-11-30 18:02:36 +00001563 }
1564
Douglas Gregor2537a362011-12-08 17:01:29 +00001565 // Determine whether we are actually building the module that this
1566 // include directive maps to.
1567 bool BuildingImportedModule
David Blaikiebbafb8a2012-03-11 07:00:24 +00001568 = Path[0].first->getName() == getLangOpts().CurrentModule;
Richard Smith34f30512013-11-23 04:06:09 +00001569
David Blaikiebbafb8a2012-03-11 07:00:24 +00001570 if (!BuildingImportedModule && getLangOpts().ObjC2) {
Douglas Gregor2537a362011-12-08 17:01:29 +00001571 // If we're not building the imported module, warn that we're going
1572 // to automatically turn this inclusion directive into a module import.
Douglas Gregorda82e702012-01-03 19:32:59 +00001573 // We only do this in Objective-C, where we have a module-import syntax.
Douglas Gregor2537a362011-12-08 17:01:29 +00001574 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1575 /*IsTokenRange=*/false);
1576 Diag(HashLoc, diag::warn_auto_module_import)
1577 << IncludeKind << PathString
1578 << FixItHint::CreateReplacement(ReplaceRange,
Douglas Gregorc50d4922012-12-11 22:11:52 +00001579 "@import " + PathString.str().str() + ";");
Douglas Gregor2537a362011-12-08 17:01:29 +00001580 }
Douglas Gregor41e115a2011-11-30 18:02:36 +00001581
Richard Smithce587f52013-11-15 04:24:58 +00001582 // Load the module. Only make macros visible. We'll make the declarations
1583 // visible when the parser gets here.
1584 Module::NameVisibilityKind Visibility = Module::MacrosVisible;
Douglas Gregor7a626572012-11-29 23:55:25 +00001585 ModuleLoadResult Imported
Douglas Gregor98a52db2011-12-20 00:28:52 +00001586 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1587 /*IsIncludeDirective=*/true);
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001588 assert((Imported == 0 || Imported == SuggestedModule.getModule()) &&
Argyrios Kyrtzidis051b4432012-09-29 01:06:01 +00001589 "the imported module is different than the suggested one");
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00001590
1591 if (!Imported && hadModuleLoaderFatalFailure()) {
1592 // With a fatal failure in the module loader, we abort parsing.
1593 Token &Result = IncludeTok;
1594 if (CurLexer) {
1595 Result.startToken();
1596 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1597 CurLexer->cutOffLexing();
1598 } else {
1599 assert(CurPTHLexer && "#include but no current lexer set!");
1600 CurPTHLexer->getEOF(Result);
1601 }
1602 return;
1603 }
Richard Smithce587f52013-11-15 04:24:58 +00001604
Douglas Gregor2537a362011-12-08 17:01:29 +00001605 // If this header isn't part of the module we're building, we're done.
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001606 if (!BuildingImportedModule && Imported) {
1607 if (Callbacks) {
1608 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1609 FilenameRange, File,
1610 SearchPath, RelativePath, Imported);
1611 }
Richard Smithce587f52013-11-15 04:24:58 +00001612
1613 if (IncludeKind != 3) {
1614 // Let the parser know that we hit a module import, and it should
1615 // make the module visible.
1616 // FIXME: Produce this as the current token directly, rather than
1617 // allocating a new token for it.
Richard Smith34f30512013-11-23 04:06:09 +00001618 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include,
1619 Imported);
Richard Smithce587f52013-11-15 04:24:58 +00001620 }
Douglas Gregor2537a362011-12-08 17:01:29 +00001621 return;
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001622 }
Douglas Gregor7a626572012-11-29 23:55:25 +00001623
1624 // If we failed to find a submodule that we expected to find, we can
1625 // continue. Otherwise, there's an error in the included file, so we
1626 // don't want to include it.
1627 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1628 return;
1629 }
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001630 }
1631
1632 if (Callbacks && SuggestedModule) {
1633 // We didn't notify the callback object that we've seen an inclusion
1634 // directive before. Now that we are parsing the include normally and not
1635 // turning it to a module import, notify the callback object.
1636 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1637 FilenameRange, File,
1638 SearchPath, RelativePath,
1639 /*ImportedModule=*/0);
Douglas Gregor97eec242011-09-15 22:00:41 +00001640 }
1641
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001642 // The #included file will be considered to be a system header if either it is
1643 // in a system include directory, or if the #includer is a system include
1644 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001645 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001646 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001647 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001648
Chris Lattner72286d62010-04-19 20:44:31 +00001649 // Ask HeaderInfo if we should enter this #include file. If not, #including
1650 // this file will have no effect.
1651 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001652 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001653 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner72286d62010-04-19 20:44:31 +00001654 return;
1655 }
1656
Chris Lattnerf64b3522008-03-09 01:54:53 +00001657 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001658 SourceLocation IncludePos = End;
1659 // If the filename string was the result of macro expansions, set the include
1660 // position on the file where it will be included and after the expansions.
1661 if (IncludePos.isMacroID())
1662 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1663 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Peter Collingbourned395b932011-06-30 16:41:03 +00001664 assert(!FID.isInvalid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001665
Richard Smith34f30512013-11-23 04:06:09 +00001666 // Determine if we're switching to building a new submodule, and which one.
1667 ModuleMap::KnownHeader BuildingModule;
1668 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) {
1669 Module *RequestingModule = getModuleForLocation(FilenameLoc);
1670 BuildingModule =
1671 HeaderInfo.getModuleMap().findModuleForHeader(File, RequestingModule);
1672 }
1673
1674 // If all is good, enter the new file!
Richard Smith67294e22014-01-31 20:47:44 +00001675 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1676 return;
Richard Smith34f30512013-11-23 04:06:09 +00001677
1678 // If we're walking into another part of the same module, let the parser
1679 // know that any future declarations are within that other submodule.
Richard Smith67294e22014-01-31 20:47:44 +00001680 if (BuildingModule) {
1681 assert(!CurSubmodule && "should not have marked this as a module yet");
1682 CurSubmodule = BuildingModule.getModule();
1683
Richard Smith34f30512013-11-23 04:06:09 +00001684 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin,
Richard Smith67294e22014-01-31 20:47:44 +00001685 CurSubmodule);
1686 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001687}
1688
James Dennettf6333ac2012-06-22 05:46:07 +00001689/// HandleIncludeNextDirective - Implements \#include_next.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001690///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001691void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1692 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001693 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001694
Chris Lattnerf64b3522008-03-09 01:54:53 +00001695 // #include_next is like #include, except that we start searching after
1696 // the current found directory. If we can't do this, issue a
1697 // diagnostic.
1698 const DirectoryLookup *Lookup = CurDirLookup;
1699 if (isInPrimaryFile()) {
1700 Lookup = 0;
1701 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1702 } else if (Lookup == 0) {
1703 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1704 } else {
1705 // Start looking up in the next directory.
1706 ++Lookup;
1707 }
Mike Stump11289f42009-09-09 15:08:12 +00001708
Douglas Gregor796d76a2010-10-20 22:00:55 +00001709 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001710}
1711
James Dennettf6333ac2012-06-22 05:46:07 +00001712/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
Aaron Ballman0467f552012-03-18 03:10:37 +00001713void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1714 // The Microsoft #import directive takes a type library and generates header
1715 // files from it, and includes those. This is beyond the scope of what clang
1716 // does, so we ignore it and error out. However, #import can optionally have
1717 // trailing attributes that span multiple lines. We're going to eat those
1718 // so we can continue processing from there.
1719 Diag(Tok, diag::err_pp_import_directive_ms );
1720
1721 // Read tokens until we get to the end of the directive. Note that the
1722 // directive can be split over multiple lines using the backslash character.
1723 DiscardUntilEndOfDirective();
1724}
1725
James Dennettf6333ac2012-06-22 05:46:07 +00001726/// HandleImportDirective - Implements \#import.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001727///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001728void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1729 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001730 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
Alp Tokerbfa39342014-01-14 12:51:41 +00001731 if (LangOpts.MSVCCompat)
Aaron Ballman0467f552012-03-18 03:10:37 +00001732 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001733 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001734 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001735 return HandleIncludeDirective(HashLoc, ImportTok, 0, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001736}
1737
Chris Lattner58a1eb02009-04-08 18:46:40 +00001738/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1739/// pseudo directive in the predefines buffer. This handles it by sucking all
1740/// tokens through the preprocessor and discarding them (only keeping the side
1741/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001742void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1743 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001744 // This directive should only occur in the predefines buffer. If not, emit an
1745 // error and reject it.
1746 SourceLocation Loc = IncludeMacrosTok.getLocation();
1747 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1748 Diag(IncludeMacrosTok.getLocation(),
1749 diag::pp_include_macros_out_of_predefines);
1750 DiscardUntilEndOfDirective();
1751 return;
1752 }
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattnere01d82b2009-04-08 20:53:24 +00001754 // Treat this as a normal #include for checking purposes. If this is
1755 // successful, it will push a new lexer onto the include stack.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001756 HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false);
Mike Stump11289f42009-09-09 15:08:12 +00001757
Chris Lattnere01d82b2009-04-08 20:53:24 +00001758 Token TmpTok;
1759 do {
1760 Lex(TmpTok);
1761 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1762 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001763}
1764
Chris Lattnerf64b3522008-03-09 01:54:53 +00001765//===----------------------------------------------------------------------===//
1766// Preprocessor Macro Directive Handling.
1767//===----------------------------------------------------------------------===//
1768
1769/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1770/// definition has just been read. Lex the rest of the arguments and the
1771/// closing ), updating MI with what we learn. Return true if an error occurs
1772/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001773bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001774 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001775
Chris Lattnerf64b3522008-03-09 01:54:53 +00001776 while (1) {
1777 LexUnexpandedToken(Tok);
1778 switch (Tok.getKind()) {
1779 case tok::r_paren:
1780 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001781 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001782 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001783 // Otherwise we have #define FOO(A,)
1784 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1785 return true;
1786 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001787 if (!LangOpts.C99)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001788 Diag(Tok, LangOpts.CPlusPlus11 ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001789 diag::warn_cxx98_compat_variadic_macro :
1790 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001791
Joey Gouly1d58cdb2013-01-17 17:35:00 +00001792 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1793 if (LangOpts.OpenCL) {
1794 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1795 return true;
1796 }
1797
Chris Lattnerf64b3522008-03-09 01:54:53 +00001798 // Lex the token after the identifier.
1799 LexUnexpandedToken(Tok);
1800 if (Tok.isNot(tok::r_paren)) {
1801 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1802 return true;
1803 }
1804 // Add the __VA_ARGS__ identifier as an argument.
1805 Arguments.push_back(Ident__VA_ARGS__);
1806 MI->setIsC99Varargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001807 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001808 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001809 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001810 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1811 return true;
1812 default:
1813 // Handle keywords and identifiers here to accept things like
1814 // #define Foo(for) for.
1815 IdentifierInfo *II = Tok.getIdentifierInfo();
1816 if (II == 0) {
1817 // #define X(1
1818 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1819 return true;
1820 }
1821
1822 // If this is already used as an argument, it is used multiple times (e.g.
1823 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001824 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001825 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001826 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001827 return true;
1828 }
Mike Stump11289f42009-09-09 15:08:12 +00001829
Chris Lattnerf64b3522008-03-09 01:54:53 +00001830 // Add the argument to the macro info.
1831 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001832
Chris Lattnerf64b3522008-03-09 01:54:53 +00001833 // Lex the token after the identifier.
1834 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001835
Chris Lattnerf64b3522008-03-09 01:54:53 +00001836 switch (Tok.getKind()) {
1837 default: // #define X(A B
1838 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1839 return true;
1840 case tok::r_paren: // #define X(A)
Chris Lattner70946da2009-02-20 22:46:43 +00001841 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001842 return false;
1843 case tok::comma: // #define X(A,
1844 break;
1845 case tok::ellipsis: // #define X(A... -> GCC extension
1846 // Diagnose extension.
1847 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001848
Chris Lattnerf64b3522008-03-09 01:54:53 +00001849 // Lex the token after the identifier.
1850 LexUnexpandedToken(Tok);
1851 if (Tok.isNot(tok::r_paren)) {
1852 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1853 return true;
1854 }
Mike Stump11289f42009-09-09 15:08:12 +00001855
Chris Lattnerf64b3522008-03-09 01:54:53 +00001856 MI->setIsGNUVarargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001857 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001858 return false;
1859 }
1860 }
1861 }
1862}
1863
James Dennettf6333ac2012-06-22 05:46:07 +00001864/// HandleDefineDirective - Implements \#define. This consumes the entire macro
Chris Lattnerf64b3522008-03-09 01:54:53 +00001865/// line then lets the caller lex the next real token.
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001866void Preprocessor::HandleDefineDirective(Token &DefineTok,
1867 bool ImmediatelyAfterHeaderGuard) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001868 ++NumDefined;
1869
1870 Token MacroNameTok;
1871 ReadMacroName(MacroNameTok, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001872
Chris Lattnerf64b3522008-03-09 01:54:53 +00001873 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001874 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001875 return;
1876
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001877 Token LastTok = MacroNameTok;
1878
Chris Lattnerf64b3522008-03-09 01:54:53 +00001879 // If we are supposed to keep comments in #defines, reenable comment saving
1880 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00001881 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00001882
Chris Lattnerf64b3522008-03-09 01:54:53 +00001883 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001884 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001885
Chris Lattnerf64b3522008-03-09 01:54:53 +00001886 Token Tok;
1887 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001888
Chris Lattnerf64b3522008-03-09 01:54:53 +00001889 // If this is a function-like macro definition, parse the argument list,
1890 // marking each of the identifiers as being used as macro arguments. Also,
1891 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001892 if (Tok.is(tok::eod)) {
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001893 if (ImmediatelyAfterHeaderGuard) {
1894 // Save this macro information since it may part of a header guard.
1895 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
1896 MacroNameTok.getLocation());
1897 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001898 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00001899 } else if (Tok.hasLeadingSpace()) {
1900 // This is a normal token with leading space. Clear the leading space
1901 // marker on the first token to get proper expansion.
1902 Tok.clearFlag(Token::LeadingSpace);
1903 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001904 // This is a function-like macro definition. Read the argument list.
1905 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001906 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001907 // Forget about MI.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001908 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001909 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001910 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00001911 DiscardUntilEndOfDirective();
1912 return;
1913 }
1914
Chris Lattner249c38b2009-04-19 18:26:34 +00001915 // If this is a definition of a variadic C99 function-like macro, not using
1916 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00001917
Chris Lattner249c38b2009-04-19 18:26:34 +00001918 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1919 // This gets unpoisoned where it is allowed.
1920 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1921 if (MI->isC99Varargs())
1922 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00001923
Chris Lattnerf64b3522008-03-09 01:54:53 +00001924 // Read the first token after the arg list for down below.
1925 LexUnexpandedToken(Tok);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001926 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001927 // C99 requires whitespace between the macro definition and the body. Emit
1928 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00001929 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001930 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00001931 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1932 // first character of a replacement list is not a character required by
1933 // subclause 5.2.1, then there shall be white-space separation between the
1934 // identifier and the replacement list.". 5.2.1 lists this set:
1935 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1936 // is irrelevant here.
1937 bool isInvalid = false;
1938 if (Tok.is(tok::at)) // @ is not in the list above.
1939 isInvalid = true;
1940 else if (Tok.is(tok::unknown)) {
1941 // If we have an unknown token, it is something strange like "`". Since
1942 // all of valid characters would have lexed into a single character
1943 // token of some sort, we know this is not a valid case.
1944 isInvalid = true;
1945 }
1946 if (isInvalid)
1947 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1948 else
1949 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001950 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001951
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001952 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001953 LastTok = Tok;
1954
Chris Lattnerf64b3522008-03-09 01:54:53 +00001955 // Read the rest of the macro body.
1956 if (MI->isObjectLike()) {
1957 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001958 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001959 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001960 MI->AddTokenToBody(Tok);
1961 // Get the next token of the macro.
1962 LexUnexpandedToken(Tok);
1963 }
Mike Stump11289f42009-09-09 15:08:12 +00001964
Chris Lattnerf64b3522008-03-09 01:54:53 +00001965 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00001966 // Otherwise, read the body of a function-like macro. While we are at it,
1967 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1968 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001969 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001970 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001971
Eli Friedman14d3c792012-11-14 02:18:46 +00001972 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001973 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001974
Chris Lattnerf64b3522008-03-09 01:54:53 +00001975 // Get the next token of the macro.
1976 LexUnexpandedToken(Tok);
1977 continue;
1978 }
Mike Stump11289f42009-09-09 15:08:12 +00001979
Richard Smith701a3522013-07-09 01:00:29 +00001980 // If we're in -traditional mode, then we should ignore stringification
1981 // and token pasting. Mark the tokens as unknown so as not to confuse
1982 // things.
1983 if (getLangOpts().TraditionalCPP) {
1984 Tok.setKind(tok::unknown);
1985 MI->AddTokenToBody(Tok);
1986
1987 // Get the next token of the macro.
1988 LexUnexpandedToken(Tok);
1989 continue;
1990 }
1991
Eli Friedman14d3c792012-11-14 02:18:46 +00001992 if (Tok.is(tok::hashhash)) {
1993
1994 // If we see token pasting, check if it looks like the gcc comma
1995 // pasting extension. We'll use this information to suppress
1996 // diagnostics later on.
1997
1998 // Get the next token of the macro.
1999 LexUnexpandedToken(Tok);
2000
2001 if (Tok.is(tok::eod)) {
2002 MI->AddTokenToBody(LastTok);
2003 break;
2004 }
2005
2006 unsigned NumTokens = MI->getNumTokens();
2007 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2008 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2009 MI->setHasCommaPasting();
2010
David Majnemer76faf1f2013-11-05 09:30:17 +00002011 // Things look ok, add the '##' token to the macro.
Eli Friedman14d3c792012-11-14 02:18:46 +00002012 MI->AddTokenToBody(LastTok);
Eli Friedman14d3c792012-11-14 02:18:46 +00002013 continue;
2014 }
2015
Chris Lattnerf64b3522008-03-09 01:54:53 +00002016 // Get the next token of the macro.
2017 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002018
Chris Lattner83bd8282009-05-25 17:16:10 +00002019 // Check for a valid macro arg identifier.
2020 if (Tok.getIdentifierInfo() == 0 ||
2021 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2022
2023 // If this is assembler-with-cpp mode, we accept random gibberish after
2024 // the '#' because '#' is often a comment character. However, change
2025 // the kind of the token to tok::unknown so that the preprocessor isn't
2026 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002027 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00002028 LastTok.setKind(tok::unknown);
Eli Friedmancdf8b882013-06-18 21:33:38 +00002029 MI->AddTokenToBody(LastTok);
2030 continue;
Chris Lattner83bd8282009-05-25 17:16:10 +00002031 } else {
2032 Diag(Tok, diag::err_pp_stringize_not_parameter);
2033 ReleaseMacroInfo(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002034
Chris Lattner83bd8282009-05-25 17:16:10 +00002035 // Disable __VA_ARGS__ again.
2036 Ident__VA_ARGS__->setIsPoisoned(true);
2037 return;
2038 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002039 }
Mike Stump11289f42009-09-09 15:08:12 +00002040
Chris Lattner83bd8282009-05-25 17:16:10 +00002041 // Things look ok, add the '#' and param name tokens to the macro.
2042 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002043 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00002044 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00002045
Chris Lattnerf64b3522008-03-09 01:54:53 +00002046 // Get the next token of the macro.
2047 LexUnexpandedToken(Tok);
2048 }
2049 }
Mike Stump11289f42009-09-09 15:08:12 +00002050
2051
Chris Lattnerf64b3522008-03-09 01:54:53 +00002052 // Disable __VA_ARGS__ again.
2053 Ident__VA_ARGS__->setIsPoisoned(true);
2054
Chris Lattner57540c52011-04-15 05:22:18 +00002055 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00002056 // replacement list.
2057 unsigned NumTokens = MI->getNumTokens();
2058 if (NumTokens != 0) {
2059 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2060 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002061 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002062 return;
2063 }
2064 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2065 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002066 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002067 return;
2068 }
2069 }
Mike Stump11289f42009-09-09 15:08:12 +00002070
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002071 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00002072
Chris Lattnerf64b3522008-03-09 01:54:53 +00002073 // Finally, if this identifier already had a macro defined for it, verify that
Alexander Kornienko8b3f6232012-08-29 00:20:03 +00002074 // the macro bodies are identical, and issue diagnostics if they are not.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002075 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner5244f342009-01-16 19:50:11 +00002076 // It is very common for system headers to have tons of macro redefinitions
2077 // and for warnings to be disabled in system headers. If this is the case,
2078 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00002079 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00002080 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002081 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00002082 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002083
Richard Smith7b242542013-03-06 00:46:00 +00002084 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2085 // C++ [cpp.predefined]p4, but allow it as an extension.
2086 if (OtherMI->isBuiltinMacro())
2087 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
Chris Lattnerc0a585d2010-08-17 15:55:45 +00002088 // Macros must be identical. This means all tokens and whitespace
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002089 // separation must be the same. C99 6.10.3p2.
Richard Smith7b242542013-03-06 00:46:00 +00002090 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002091 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
Chris Lattner5244f342009-01-16 19:50:11 +00002092 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2093 << MacroNameTok.getIdentifierInfo();
2094 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2095 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002096 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002097 if (OtherMI->isWarnIfUnused())
2098 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002099 }
Mike Stump11289f42009-09-09 15:08:12 +00002100
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002101 DefMacroDirective *MD =
2102 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00002103
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002104 assert(!MI->isUsed());
2105 // If we need warning for not using the macro, add its location in the
2106 // warn-because-unused-macro set. If it gets used it will be removed from set.
Eli Friedman5ba37d52013-08-22 00:27:10 +00002107 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002108 Diags->getDiagnosticLevel(diag::pp_macro_not_used,
David Blaikie9c902b52011-09-25 23:23:43 +00002109 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002110 MI->setIsWarnIfUnused(true);
2111 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2112 }
2113
Chris Lattner928e9092009-04-12 01:39:54 +00002114 // If the callbacks want to know, tell them about the macro definition.
2115 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002116 Callbacks->MacroDefined(MacroNameTok, MD);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002117}
2118
James Dennettf6333ac2012-06-22 05:46:07 +00002119/// HandleUndefDirective - Implements \#undef.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002120///
2121void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2122 ++NumUndefined;
2123
2124 Token MacroNameTok;
2125 ReadMacroName(MacroNameTok, 2);
Mike Stump11289f42009-09-09 15:08:12 +00002126
Chris Lattnerf64b3522008-03-09 01:54:53 +00002127 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002128 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00002129 return;
Mike Stump11289f42009-09-09 15:08:12 +00002130
Chris Lattnerf64b3522008-03-09 01:54:53 +00002131 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002132 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00002133
Chris Lattnerf64b3522008-03-09 01:54:53 +00002134 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002135 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002136 const MacroInfo *MI = MD ? MD->getMacroInfo() : 0;
Mike Stump11289f42009-09-09 15:08:12 +00002137
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002138 // If the callbacks want to know, tell them about the macro #undef.
2139 // Note: no matter if the macro was defined or not.
2140 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002141 Callbacks->MacroUndefined(MacroNameTok, MD);
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002142
Chris Lattnerf64b3522008-03-09 01:54:53 +00002143 // If the macro is not defined, this is a noop undef, just return.
2144 if (MI == 0) return;
2145
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00002146 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00002147 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00002148
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002149 if (MI->isWarnIfUnused())
2150 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2151
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002152 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2153 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
Chris Lattnerf64b3522008-03-09 01:54:53 +00002154}
2155
2156
2157//===----------------------------------------------------------------------===//
2158// Preprocessor Conditional Directive Handling.
2159//===----------------------------------------------------------------------===//
2160
James Dennettf6333ac2012-06-22 05:46:07 +00002161/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2162/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2163/// true if any tokens have been returned or pp-directives activated before this
2164/// \#ifndef has been lexed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002165///
2166void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2167 bool ReadAnyTokensBeforeDirective) {
2168 ++NumIf;
2169 Token DirectiveTok = Result;
2170
2171 Token MacroNameTok;
2172 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00002173
Chris Lattnerf64b3522008-03-09 01:54:53 +00002174 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002175 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002176 // Skip code until we get to #endif. This helps with recovery by not
2177 // emitting an error when the #endif is reached.
2178 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2179 /*Foundnonskip*/false, /*FoundElse*/false);
2180 return;
2181 }
Mike Stump11289f42009-09-09 15:08:12 +00002182
Chris Lattnerf64b3522008-03-09 01:54:53 +00002183 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002184 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00002185
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002186 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002187 MacroDirective *MD = getMacroDirective(MII);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002188 MacroInfo *MI = MD ? MD->getMacroInfo() : 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002189
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002190 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002191 // If the start of a top-level #ifdef and if the macro is not defined,
2192 // inform MIOpt that this might be the start of a proper include guard.
2193 // Otherwise it is some other form of unknown conditional which we can't
2194 // handle.
2195 if (!ReadAnyTokensBeforeDirective && MI == 0) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002196 assert(isIfndef && "#ifdef shouldn't reach here");
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002197 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002198 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002199 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002200 }
2201
Chris Lattnerf64b3522008-03-09 01:54:53 +00002202 // If there is a macro, process it.
2203 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002204 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002205
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002206 if (Callbacks) {
2207 if (isIfndef)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002208 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002209 else
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002210 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002211 }
2212
Chris Lattnerf64b3522008-03-09 01:54:53 +00002213 // Should we include the stuff contained by this directive?
2214 if (!MI == isIfndef) {
2215 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00002216 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2217 /*wasskip*/false, /*foundnonskip*/true,
2218 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002219 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002220 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002221 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00002222 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002223 /*FoundElse*/false);
2224 }
2225}
2226
James Dennettf6333ac2012-06-22 05:46:07 +00002227/// HandleIfDirective - Implements the \#if directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002228///
2229void Preprocessor::HandleIfDirective(Token &IfToken,
2230 bool ReadAnyTokensBeforeDirective) {
2231 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00002232
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002233 // Parse and evaluate the conditional expression.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002234 IdentifierInfo *IfNDefMacro = 0;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002235 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2236 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2237 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00002238
2239 // If this condition is equivalent to #ifndef X, and if this is the first
2240 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002241 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002242 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Richard Smith089ee152013-06-16 05:05:39 +00002243 // FIXME: Pass in the location of the macro name, not the 'if' token.
2244 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
Nuno Lopes363212b2008-06-01 18:31:24 +00002245 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002246 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00002247 }
2248
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002249 if (Callbacks)
2250 Callbacks->If(IfToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002251 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002252 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002253
Chris Lattnerf64b3522008-03-09 01:54:53 +00002254 // Should we include the stuff contained by this directive?
2255 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002256 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002257 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002258 /*foundnonskip*/true, /*foundelse*/false);
2259 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002260 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00002261 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002262 /*FoundElse*/false);
2263 }
2264}
2265
James Dennettf6333ac2012-06-22 05:46:07 +00002266/// HandleEndifDirective - Implements the \#endif directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002267///
2268void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2269 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00002270
Chris Lattnerf64b3522008-03-09 01:54:53 +00002271 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002272 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00002273
Chris Lattnerf64b3522008-03-09 01:54:53 +00002274 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002275 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002276 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00002277 Diag(EndifToken, diag::err_pp_endif_without_if);
2278 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002279 }
Mike Stump11289f42009-09-09 15:08:12 +00002280
Chris Lattnerf64b3522008-03-09 01:54:53 +00002281 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002282 if (CurPPLexer->getConditionalStackDepth() == 0)
2283 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002284
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002285 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002286 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002287
2288 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002289 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002290}
2291
James Dennettf6333ac2012-06-22 05:46:07 +00002292/// HandleElseDirective - Implements the \#else directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002293///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002294void Preprocessor::HandleElseDirective(Token &Result) {
2295 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattnerf64b3522008-03-09 01:54:53 +00002297 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002298 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002299
Chris Lattnerf64b3522008-03-09 01:54:53 +00002300 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002301 if (CurPPLexer->popConditionalLevel(CI)) {
2302 Diag(Result, diag::pp_err_else_without_if);
2303 return;
2304 }
Mike Stump11289f42009-09-09 15:08:12 +00002305
Chris Lattnerf64b3522008-03-09 01:54:53 +00002306 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002307 if (CurPPLexer->getConditionalStackDepth() == 0)
2308 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002309
2310 // If this is a #else with a #else before it, report the error.
2311 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002312
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002313 if (Callbacks)
2314 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2315
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002316 // Finally, skip the rest of the contents of this block.
2317 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002318 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002319}
2320
James Dennettf6333ac2012-06-22 05:46:07 +00002321/// HandleElifDirective - Implements the \#elif directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002322///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002323void Preprocessor::HandleElifDirective(Token &ElifToken) {
2324 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002325
Chris Lattnerf64b3522008-03-09 01:54:53 +00002326 // #elif directive in a non-skipping conditional... start skipping.
2327 // We don't care what the condition is, because we will always skip it (since
2328 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002329 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002330 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002331 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002332
2333 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002334 if (CurPPLexer->popConditionalLevel(CI)) {
2335 Diag(ElifToken, diag::pp_err_elif_without_if);
2336 return;
2337 }
Mike Stump11289f42009-09-09 15:08:12 +00002338
Chris Lattnerf64b3522008-03-09 01:54:53 +00002339 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002340 if (CurPPLexer->getConditionalStackDepth() == 0)
2341 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002342
Chris Lattnerf64b3522008-03-09 01:54:53 +00002343 // If this is a #elif with a #else before it, report the error.
2344 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002345
2346 if (Callbacks)
2347 Callbacks->Elif(ElifToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002348 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002349 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002350
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002351 // Finally, skip the rest of the contents of this block.
2352 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002353 /*FoundElse*/CI.FoundElse,
2354 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002355}