blob: c38080f7159e4b6c8ac3ed9ad9acce5a7c298e10 [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() {
Richard Smithee0c4c12014-07-24 01:13:23 +000037 MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
Ted Kremenekc8456f82010-10-19 22:15:20 +000038 MIChain->Next = MIChainHead;
Ted Kremenekc8456f82010-10-19 22:15:20 +000039 MIChainHead = MIChain;
Richard Smithee0c4c12014-07-24 01:13:23 +000040 return &MIChain->MI;
Chris Lattnerc0a585d2010-08-17 15:55:45 +000041}
42
43MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
44 MacroInfo *MI = AllocateMacroInfo();
Ted Kremenek6c7ea112008-12-15 19:56:42 +000045 new (MI) MacroInfo(L);
46 return MI;
47}
48
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000049MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
50 unsigned SubModuleID) {
Chandler Carruth06dde922014-03-02 13:02:01 +000051 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
52 "alignment for MacroInfo is less than the ID");
Argyrios Kyrtzidisd48b91d2013-04-30 05:05:35 +000053 DeserializedMacroInfoChain *MIChain =
54 BP.Allocate<DeserializedMacroInfoChain>();
55 MIChain->Next = DeserialMIChainHead;
56 DeserialMIChainHead = MIChain;
57
58 MacroInfo *MI = &MIChain->MI;
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000059 new (MI) MacroInfo(L);
60 MI->FromASTFile = true;
61 MI->setOwningModuleID(SubModuleID);
62 return MI;
63}
64
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +000065DefMacroDirective *
66Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc,
67 bool isImported) {
68 DefMacroDirective *MD = BP.Allocate<DefMacroDirective>();
69 new (MD) DefMacroDirective(MI, Loc, isImported);
70 return MD;
71}
72
73UndefMacroDirective *
74Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
75 UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>();
76 new (MD) UndefMacroDirective(UndefLoc);
77 return MD;
78}
79
80VisibilityMacroDirective *
81Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
82 bool isPublic) {
83 VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>();
84 new (MD) VisibilityMacroDirective(Loc, isPublic);
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +000085 return MD;
Chris Lattnerc0a585d2010-08-17 15:55:45 +000086}
87
James Dennettf6333ac2012-06-22 05:46:07 +000088/// \brief Release the specified MacroInfo to be reused for allocating
89/// new MacroInfo objects.
Chris Lattner66b67d22010-08-18 16:08:51 +000090void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) {
Ted Kremenekc8456f82010-10-19 22:15:20 +000091 MI->Destroy();
92}
Chris Lattner666f7a42009-02-20 22:19:20 +000093
James Dennettf6333ac2012-06-22 05:46:07 +000094/// \brief Read and discard all tokens remaining on the current line until
95/// the tok::eod token is found.
Chris Lattnerf64b3522008-03-09 01:54:53 +000096void Preprocessor::DiscardUntilEndOfDirective() {
97 Token Tmp;
98 do {
99 LexUnexpandedToken(Tmp);
Peter Collingbournef29ce972011-02-22 13:49:06 +0000100 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000101 } while (Tmp.isNot(tok::eod));
Chris Lattnerf64b3522008-03-09 01:54:53 +0000102}
103
Alp Tokerb05e0b52014-05-21 06:13:51 +0000104bool Preprocessor::CheckMacroName(Token &MacroNameTok, char isDefineUndef) {
105 // Missing macro name?
106 if (MacroNameTok.is(tok::eod))
107 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
108
109 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
110 if (!II) {
111 bool Invalid = false;
112 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
113 if (Invalid)
114 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Alp Tokerf33619c2014-05-31 03:38:08 +0000115 II = getIdentifierInfo(Spelling);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000116
Alp Tokerf33619c2014-05-31 03:38:08 +0000117 if (!II->isCPlusPlusOperatorKeyword())
118 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000119
Alp Tokere03e9e12014-05-31 16:32:22 +0000120 // C++ 2.5p2: Alternative tokens behave the same as its primary token
121 // except for their spellings.
122 Diag(MacroNameTok, getLangOpts().MicrosoftExt
123 ? diag::ext_pp_operator_used_as_macro_name
124 : diag::err_pp_operator_used_as_macro_name)
125 << II << MacroNameTok.getKind();
Alp Tokerb05e0b52014-05-21 06:13:51 +0000126
Alp Tokerc5d194fc2014-05-31 03:38:17 +0000127 // Allow #defining |and| and friends for Microsoft compatibility or
128 // recovery when legacy C headers are included in C++.
Alp Tokerf33619c2014-05-31 03:38:08 +0000129 MacroNameTok.setIdentifierInfo(II);
Alp Tokerb05e0b52014-05-21 06:13:51 +0000130 }
131
132 if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
133 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
134 return Diag(MacroNameTok, diag::err_defined_macro_name);
135 }
136
137 if (isDefineUndef == 2 && II->hasMacroDefinition() &&
138 getMacroInfo(II)->isBuiltinMacro()) {
139 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
140 // and C++ [cpp.predefined]p4], but allow it as an extension.
141 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
142 }
143
144 // Okay, we got a good identifier.
145 return false;
146}
147
James Dennettf6333ac2012-06-22 05:46:07 +0000148/// \brief Lex and validate a macro name, which occurs after a
149/// \#define or \#undef.
150///
151/// This sets the token kind to eod and discards the rest
152/// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if
153/// this is due to a a \#define, 2 if \#undef directive, 0 if it is something
154/// else (e.g. \#ifdef).
Chris Lattnerf64b3522008-03-09 01:54:53 +0000155void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
156 // Read the token, don't allow macro expansion on it.
157 LexUnexpandedToken(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +0000158
Douglas Gregor12785102010-08-24 20:21:13 +0000159 if (MacroNameTok.is(tok::code_completion)) {
160 if (CodeComplete)
161 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000162 setCodeCompletionReached();
Douglas Gregor12785102010-08-24 20:21:13 +0000163 LexUnexpandedToken(MacroNameTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000164 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000165
166 if (!CheckMacroName(MacroNameTok, isDefineUndef))
Chris Lattner907dfe92008-11-18 07:59:24 +0000167 return;
Alp Tokerb05e0b52014-05-21 06:13:51 +0000168
169 // Invalid macro name, read and discard the rest of the line and set the
170 // token kind to tok::eod if necessary.
171 if (MacroNameTok.isNot(tok::eod)) {
172 MacroNameTok.setKind(tok::eod);
173 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +0000174 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000175}
176
James Dennettf6333ac2012-06-22 05:46:07 +0000177/// \brief Ensure that the next token is a tok::eod token.
178///
179/// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
Chris Lattner0003c272009-04-17 23:30:53 +0000180/// true, then we consider macros that expand to zero tokens as being ok.
181void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000182 Token Tmp;
Chris Lattner0003c272009-04-17 23:30:53 +0000183 // Lex unexpanded tokens for most directives: macros might expand to zero
184 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
185 // #line) allow empty macros.
186 if (EnableMacros)
187 Lex(Tmp);
188 else
189 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000190
Chris Lattnerf64b3522008-03-09 01:54:53 +0000191 // There should be no tokens after the directive, but we allow them as an
192 // extension.
193 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
194 LexUnexpandedToken(Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000195
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000196 if (Tmp.isNot(tok::eod)) {
Chris Lattner825676a2009-04-14 05:15:20 +0000197 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000198 // or if this is a macro-style preprocessing directive, because it is more
199 // trouble than it is worth to insert /**/ and check that there is no /**/
200 // in the range also.
Douglas Gregora771f462010-03-31 17:46:05 +0000201 FixItHint Hint;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000202 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
Peter Collingbourne2c9f9662011-02-22 13:49:00 +0000203 !CurTokenLexer)
Douglas Gregora771f462010-03-31 17:46:05 +0000204 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
205 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000206 DiscardUntilEndOfDirective();
207 }
208}
209
210
211
James Dennettf6333ac2012-06-22 05:46:07 +0000212/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
213/// decided that the subsequent tokens are in the \#if'd out portion of the
214/// file. Lex the rest of the file, until we see an \#endif. If
Chris Lattnerf64b3522008-03-09 01:54:53 +0000215/// FoundNonSkipPortion is true, then we have already emitted code for part of
James Dennettf6333ac2012-06-22 05:46:07 +0000216/// this \#if directive, so \#else/\#elif blocks should never be entered.
217/// If ElseOk is true, then \#else directives are ok, if not, then we have
218/// already seen one so a \#else directive is a duplicate. When this returns,
219/// the caller can lex the first valid token.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000220void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
221 bool FoundNonSkipPortion,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000222 bool FoundElse,
223 SourceLocation ElseLoc) {
Chris Lattnerf64b3522008-03-09 01:54:53 +0000224 ++NumSkipped;
David Blaikie7d170102013-05-15 07:37:26 +0000225 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
Chris Lattnerf64b3522008-03-09 01:54:53 +0000226
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000227 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000228 FoundNonSkipPortion, FoundElse);
Mike Stump11289f42009-09-09 15:08:12 +0000229
Ted Kremenek56572ab2008-12-12 18:34:08 +0000230 if (CurPTHLexer) {
231 PTHSkipExcludedConditionalBlock();
232 return;
233 }
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattnerf64b3522008-03-09 01:54:53 +0000235 // Enter raw mode to disable identifier lookup (and thus macro expansion),
236 // disabling warnings, etc.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000237 CurPPLexer->LexingRawMode = true;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000238 Token Tok;
239 while (1) {
Chris Lattnerf406b242010-01-18 22:33:01 +0000240 CurLexer->Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000241
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000242 if (Tok.is(tok::code_completion)) {
243 if (CodeComplete)
244 CodeComplete->CodeCompleteInConditionalExclusion();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000245 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000246 continue;
247 }
248
Chris Lattnerf64b3522008-03-09 01:54:53 +0000249 // If this is the end of the buffer, we have an error.
250 if (Tok.is(tok::eof)) {
251 // Emit errors for each unterminated conditional on the stack, including
252 // the current one.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000253 while (!CurPPLexer->ConditionalStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000254 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
Douglas Gregor02690ba2010-08-12 17:04:55 +0000255 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
256 diag::err_pp_unterminated_conditional);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000257 CurPPLexer->ConditionalStack.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000258 }
259
Chris Lattnerf64b3522008-03-09 01:54:53 +0000260 // Just return and let the caller lex after this #include.
261 break;
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattnerf64b3522008-03-09 01:54:53 +0000264 // If this token is not a preprocessor directive, just skip it.
265 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
266 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattnerf64b3522008-03-09 01:54:53 +0000268 // We just parsed a # character at the start of a line, so we're in
269 // directive mode. Tell the lexer this so any newlines we see will be
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000270 // converted into an EOD token (this terminates the macro).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000271 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rose176057b2013-02-22 00:32:00 +0000272 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000273
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattnerf64b3522008-03-09 01:54:53 +0000275 // Read the next token, the directive flavor.
276 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattnerf64b3522008-03-09 01:54:53 +0000278 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
279 // something bogus), skip it.
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000280 if (Tok.isNot(tok::raw_identifier)) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000281 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000282 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000283 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000284 continue;
285 }
286
287 // If the first letter isn't i or e, it isn't intesting to us. We know that
288 // this is safe in the face of spelling differences, because there is no way
289 // to spell an i/e in a strange way that is another letter. Skipping this
290 // allows us to avoid looking up the identifier info for #define/#undef and
291 // other common directives.
Alp Toker2d57cea2014-05-17 04:53:25 +0000292 StringRef RI = Tok.getRawIdentifier();
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000293
Alp Toker2d57cea2014-05-17 04:53:25 +0000294 char FirstChar = RI[0];
Mike Stump11289f42009-09-09 15:08:12 +0000295 if (FirstChar >= 'a' && FirstChar <= 'z' &&
Chris Lattnerf64b3522008-03-09 01:54:53 +0000296 FirstChar != 'i' && FirstChar != 'e') {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000297 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000298 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000299 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000300 continue;
301 }
Mike Stump11289f42009-09-09 15:08:12 +0000302
Chris Lattnerf64b3522008-03-09 01:54:53 +0000303 // Get the identifier name without trigraphs or embedded newlines. Note
304 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
305 // when skipping.
Benjamin Kramer144884642009-12-31 13:32:38 +0000306 char DirectiveBuf[20];
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000307 StringRef Directive;
Alp Toker2d57cea2014-05-17 04:53:25 +0000308 if (!Tok.needsCleaning() && RI.size() < 20) {
309 Directive = RI;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000310 } else {
311 std::string DirectiveStr = getSpelling(Tok);
Benjamin Kramer144884642009-12-31 13:32:38 +0000312 unsigned IdLen = DirectiveStr.size();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000313 if (IdLen >= 20) {
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000314 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000315 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000316 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000317 continue;
318 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000319 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000320 Directive = StringRef(DirectiveBuf, IdLen);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000321 }
Mike Stump11289f42009-09-09 15:08:12 +0000322
Benjamin Kramer144884642009-12-31 13:32:38 +0000323 if (Directive.startswith("if")) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000324 StringRef Sub = Directive.substr(2);
Benjamin Kramer144884642009-12-31 13:32:38 +0000325 if (Sub.empty() || // "if"
326 Sub == "def" || // "ifdef"
327 Sub == "ndef") { // "ifndef"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000328 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
329 // bother parsing the condition.
330 DiscardUntilEndOfDirective();
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000331 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerf64b3522008-03-09 01:54:53 +0000332 /*foundnonskip*/false,
Chandler Carruth540960f2011-01-03 17:40:17 +0000333 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000334 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000335 } else if (Directive[0] == 'e') {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000336 StringRef Sub = Directive.substr(1);
Benjamin Kramer144884642009-12-31 13:32:38 +0000337 if (Sub == "ndif") { // "endif"
Chris Lattnerf64b3522008-03-09 01:54:53 +0000338 PPConditionalInfo CondInfo;
339 CondInfo.WasSkipping = true; // Silence bogus warning.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000340 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000341 (void)InCond; // Silence warning in no-asserts mode.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000342 assert(!InCond && "Can't be skipping if not in a conditional!");
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattnerf64b3522008-03-09 01:54:53 +0000344 // If we popped the outermost skipping block, we're done skipping!
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000345 if (!CondInfo.WasSkipping) {
Richard Smith87d8fb92012-06-24 23:56:26 +0000346 // Restore the value of LexingRawMode so that trailing comments
347 // are handled correctly, if we've reached the outermost block.
348 CurPPLexer->LexingRawMode = false;
Richard Smithd0124572012-06-21 00:35:03 +0000349 CheckEndOfDirective("endif");
Richard Smith87d8fb92012-06-24 23:56:26 +0000350 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000351 if (Callbacks)
352 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000353 break;
Richard Smithd0124572012-06-21 00:35:03 +0000354 } else {
355 DiscardUntilEndOfDirective();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000356 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000357 } else if (Sub == "lse") { // "else".
Chris Lattnerf64b3522008-03-09 01:54:53 +0000358 // #else directive in a skipping conditional. If not in some other
359 // skipping conditional, and if #else hasn't already been seen, enter it
360 // as a non-skipping conditional.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000361 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chris Lattnerf64b3522008-03-09 01:54:53 +0000363 // If this is a #else with a #else before it, report the error.
364 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattnerf64b3522008-03-09 01:54:53 +0000366 // Note that we've seen a #else in this conditional.
367 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnerf64b3522008-03-09 01:54:53 +0000369 // If the conditional is at the top level, and the #if block wasn't
370 // entered, enter the #else block now.
371 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
372 CondInfo.FoundNonSkip = true;
Richard Smith87d8fb92012-06-24 23:56:26 +0000373 // Restore the value of LexingRawMode so that trailing comments
374 // are handled correctly.
375 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000376 CheckEndOfDirective("else");
Richard Smith87d8fb92012-06-24 23:56:26 +0000377 CurPPLexer->LexingRawMode = true;
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000378 if (Callbacks)
379 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000380 break;
Argyrios Kyrtzidis627c14a2011-05-21 04:26:04 +0000381 } else {
382 DiscardUntilEndOfDirective(); // C99 6.10p4.
Chris Lattnerf64b3522008-03-09 01:54:53 +0000383 }
Benjamin Kramer144884642009-12-31 13:32:38 +0000384 } else if (Sub == "lif") { // "elif".
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000385 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000386
John Thompson17c35732013-12-04 20:19:30 +0000387 // If this is a #elif with a #else before it, report the error.
388 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
389
Chris Lattnerf64b3522008-03-09 01:54:53 +0000390 // If this is in a skipping block or if we're already handled this #if
391 // block, don't bother parsing the condition.
392 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
393 DiscardUntilEndOfDirective();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000394 } else {
John Thompson17c35732013-12-04 20:19:30 +0000395 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000396 // Restore the value of LexingRawMode so that identifiers are
397 // looked up, etc, inside the #elif expression.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000398 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
399 CurPPLexer->LexingRawMode = false;
Craig Topperd2d442c2014-05-17 23:10:59 +0000400 IdentifierInfo *IfNDefMacro = nullptr;
John Thompson17c35732013-12-04 20:19:30 +0000401 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000402 CurPPLexer->LexingRawMode = true;
John Thompson17c35732013-12-04 20:19:30 +0000403 if (Callbacks) {
404 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +0000405 Callbacks->Elif(Tok.getLocation(),
John Thompson17c35732013-12-04 20:19:30 +0000406 SourceRange(CondBegin, CondEnd),
John Thompson87f9fef2013-12-07 08:41:15 +0000407 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
John Thompson17c35732013-12-04 20:19:30 +0000408 }
409 // If this condition is true, enter it!
410 if (CondValue) {
411 CondInfo.FoundNonSkip = true;
412 break;
413 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000414 }
415 }
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000418 CurPPLexer->ParsingPreprocessorDirective = false;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000419 // Restore comment saving mode.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000420 if (CurLexer) CurLexer->resetExtendedTokenMode();
Chris Lattnerf64b3522008-03-09 01:54:53 +0000421 }
422
423 // Finally, if we are out of the conditional (saw an #endif or ran off the end
424 // of the file, just stop skipping and return to lexing whatever came after
425 // the #if block.
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000426 CurPPLexer->LexingRawMode = false;
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +0000427
428 if (Callbacks) {
429 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
430 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
431 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000432}
433
Ted Kremenek56572ab2008-12-12 18:34:08 +0000434void Preprocessor::PTHSkipExcludedConditionalBlock() {
Mike Stump11289f42009-09-09 15:08:12 +0000435
436 while (1) {
Ted Kremenek56572ab2008-12-12 18:34:08 +0000437 assert(CurPTHLexer);
438 assert(CurPTHLexer->LexingRawMode == false);
Mike Stump11289f42009-09-09 15:08:12 +0000439
Ted Kremenek56572ab2008-12-12 18:34:08 +0000440 // Skip to the next '#else', '#elif', or #endif.
441 if (CurPTHLexer->SkipBlock()) {
442 // We have reached an #endif. Both the '#' and 'endif' tokens
443 // have been consumed by the PTHLexer. Just pop off the condition level.
444 PPConditionalInfo CondInfo;
445 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +0000446 (void)InCond; // Silence warning in no-asserts mode.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000447 assert(!InCond && "Can't be skipping if not in a conditional!");
448 break;
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenek56572ab2008-12-12 18:34:08 +0000451 // We have reached a '#else' or '#elif'. Lex the next token to get
452 // the directive flavor.
453 Token Tok;
454 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000455
Ted Kremenek56572ab2008-12-12 18:34:08 +0000456 // We can actually look up the IdentifierInfo here since we aren't in
457 // raw mode.
458 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
459
460 if (K == tok::pp_else) {
461 // #else: Enter the else condition. We aren't in a nested condition
462 // since we skip those. We're always in the one matching the last
463 // blocked we skipped.
464 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
465 // Note that we've seen a #else in this conditional.
466 CondInfo.FoundElse = true;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenek56572ab2008-12-12 18:34:08 +0000468 // If the #if block wasn't entered then enter the #else block now.
469 if (!CondInfo.FoundNonSkip) {
470 CondInfo.FoundNonSkip = true;
Mike Stump11289f42009-09-09 15:08:12 +0000471
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000472 // Scan until the eod token.
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000473 CurPTHLexer->ParsingPreprocessorDirective = true;
Daniel Dunbar2cba6be2009-04-13 17:57:49 +0000474 DiscardUntilEndOfDirective();
Ted Kremenek1b18ad22008-12-23 01:30:52 +0000475 CurPTHLexer->ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +0000476
Ted Kremenek56572ab2008-12-12 18:34:08 +0000477 break;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek56572ab2008-12-12 18:34:08 +0000480 // Otherwise skip this block.
481 continue;
482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenek56572ab2008-12-12 18:34:08 +0000484 assert(K == tok::pp_elif);
485 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
486
487 // If this is a #elif with a #else before it, report the error.
488 if (CondInfo.FoundElse)
489 Diag(Tok, diag::pp_err_elif_after_else);
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenek56572ab2008-12-12 18:34:08 +0000491 // If this is in a skipping block or if we're already handled this #if
Mike Stump11289f42009-09-09 15:08:12 +0000492 // block, don't bother parsing the condition. We just skip this block.
Ted Kremenek56572ab2008-12-12 18:34:08 +0000493 if (CondInfo.FoundNonSkip)
494 continue;
495
496 // Evaluate the condition of the #elif.
Craig Topperd2d442c2014-05-17 23:10:59 +0000497 IdentifierInfo *IfNDefMacro = nullptr;
Ted Kremenek56572ab2008-12-12 18:34:08 +0000498 CurPTHLexer->ParsingPreprocessorDirective = true;
499 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
500 CurPTHLexer->ParsingPreprocessorDirective = false;
501
502 // If this condition is true, enter it!
503 if (ShouldEnter) {
504 CondInfo.FoundNonSkip = true;
505 break;
506 }
507
508 // Otherwise, skip this block and go to the next one.
509 continue;
510 }
511}
512
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000513Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) {
514 ModuleMap &ModMap = HeaderInfo.getModuleMap();
515 if (SourceMgr.isInMainFile(FilenameLoc)) {
516 if (Module *CurMod = getCurrentModule())
517 return CurMod; // Compiling a module.
518 return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
519 }
520 // Try to determine the module of the include directive.
Daniel Jasper88d86952013-12-03 20:30:36 +0000521 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
Manuel Klimek98a9a6c2014-03-19 10:22:36 +0000522 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(FilenameLoc));
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000523 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
524 // The include comes from a file.
525 return ModMap.findModuleForHeader(EntryOfIncl).getModule();
526 } else {
527 // The include does not come from a file,
528 // so it is probably a module compilation.
529 return getCurrentModule();
530 }
531}
532
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000533const FileEntry *Preprocessor::LookupFile(
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000534 SourceLocation FilenameLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000535 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000536 bool isAngled,
537 const DirectoryLookup *FromDir,
538 const DirectoryLookup *&CurDir,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000539 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000540 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000541 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000542 bool SkipCache) {
Will Wilson0fafd342013-12-27 19:46:16 +0000543 // If the header lookup mechanism may be relative to the current inclusion
544 // stack, record the parent #includes.
545 SmallVector<const FileEntry *, 16> Includers;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000546 if (!FromDir) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000547 FileID FID = getCurrentFileLexer()->getFileID();
Will Wilson0fafd342013-12-27 19:46:16 +0000548 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattner022923a2009-02-04 19:45:07 +0000550 // If there is no file entry associated with this file, it must be the
551 // predefines buffer. Any other file is not lexed with a normal lexer, so
Douglas Gregor618e64a2010-08-08 07:49:23 +0000552 // it won't be scanned for preprocessor directives. If we have the
553 // predefines buffer, resolve #include references (which come from the
554 // -include command line argument) as if they came from the main file, this
555 // affects file lookup etc.
Will Wilson0fafd342013-12-27 19:46:16 +0000556 if (!FileEnt)
557 FileEnt = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
558
559 if (FileEnt)
560 Includers.push_back(FileEnt);
561
562 // MSVC searches the current include stack from top to bottom for
563 // headers included by quoted include directives.
564 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
Alp Tokerbfa39342014-01-14 12:51:41 +0000565 if (LangOpts.MSVCCompat && !isAngled) {
Will Wilson0fafd342013-12-27 19:46:16 +0000566 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
567 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
568 if (IsFileLexer(ISEntry))
569 if ((FileEnt = SourceMgr.getFileEntryForID(
570 ISEntry.ThePPLexer->getFileID())))
571 Includers.push_back(FileEnt);
572 }
Chris Lattner022923a2009-02-04 19:45:07 +0000573 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000574 }
Mike Stump11289f42009-09-09 15:08:12 +0000575
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000576 // Do a standard file entry lookup.
577 CurDir = CurDirLookup;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000578 const FileEntry *FE = HeaderInfo.LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000579 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
580 RelativePath, SuggestedModule, SkipCache);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000581 if (FE) {
Daniel Jasper5c77e392014-03-14 14:53:17 +0000582 if (SuggestedModule && !LangOpts.AsmPreprocessor)
Daniel Jasper92669ee2013-12-20 12:09:36 +0000583 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
584 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000585 return FE;
586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Will Wilson0fafd342013-12-27 19:46:16 +0000588 const FileEntry *CurFileEnt;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000589 // Otherwise, see if this is a subframework header. If so, this is relative
590 // to one of the headers on the #include stack. Walk the list of the current
591 // headers on the #include stack and pass them to HeaderInfo.
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000592 if (IsFileLexer()) {
Ben Langmuir71e1a642014-05-05 21:44:13 +0000593 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) {
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000594 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000595 SearchPath, RelativePath,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000596 SuggestedModule))) {
597 if (SuggestedModule && !LangOpts.AsmPreprocessor)
598 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
599 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000600 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000601 }
602 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000605 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
606 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +0000607 if (IsFileLexer(ISEntry)) {
Mike Stump11289f42009-09-09 15:08:12 +0000608 if ((CurFileEnt =
Ben Langmuir71e1a642014-05-05 21:44:13 +0000609 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000610 if ((FE = HeaderInfo.LookupSubframeworkHeader(
Douglas Gregorf5f94522013-02-08 00:10:48 +0000611 Filename, CurFileEnt, SearchPath, RelativePath,
Ben Langmuir71e1a642014-05-05 21:44:13 +0000612 SuggestedModule))) {
613 if (SuggestedModule && !LangOpts.AsmPreprocessor)
614 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
615 getModuleForLocation(FilenameLoc), FilenameLoc, Filename, FE);
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000616 return FE;
Ben Langmuir71e1a642014-05-05 21:44:13 +0000617 }
618 }
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000619 }
620 }
Mike Stump11289f42009-09-09 15:08:12 +0000621
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000622 // Otherwise, we really couldn't find the file.
Craig Topperd2d442c2014-05-17 23:10:59 +0000623 return nullptr;
Chris Lattnerf7ad82d2008-03-09 04:17:44 +0000624}
625
Chris Lattnerf64b3522008-03-09 01:54:53 +0000626
627//===----------------------------------------------------------------------===//
628// Preprocessor Directive Handling.
629//===----------------------------------------------------------------------===//
630
David Blaikied5321242012-06-06 18:52:13 +0000631class Preprocessor::ResetMacroExpansionHelper {
632public:
633 ResetMacroExpansionHelper(Preprocessor *pp)
634 : PP(pp), save(pp->DisableMacroExpansion) {
635 if (pp->MacroExpansionInDirectivesOverride)
636 pp->DisableMacroExpansion = false;
637 }
638 ~ResetMacroExpansionHelper() {
639 PP->DisableMacroExpansion = save;
640 }
641private:
642 Preprocessor *PP;
643 bool save;
644};
645
Chris Lattnerf64b3522008-03-09 01:54:53 +0000646/// HandleDirective - This callback is invoked when the lexer sees a # token
Mike Stump11289f42009-09-09 15:08:12 +0000647/// at the start of a line. This consumes the directive, modifies the
Chris Lattnerf64b3522008-03-09 01:54:53 +0000648/// lexer/preprocessor state, and advances the lexer(s) so that the next token
649/// read is the correct one.
650void Preprocessor::HandleDirective(Token &Result) {
651 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattnerf64b3522008-03-09 01:54:53 +0000653 // We just parsed a # character at the start of a line, so we're in directive
654 // mode. Tell the lexer this so any newlines we see will be converted into an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000655 // EOD token (which terminates the directive).
Ted Kremenek30cd88c2008-11-18 00:34:22 +0000656 CurPPLexer->ParsingPreprocessorDirective = true;
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000657 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
Mike Stump11289f42009-09-09 15:08:12 +0000658
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000659 bool ImmediatelyAfterTopLevelIfndef =
660 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
661 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
662
Chris Lattnerf64b3522008-03-09 01:54:53 +0000663 ++NumDirectives;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000664
Chris Lattnerf64b3522008-03-09 01:54:53 +0000665 // We are about to read a token. For the multiple-include optimization FA to
Mike Stump11289f42009-09-09 15:08:12 +0000666 // work, we have to remember if we had read any tokens *before* this
Chris Lattnerf64b3522008-03-09 01:54:53 +0000667 // pp-directive.
Chris Lattner8cf1f932009-12-14 04:54:40 +0000668 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner2d17ab72009-03-18 21:00:25 +0000670 // Save the '#' token in case we need to return it later.
671 Token SavedHash = Result;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Chris Lattnerf64b3522008-03-09 01:54:53 +0000673 // Read the next token, the directive flavor. This isn't expanded due to
674 // C99 6.10.3p8.
675 LexUnexpandedToken(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000676
Chris Lattnerf64b3522008-03-09 01:54:53 +0000677 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
678 // #define A(x) #x
679 // A(abc
680 // #warning blah
681 // def)
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000682 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
683 // not support this for #include-like directives, since that can result in
684 // terrible diagnostics, and does not work in GCC.
685 if (InMacroArgs) {
686 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
687 switch (II->getPPKeywordID()) {
688 case tok::pp_include:
689 case tok::pp_import:
690 case tok::pp_include_next:
691 case tok::pp___include_macros:
692 Diag(Result, diag::err_embedded_include) << II->getName();
693 DiscardUntilEndOfDirective();
694 return;
695 default:
696 break;
697 }
698 }
Chris Lattnerf64b3522008-03-09 01:54:53 +0000699 Diag(Result, diag::ext_embedded_directive);
Richard Smitheb3ce7c2011-12-16 22:50:01 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
David Blaikied5321242012-06-06 18:52:13 +0000702 // Temporarily enable macro expansion if set so
703 // and reset to previous state when returning from this function.
704 ResetMacroExpansionHelper helper(this);
705
Chris Lattnerf64b3522008-03-09 01:54:53 +0000706 switch (Result.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000707 case tok::eod:
Chris Lattnerf64b3522008-03-09 01:54:53 +0000708 return; // null directive.
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000709 case tok::code_completion:
710 if (CodeComplete)
711 CodeComplete->CodeCompleteDirective(
712 CurPPLexer->getConditionalStackDepth() > 0);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000713 setCodeCompletionReached();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000714 return;
Chris Lattner76e68962009-01-26 06:19:46 +0000715 case tok::numeric_constant: // # 7 GNU line marker directive.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000716 if (getLangOpts().AsmPreprocessor)
Chris Lattner5eb8ae22009-03-18 20:41:10 +0000717 break; // # 4 is not a preprocessor directive in .S files.
Chris Lattner76e68962009-01-26 06:19:46 +0000718 return HandleDigitDirective(Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000719 default:
720 IdentifierInfo *II = Result.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +0000721 if (!II) break; // Not an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000722
Chris Lattnerf64b3522008-03-09 01:54:53 +0000723 // Ask what the preprocessor keyword ID is.
724 switch (II->getPPKeywordID()) {
725 default: break;
726 // C99 6.10.1 - Conditional Inclusion.
727 case tok::pp_if:
728 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
729 case tok::pp_ifdef:
730 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
731 case tok::pp_ifndef:
732 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
733 case tok::pp_elif:
734 return HandleElifDirective(Result);
735 case tok::pp_else:
736 return HandleElseDirective(Result);
737 case tok::pp_endif:
738 return HandleEndifDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Chris Lattnerf64b3522008-03-09 01:54:53 +0000740 // C99 6.10.2 - Source File Inclusion.
741 case tok::pp_include:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000742 // Handle #include.
743 return HandleIncludeDirective(SavedHash.getLocation(), Result);
Chris Lattner14a7f392009-04-08 18:24:34 +0000744 case tok::pp___include_macros:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000745 // Handle -imacros.
746 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000747
Chris Lattnerf64b3522008-03-09 01:54:53 +0000748 // C99 6.10.3 - Macro Replacement.
749 case tok::pp_define:
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000750 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000751 case tok::pp_undef:
752 return HandleUndefDirective(Result);
753
754 // C99 6.10.4 - Line Control.
755 case tok::pp_line:
Chris Lattner100c65e2009-01-26 05:29:08 +0000756 return HandleLineDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattnerf64b3522008-03-09 01:54:53 +0000758 // C99 6.10.5 - Error Directive.
759 case tok::pp_error:
760 return HandleUserDiagnosticDirective(Result, false);
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattnerf64b3522008-03-09 01:54:53 +0000762 // C99 6.10.6 - Pragma Directive.
763 case tok::pp_pragma:
Enea Zaffanella5afb04a2013-07-20 20:09:11 +0000764 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattnerf64b3522008-03-09 01:54:53 +0000766 // GNU Extensions.
767 case tok::pp_import:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000768 return HandleImportDirective(SavedHash.getLocation(), Result);
Chris Lattnerf64b3522008-03-09 01:54:53 +0000769 case tok::pp_include_next:
Douglas Gregor796d76a2010-10-20 22:00:55 +0000770 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattnerf64b3522008-03-09 01:54:53 +0000772 case tok::pp_warning:
773 Diag(Result, diag::ext_pp_warning_directive);
774 return HandleUserDiagnosticDirective(Result, true);
775 case tok::pp_ident:
776 return HandleIdentSCCSDirective(Result);
777 case tok::pp_sccs:
778 return HandleIdentSCCSDirective(Result);
779 case tok::pp_assert:
780 //isExtension = true; // FIXME: implement #assert
781 break;
782 case tok::pp_unassert:
783 //isExtension = true; // FIXME: implement #unassert
784 break;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +0000785
Douglas Gregor663b48f2012-01-03 19:48:16 +0000786 case tok::pp___public_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000787 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000788 return HandleMacroPublicDirective(Result);
789 break;
790
Douglas Gregor663b48f2012-01-03 19:48:16 +0000791 case tok::pp___private_macro:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000792 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000793 return HandleMacroPrivateDirective(Result);
794 break;
Chris Lattnerf64b3522008-03-09 01:54:53 +0000795 }
796 break;
797 }
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattner2d17ab72009-03-18 21:00:25 +0000799 // If this is a .S file, treat unknown # directives as non-preprocessor
800 // directives. This is important because # may be a comment or introduce
801 // various pseudo-ops. Just return the # token and push back the following
802 // token to be lexed next time.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000803 if (getLangOpts().AsmPreprocessor) {
Daniel Dunbar48b4d1e2009-07-13 21:48:50 +0000804 Token *Toks = new Token[2];
Chris Lattner2d17ab72009-03-18 21:00:25 +0000805 // Return the # and the token after it.
Mike Stump11289f42009-09-09 15:08:12 +0000806 Toks[0] = SavedHash;
Chris Lattner2d17ab72009-03-18 21:00:25 +0000807 Toks[1] = Result;
Chris Lattner56f64c12011-01-06 05:01:51 +0000808
809 // If the second token is a hashhash token, then we need to translate it to
810 // unknown so the token lexer doesn't try to perform token pasting.
811 if (Result.is(tok::hashhash))
812 Toks[1].setKind(tok::unknown);
813
Chris Lattner2d17ab72009-03-18 21:00:25 +0000814 // Enter this token stream so that we re-lex the tokens. Make sure to
815 // enable macro expansion, in case the token after the # is an identifier
816 // that is expanded.
817 EnterTokenStream(Toks, 2, false, true);
818 return;
819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattnerf64b3522008-03-09 01:54:53 +0000821 // If we reached here, the preprocessing token is not valid!
822 Diag(Result, diag::err_pp_invalid_directive);
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattnerf64b3522008-03-09 01:54:53 +0000824 // Read the rest of the PP line.
825 DiscardUntilEndOfDirective();
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattnerf64b3522008-03-09 01:54:53 +0000827 // Okay, we're done parsing the directive.
828}
829
Chris Lattner76e68962009-01-26 06:19:46 +0000830/// GetLineValue - Convert a numeric token into an unsigned value, emitting
831/// Diagnostic DiagID if it is invalid, and returning the value in Val.
832static bool GetLineValue(Token &DigitTok, unsigned &Val,
Michael Ilsemane910cc82013-04-10 01:04:18 +0000833 unsigned DiagID, Preprocessor &PP,
834 bool IsGNULineDirective=false) {
Chris Lattner76e68962009-01-26 06:19:46 +0000835 if (DigitTok.isNot(tok::numeric_constant)) {
836 PP.Diag(DigitTok, DiagID);
Mike Stump11289f42009-09-09 15:08:12 +0000837
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000838 if (DigitTok.isNot(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +0000839 PP.DiscardUntilEndOfDirective();
840 return true;
841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000843 SmallString<64> IntegerBuffer;
Chris Lattner76e68962009-01-26 06:19:46 +0000844 IntegerBuffer.resize(DigitTok.getLength());
845 const char *DigitTokBegin = &IntegerBuffer[0];
Douglas Gregordc970f02010-03-16 22:30:13 +0000846 bool Invalid = false;
847 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
848 if (Invalid)
849 return true;
850
Chris Lattnerd66f1722009-04-18 18:35:15 +0000851 // Verify that we have a simple digit-sequence, and compute the value. This
852 // is always a simple digit string computed in decimal, so we do this manually
853 // here.
854 Val = 0;
855 for (unsigned i = 0; i != ActualLength; ++i) {
Richard Smith7f2707a2013-09-26 18:13:20 +0000856 // C++1y [lex.fcon]p1:
857 // Optional separating single quotes in a digit-sequence are ignored
858 if (DigitTokBegin[i] == '\'')
859 continue;
860
Jordan Rosea7d03842013-02-08 22:30:41 +0000861 if (!isDigit(DigitTokBegin[i])) {
Chris Lattnerd66f1722009-04-18 18:35:15 +0000862 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
Michael Ilsemane910cc82013-04-10 01:04:18 +0000863 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000864 PP.DiscardUntilEndOfDirective();
865 return true;
866 }
Mike Stump11289f42009-09-09 15:08:12 +0000867
Chris Lattnerd66f1722009-04-18 18:35:15 +0000868 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
869 if (NextVal < Val) { // overflow.
870 PP.Diag(DigitTok, DiagID);
871 PP.DiscardUntilEndOfDirective();
872 return true;
873 }
874 Val = NextVal;
Chris Lattner76e68962009-01-26 06:19:46 +0000875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000877 if (DigitTokBegin[0] == '0' && Val)
Michael Ilsemane910cc82013-04-10 01:04:18 +0000878 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
879 << IsGNULineDirective;
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner76e68962009-01-26 06:19:46 +0000881 return false;
882}
883
James Dennettf6333ac2012-06-22 05:46:07 +0000884/// \brief Handle a \#line directive: C99 6.10.4.
885///
886/// The two acceptable forms are:
887/// \verbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000888/// # line digit-sequence
889/// # line digit-sequence "s-char-sequence"
James Dennettf6333ac2012-06-22 05:46:07 +0000890/// \endverbatim
Chris Lattner100c65e2009-01-26 05:29:08 +0000891void Preprocessor::HandleLineDirective(Token &Tok) {
892 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
893 // expanded.
894 Token DigitTok;
895 Lex(DigitTok);
896
Chris Lattner100c65e2009-01-26 05:29:08 +0000897 // Validate the number and convert it to an unsigned.
Chris Lattner76e68962009-01-26 06:19:46 +0000898 unsigned LineNo;
Chris Lattnerd66f1722009-04-18 18:35:15 +0000899 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
Chris Lattner100c65e2009-01-26 05:29:08 +0000900 return;
Fariborz Jahanian0638c152012-06-26 21:19:20 +0000901
902 if (LineNo == 0)
903 Diag(DigitTok, diag::ext_pp_line_zero);
Chris Lattner100c65e2009-01-26 05:29:08 +0000904
Chris Lattner76e68962009-01-26 06:19:46 +0000905 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
906 // number greater than 2147483647". C90 requires that the line # be <= 32767.
Eli Friedman192e0342011-10-10 23:35:28 +0000907 unsigned LineLimit = 32768U;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000908 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Eli Friedman192e0342011-10-10 23:35:28 +0000909 LineLimit = 2147483648U;
Chris Lattner100c65e2009-01-26 05:29:08 +0000910 if (LineNo >= LineLimit)
911 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000912 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
Richard Smithacd4d3d2011-10-15 01:18:56 +0000913 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
Mike Stump11289f42009-09-09 15:08:12 +0000914
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000915 int FilenameID = -1;
Chris Lattner100c65e2009-01-26 05:29:08 +0000916 Token StrTok;
917 Lex(StrTok);
918
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000919 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
920 // string followed by eod.
921 if (StrTok.is(tok::eod))
Chris Lattner100c65e2009-01-26 05:29:08 +0000922 ; // ok
923 else if (StrTok.isNot(tok::string_literal)) {
924 Diag(StrTok, diag::err_pp_line_invalid_filename);
Richard Smithd67aea22012-03-06 03:21:47 +0000925 return DiscardUntilEndOfDirective();
926 } else if (StrTok.hasUDSuffix()) {
927 Diag(StrTok, diag::err_invalid_string_udl);
928 return DiscardUntilEndOfDirective();
Chris Lattner100c65e2009-01-26 05:29:08 +0000929 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000930 // Parse and validate the string, converting it into a unique ID.
Craig Topper9d5583e2014-06-26 04:58:39 +0000931 StringLiteralParser Literal(StrTok, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +0000932 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +0000933 if (Literal.hadError)
934 return DiscardUntilEndOfDirective();
935 if (Literal.Pascal) {
936 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
937 return DiscardUntilEndOfDirective();
938 }
Jay Foad9a6b0982011-06-21 15:13:30 +0000939 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +0000940
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000941 // Verify that there is nothing after the string, other than EOD. Because
Chris Lattner0003c272009-04-17 23:30:53 +0000942 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
943 CheckEndOfDirective("line", true);
Chris Lattner100c65e2009-01-26 05:29:08 +0000944 }
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattner1eaa70a2009-02-03 21:52:55 +0000946 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
Mike Stump11289f42009-09-09 15:08:12 +0000947
Chris Lattner839150e2009-03-27 17:13:49 +0000948 if (Callbacks)
Chris Lattnerc745cec2010-04-14 04:28:50 +0000949 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
950 PPCallbacks::RenameFile,
Chris Lattner839150e2009-03-27 17:13:49 +0000951 SrcMgr::C_User);
Chris Lattner100c65e2009-01-26 05:29:08 +0000952}
953
Chris Lattner76e68962009-01-26 06:19:46 +0000954/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
955/// marker directive.
956static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
957 bool &IsSystemHeader, bool &IsExternCHeader,
958 Preprocessor &PP) {
959 unsigned FlagVal;
960 Token FlagTok;
961 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000962 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000963 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
964 return true;
965
966 if (FlagVal == 1) {
967 IsFileEntry = true;
Mike Stump11289f42009-09-09 15:08:12 +0000968
Chris Lattner76e68962009-01-26 06:19:46 +0000969 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000970 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000971 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
972 return true;
973 } else if (FlagVal == 2) {
974 IsFileExit = true;
Mike Stump11289f42009-09-09 15:08:12 +0000975
Chris Lattner1c967782009-02-04 06:25:26 +0000976 SourceManager &SM = PP.getSourceManager();
977 // If we are leaving the current presumed file, check to make sure the
978 // presumed include stack isn't empty!
979 FileID CurFileID =
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000980 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
Chris Lattner1c967782009-02-04 06:25:26 +0000981 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
Douglas Gregor453b0122010-11-12 07:15:47 +0000982 if (PLoc.isInvalid())
983 return true;
984
Chris Lattner1c967782009-02-04 06:25:26 +0000985 // If there is no include loc (main file) or if the include loc is in a
986 // different physical file, then we aren't in a "1" line marker flag region.
987 SourceLocation IncLoc = PLoc.getIncludeLoc();
988 if (IncLoc.isInvalid() ||
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000989 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
Chris Lattner1c967782009-02-04 06:25:26 +0000990 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
991 PP.DiscardUntilEndOfDirective();
992 return true;
993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Chris Lattner76e68962009-01-26 06:19:46 +0000995 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000996 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +0000997 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
998 return true;
999 }
1000
1001 // We must have 3 if there are still flags.
1002 if (FlagVal != 3) {
1003 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001004 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001005 return true;
1006 }
Mike Stump11289f42009-09-09 15:08:12 +00001007
Chris Lattner76e68962009-01-26 06:19:46 +00001008 IsSystemHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner76e68962009-01-26 06:19:46 +00001010 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001011 if (FlagTok.is(tok::eod)) return false;
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001012 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
Chris Lattner76e68962009-01-26 06:19:46 +00001013 return true;
1014
1015 // We must have 4 if there is yet another flag.
1016 if (FlagVal != 4) {
1017 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001018 PP.DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001019 return true;
1020 }
Mike Stump11289f42009-09-09 15:08:12 +00001021
Chris Lattner76e68962009-01-26 06:19:46 +00001022 IsExternCHeader = true;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner76e68962009-01-26 06:19:46 +00001024 PP.Lex(FlagTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001025 if (FlagTok.is(tok::eod)) return false;
Chris Lattner76e68962009-01-26 06:19:46 +00001026
1027 // There are no more valid flags here.
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}
1032
1033/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1034/// one of the following forms:
1035///
1036/// # 42
Mike Stump11289f42009-09-09 15:08:12 +00001037/// # 42 "file" ('1' | '2')?
Chris Lattner76e68962009-01-26 06:19:46 +00001038/// # 42 "file" ('1' | '2')? '3' '4'?
1039///
1040void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1041 // Validate the number and convert it to an unsigned. GNU does not have a
1042 // line # limit other than it fit in 32-bits.
1043 unsigned LineNo;
1044 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
Michael Ilsemane910cc82013-04-10 01:04:18 +00001045 *this, true))
Chris Lattner76e68962009-01-26 06:19:46 +00001046 return;
Mike Stump11289f42009-09-09 15:08:12 +00001047
Chris Lattner76e68962009-01-26 06:19:46 +00001048 Token StrTok;
1049 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner76e68962009-01-26 06:19:46 +00001051 bool IsFileEntry = false, IsFileExit = false;
1052 bool IsSystemHeader = false, IsExternCHeader = false;
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001053 int FilenameID = -1;
1054
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001055 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1056 // string followed by eod.
1057 if (StrTok.is(tok::eod))
Chris Lattner76e68962009-01-26 06:19:46 +00001058 ; // ok
1059 else if (StrTok.isNot(tok::string_literal)) {
1060 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001061 return DiscardUntilEndOfDirective();
Richard Smithd67aea22012-03-06 03:21:47 +00001062 } else if (StrTok.hasUDSuffix()) {
1063 Diag(StrTok, diag::err_invalid_string_udl);
1064 return DiscardUntilEndOfDirective();
Chris Lattner76e68962009-01-26 06:19:46 +00001065 } else {
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001066 // Parse and validate the string, converting it into a unique ID.
Craig Topper9d5583e2014-06-26 04:58:39 +00001067 StringLiteralParser Literal(StrTok, *this);
Douglas Gregorfb65e592011-07-27 05:40:30 +00001068 assert(Literal.isAscii() && "Didn't allow wide strings in");
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001069 if (Literal.hadError)
1070 return DiscardUntilEndOfDirective();
1071 if (Literal.Pascal) {
1072 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1073 return DiscardUntilEndOfDirective();
1074 }
Jay Foad9a6b0982011-06-21 15:13:30 +00001075 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
Mike Stump11289f42009-09-09 15:08:12 +00001076
Chris Lattner76e68962009-01-26 06:19:46 +00001077 // If a filename was present, read any flags that are present.
Mike Stump11289f42009-09-09 15:08:12 +00001078 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
Chris Lattnerb5fba6f2009-01-26 07:57:50 +00001079 IsSystemHeader, IsExternCHeader, *this))
Chris Lattner76e68962009-01-26 06:19:46 +00001080 return;
Chris Lattner76e68962009-01-26 06:19:46 +00001081 }
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001083 // Create a line note with this information.
1084 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
Mike Stump11289f42009-09-09 15:08:12 +00001085 IsFileEntry, IsFileExit,
Chris Lattner0a1a8d82009-02-04 05:21:58 +00001086 IsSystemHeader, IsExternCHeader);
Mike Stump11289f42009-09-09 15:08:12 +00001087
Chris Lattner839150e2009-03-27 17:13:49 +00001088 // If the preprocessor has callbacks installed, notify them of the #line
1089 // change. This is used so that the line marker comes out in -E mode for
1090 // example.
1091 if (Callbacks) {
1092 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1093 if (IsFileEntry)
1094 Reason = PPCallbacks::EnterFile;
1095 else if (IsFileExit)
1096 Reason = PPCallbacks::ExitFile;
1097 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1098 if (IsExternCHeader)
1099 FileKind = SrcMgr::C_ExternCSystem;
1100 else if (IsSystemHeader)
1101 FileKind = SrcMgr::C_System;
Mike Stump11289f42009-09-09 15:08:12 +00001102
Chris Lattnerc745cec2010-04-14 04:28:50 +00001103 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
Chris Lattner839150e2009-03-27 17:13:49 +00001104 }
Chris Lattner76e68962009-01-26 06:19:46 +00001105}
1106
1107
Chris Lattner38d7fd22009-01-26 05:30:54 +00001108/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1109///
Mike Stump11289f42009-09-09 15:08:12 +00001110void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001111 bool isWarning) {
Chris Lattner38d7fd22009-01-26 05:30:54 +00001112 // PTH doesn't emit #warning or #error directives.
1113 if (CurPTHLexer)
Chris Lattner100c65e2009-01-26 05:29:08 +00001114 return CurPTHLexer->DiscardToEndOfLine();
1115
Chris Lattnerf64b3522008-03-09 01:54:53 +00001116 // Read the rest of the line raw. We do this because we don't want macros
1117 // to be expanded and we don't require that the tokens be valid preprocessing
1118 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1119 // collapse multiple consequtive white space between tokens, but this isn't
1120 // specified by the standard.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001121 SmallString<128> Message;
1122 CurLexer->ReadToEndOfLine(&Message);
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001123
1124 // Find the first non-whitespace character, so that we can make the
1125 // diagnostic more succinct.
Benjamin Kramere5fbc6c2012-05-18 19:32:16 +00001126 StringRef Msg = Message.str().ltrim(" ");
1127
Chris Lattner100c65e2009-01-26 05:29:08 +00001128 if (isWarning)
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001129 Diag(Tok, diag::pp_hash_warning) << Msg;
Chris Lattner100c65e2009-01-26 05:29:08 +00001130 else
Ted Kremenek7f4bd162012-02-02 00:16:13 +00001131 Diag(Tok, diag::err_pp_hash_error) << Msg;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001132}
1133
1134/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1135///
1136void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1137 // Yes, this directive is an extension.
1138 Diag(Tok, diag::ext_pp_ident_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001139
Chris Lattnerf64b3522008-03-09 01:54:53 +00001140 // Read the string argument.
1141 Token StrTok;
1142 Lex(StrTok);
Mike Stump11289f42009-09-09 15:08:12 +00001143
Chris Lattnerf64b3522008-03-09 01:54:53 +00001144 // If the token kind isn't a string, it's a malformed directive.
1145 if (StrTok.isNot(tok::string_literal) &&
Chris Lattner907dfe92008-11-18 07:59:24 +00001146 StrTok.isNot(tok::wide_string_literal)) {
1147 Diag(StrTok, diag::err_pp_malformed_ident);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001148 if (StrTok.isNot(tok::eod))
Chris Lattner38d7fd22009-01-26 05:30:54 +00001149 DiscardUntilEndOfDirective();
Chris Lattner907dfe92008-11-18 07:59:24 +00001150 return;
1151 }
Mike Stump11289f42009-09-09 15:08:12 +00001152
Richard Smithd67aea22012-03-06 03:21:47 +00001153 if (StrTok.hasUDSuffix()) {
1154 Diag(StrTok, diag::err_invalid_string_udl);
1155 return DiscardUntilEndOfDirective();
1156 }
1157
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001158 // Verify that there is nothing after the string, other than EOD.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00001159 CheckEndOfDirective("ident");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001160
Douglas Gregordc970f02010-03-16 22:30:13 +00001161 if (Callbacks) {
1162 bool Invalid = false;
1163 std::string Str = getSpelling(StrTok, &Invalid);
1164 if (!Invalid)
1165 Callbacks->Ident(Tok.getLocation(), Str);
1166 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001167}
1168
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001169/// \brief Handle a #public directive.
1170void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001171 Token MacroNameTok;
1172 ReadMacroName(MacroNameTok, 2);
1173
1174 // Error reading macro name? If so, diagnostic already issued.
1175 if (MacroNameTok.is(tok::eod))
1176 return;
1177
Douglas Gregor663b48f2012-01-03 19:48:16 +00001178 // Check to see if this is the last token on the #__public_macro line.
1179 CheckEndOfDirective("__public_macro");
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001180
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001181 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001182 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001183 MacroDirective *MD = getMacroDirective(II);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001184
1185 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001186 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001187 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001188 return;
1189 }
1190
1191 // Note that this macro has now been exported.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001192 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1193 MacroNameTok.getLocation(), /*IsPublic=*/true));
Douglas Gregorebf00492011-10-17 15:32:29 +00001194}
1195
Douglas Gregor0bf886d2012-01-03 18:24:14 +00001196/// \brief Handle a #private directive.
Douglas Gregorebf00492011-10-17 15:32:29 +00001197void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1198 Token MacroNameTok;
1199 ReadMacroName(MacroNameTok, 2);
1200
1201 // Error reading macro name? If so, diagnostic already issued.
1202 if (MacroNameTok.is(tok::eod))
1203 return;
1204
Douglas Gregor663b48f2012-01-03 19:48:16 +00001205 // Check to see if this is the last token on the #__private_macro line.
1206 CheckEndOfDirective("__private_macro");
Douglas Gregorebf00492011-10-17 15:32:29 +00001207
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001208 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
Douglas Gregorebf00492011-10-17 15:32:29 +00001209 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001210 MacroDirective *MD = getMacroDirective(II);
Douglas Gregorebf00492011-10-17 15:32:29 +00001211
1212 // If the macro is not defined, this is an error.
Craig Topperd2d442c2014-05-17 23:10:59 +00001213 if (!MD) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001214 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
Douglas Gregorebf00492011-10-17 15:32:29 +00001215 return;
1216 }
1217
1218 // Note that this macro has now been marked private.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001219 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1220 MacroNameTok.getLocation(), /*IsPublic=*/false));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001221}
1222
Chris Lattnerf64b3522008-03-09 01:54:53 +00001223//===----------------------------------------------------------------------===//
1224// Preprocessor Include Directive Handling.
1225//===----------------------------------------------------------------------===//
1226
1227/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
James Dennettf6333ac2012-06-22 05:46:07 +00001228/// checked and spelled filename, e.g. as an operand of \#include. This returns
Chris Lattnerf64b3522008-03-09 01:54:53 +00001229/// true if the input filename was in <>'s or false if it were in ""'s. The
1230/// caller is expected to provide a buffer that is large enough to hold the
1231/// spelling of the filename, but is also expected to handle the case when
1232/// this method decides to use a different buffer.
1233bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001234 StringRef &Buffer) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001235 // Get the text form of the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001236 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
Mike Stump11289f42009-09-09 15:08:12 +00001237
Chris Lattnerf64b3522008-03-09 01:54:53 +00001238 // Make sure the filename is <x> or "x".
1239 bool isAngled;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001240 if (Buffer[0] == '<') {
1241 if (Buffer.back() != '>') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001242 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001243 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001244 return true;
1245 }
1246 isAngled = true;
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001247 } else if (Buffer[0] == '"') {
1248 if (Buffer.back() != '"') {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001249 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001250 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001251 return true;
1252 }
1253 isAngled = false;
1254 } else {
1255 Diag(Loc, diag::err_pp_expects_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001256 Buffer = StringRef();
Chris Lattnerf64b3522008-03-09 01:54:53 +00001257 return true;
1258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Chris Lattnerf64b3522008-03-09 01:54:53 +00001260 // Diagnose #include "" as invalid.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001261 if (Buffer.size() <= 2) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001262 Diag(Loc, diag::err_pp_empty_filename);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001263 Buffer = StringRef();
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001264 return true;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001265 }
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattnerf64b3522008-03-09 01:54:53 +00001267 // Skip the brackets.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001268 Buffer = Buffer.substr(1, Buffer.size()-2);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001269 return isAngled;
1270}
1271
James Dennett4a4f72d2013-11-27 01:27:40 +00001272// \brief Handle cases where the \#include name is expanded from a macro
1273// as multiple tokens, which need to be glued together.
1274//
1275// This occurs for code like:
1276// \code
1277// \#define FOO <a/b.h>
1278// \#include FOO
1279// \endcode
1280// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1281//
1282// This code concatenates and consumes tokens up to the '>' token. It returns
1283// false if the > was found, otherwise it returns true if it finds and consumes
1284// the EOD marker.
1285bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001286 SourceLocation &End) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001287 Token CurTok;
Mike Stump11289f42009-09-09 15:08:12 +00001288
John Thompsonb5353522009-10-30 13:49:06 +00001289 Lex(CurTok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001290 while (CurTok.isNot(tok::eod)) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001291 End = CurTok.getLocation();
1292
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001293 // FIXME: Provide code completion for #includes.
1294 if (CurTok.is(tok::code_completion)) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001295 setCodeCompletionReached();
Douglas Gregor9c7bd2f2010-12-09 23:35:36 +00001296 Lex(CurTok);
1297 continue;
1298 }
1299
Chris Lattnerf64b3522008-03-09 01:54:53 +00001300 // Append the spelling of this token to the buffer. If there was a space
1301 // before it, add it now.
1302 if (CurTok.hasLeadingSpace())
1303 FilenameBuffer.push_back(' ');
Mike Stump11289f42009-09-09 15:08:12 +00001304
Chris Lattnerf64b3522008-03-09 01:54:53 +00001305 // Get the spelling of the token, directly into FilenameBuffer if possible.
1306 unsigned PreAppendSize = FilenameBuffer.size();
1307 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattnerf64b3522008-03-09 01:54:53 +00001309 const char *BufPtr = &FilenameBuffer[PreAppendSize];
John Thompsonb5353522009-10-30 13:49:06 +00001310 unsigned ActualLen = getSpelling(CurTok, BufPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001311
Chris Lattnerf64b3522008-03-09 01:54:53 +00001312 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1313 if (BufPtr != &FilenameBuffer[PreAppendSize])
1314 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001315
Chris Lattnerf64b3522008-03-09 01:54:53 +00001316 // Resize FilenameBuffer to the correct size.
1317 if (CurTok.getLength() != ActualLen)
1318 FilenameBuffer.resize(PreAppendSize+ActualLen);
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattnerf64b3522008-03-09 01:54:53 +00001320 // If we found the '>' marker, return success.
1321 if (CurTok.is(tok::greater))
1322 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001323
John Thompsonb5353522009-10-30 13:49:06 +00001324 Lex(CurTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001325 }
1326
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001327 // If we hit the eod marker, emit an error and return true so that the caller
1328 // knows the EOD has been read.
John Thompsonb5353522009-10-30 13:49:06 +00001329 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001330 return true;
1331}
1332
Richard Smith34f30512013-11-23 04:06:09 +00001333/// \brief Push a token onto the token stream containing an annotation.
1334static void EnterAnnotationToken(Preprocessor &PP,
1335 SourceLocation Begin, SourceLocation End,
1336 tok::TokenKind Kind, void *AnnotationVal) {
1337 Token *Tok = new Token[1];
1338 Tok[0].startToken();
1339 Tok[0].setKind(Kind);
1340 Tok[0].setLocation(Begin);
1341 Tok[0].setAnnotationEndLoc(End);
1342 Tok[0].setAnnotationValue(AnnotationVal);
1343 PP.EnterTokenStream(Tok, 1, true, true);
1344}
1345
James Dennettf6333ac2012-06-22 05:46:07 +00001346/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1347/// the file to be included from the lexer, then include it! This is a common
1348/// routine with functionality shared between \#include, \#include_next and
1349/// \#import. LookupFrom is set when this is a \#include_next directive, it
Mike Stump11289f42009-09-09 15:08:12 +00001350/// specifies the file to start searching from.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001351void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1352 Token &IncludeTok,
Chris Lattnerf64b3522008-03-09 01:54:53 +00001353 const DirectoryLookup *LookupFrom,
1354 bool isImport) {
1355
1356 Token FilenameTok;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001357 CurPPLexer->LexIncludeFilename(FilenameTok);
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattnerf64b3522008-03-09 01:54:53 +00001359 // Reserve a buffer to get the spelling.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001360 SmallString<128> FilenameBuffer;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001361 StringRef Filename;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001362 SourceLocation End;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001363 SourceLocation CharEnd; // the end of this directive, in characters
Douglas Gregor796d76a2010-10-20 22:00:55 +00001364
Chris Lattnerf64b3522008-03-09 01:54:53 +00001365 switch (FilenameTok.getKind()) {
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001366 case tok::eod:
1367 // If the token kind is EOD, the error has already been diagnosed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001368 return;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattnerf64b3522008-03-09 01:54:53 +00001370 case tok::angle_string_literal:
Benjamin Kramer0a1abd42010-02-27 13:44:12 +00001371 case tok::string_literal:
1372 Filename = getSpelling(FilenameTok, FilenameBuffer);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001373 End = FilenameTok.getLocation();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001374 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
Chris Lattnerf64b3522008-03-09 01:54:53 +00001375 break;
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattnerf64b3522008-03-09 01:54:53 +00001377 case tok::less:
1378 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1379 // case, glue the tokens together into FilenameBuffer and interpret those.
1380 FilenameBuffer.push_back('<');
Douglas Gregor796d76a2010-10-20 22:00:55 +00001381 if (ConcatenateIncludeName(FilenameBuffer, End))
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001382 return; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001383 Filename = FilenameBuffer.str();
Argyrios Kyrtzidis2edbc862012-11-01 17:52:58 +00001384 CharEnd = End.getLocWithOffset(1);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001385 break;
1386 default:
1387 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1388 DiscardUntilEndOfDirective();
1389 return;
1390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001392 CharSourceRange FilenameRange
1393 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
Aaron Ballman611306e2012-03-02 22:51:54 +00001394 StringRef OriginalFilename = Filename;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001395 bool isAngled =
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001396 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001397 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1398 // error.
Chris Lattnerd081f8c2010-01-10 01:35:12 +00001399 if (Filename.empty()) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001400 DiscardUntilEndOfDirective();
1401 return;
1402 }
Mike Stump11289f42009-09-09 15:08:12 +00001403
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001404 // Verify that there is nothing after the filename, other than EOD. Note that
Chris Lattnerb40289b2009-04-17 23:56:52 +00001405 // we allow macros that expand to nothing after the filename, because this
1406 // falls into the category of "#include pp-tokens new-line" specified in
1407 // C99 6.10.2p4.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001408 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001409
1410 // Check that we don't have infinite #include recursion.
Chris Lattner907dfe92008-11-18 07:59:24 +00001411 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1412 Diag(FilenameTok, diag::err_pp_include_too_deep);
1413 return;
1414 }
Mike Stump11289f42009-09-09 15:08:12 +00001415
John McCall32f5fe12011-09-30 05:12:12 +00001416 // Complain about attempts to #include files in an audit pragma.
1417 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1418 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1419 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1420
1421 // Immediately leave the pragma.
1422 PragmaARCCFCodeAuditedLoc = SourceLocation();
1423 }
1424
Aaron Ballman611306e2012-03-02 22:51:54 +00001425 if (HeaderInfo.HasIncludeAliasMap()) {
1426 // Map the filename with the brackets still attached. If the name doesn't
1427 // map to anything, fall back on the filename we've already gotten the
1428 // spelling for.
1429 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1430 if (!NewName.empty())
1431 Filename = NewName;
1432 }
1433
Chris Lattnerf64b3522008-03-09 01:54:53 +00001434 // Search include directories.
1435 const DirectoryLookup *CurDir;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001436 SmallString<1024> SearchPath;
1437 SmallString<1024> RelativePath;
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001438 // We get the raw path only if we have 'Callbacks' to which we later pass
1439 // the path.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001440 ModuleMap::KnownHeader SuggestedModule;
1441 SourceLocation FilenameLoc = FilenameTok.getLocation();
Saleem Abdulrasool729b7d32014-03-12 02:26:08 +00001442 SmallString<128> NormalizedPath;
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001443 if (LangOpts.MSVCCompat) {
1444 NormalizedPath = Filename.str();
1445 llvm::sys::fs::normalize_separators(NormalizedPath);
1446 }
Chandler Carruth3cc331a2011-03-16 18:34:36 +00001447 const FileEntry *File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001448 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001449 isAngled, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1450 Callbacks ? &RelativePath : nullptr,
1451 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule : nullptr);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001452
Douglas Gregor11729f02011-11-30 18:12:06 +00001453 if (Callbacks) {
1454 if (!File) {
1455 // Give the clients a chance to recover.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001456 SmallString<128> RecoveryPath;
Douglas Gregor11729f02011-11-30 18:12:06 +00001457 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1458 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1459 // Add the recovery path to the list of search paths.
Daniel Dunbarae4feb62013-01-25 01:50:28 +00001460 DirectoryLookup DL(DE, SrcMgr::C_User, false);
Douglas Gregor11729f02011-11-30 18:12:06 +00001461 HeaderInfo.AddSearchPath(DL, isAngled);
1462
1463 // Try the lookup again, skipping the cache.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001464 File = LookupFile(FilenameLoc,
1465 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1466 : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001467 isAngled, LookupFrom, CurDir, nullptr, nullptr,
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001468 HeaderInfo.getHeaderSearchOpts().ModuleMaps
Daniel Jasper07e6c402013-08-05 20:26:17 +00001469 ? &SuggestedModule
Craig Topperd2d442c2014-05-17 23:10:59 +00001470 : nullptr,
Daniel Jasper07e6c402013-08-05 20:26:17 +00001471 /*SkipCache*/ true);
Douglas Gregor11729f02011-11-30 18:12:06 +00001472 }
1473 }
1474 }
1475
Daniel Jasper07e6c402013-08-05 20:26:17 +00001476 if (!SuggestedModule || !getLangOpts().Modules) {
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001477 // Notify the callback object that we've seen an inclusion directive.
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001478 Callbacks->InclusionDirective(HashLoc, IncludeTok,
1479 LangOpts.MSVCCompat ? NormalizedPath.c_str()
1480 : Filename,
1481 isAngled, FilenameRange, File, SearchPath,
Craig Topperd2d442c2014-05-17 23:10:59 +00001482 RelativePath, /*ImportedModule=*/nullptr);
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001483 }
Douglas Gregor11729f02011-11-30 18:12:06 +00001484 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001485
1486 if (!File) {
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001487 if (!SuppressIncludeNotFoundError) {
1488 // If the file could not be located and it was included via angle
1489 // brackets, we can attempt a lookup as though it were a quoted path to
1490 // provide the user with a possible fixit.
1491 if (isAngled) {
Daniel Jasper07e6c402013-08-05 20:26:17 +00001492 File = LookupFile(
Saleem Abdulrasool19803412014-03-11 22:41:45 +00001493 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
Craig Topperd2d442c2014-05-17 23:10:59 +00001494 false, LookupFrom, CurDir, Callbacks ? &SearchPath : nullptr,
1495 Callbacks ? &RelativePath : nullptr,
1496 HeaderInfo.getHeaderSearchOpts().ModuleMaps ? &SuggestedModule
1497 : nullptr);
Aaron Ballman8f94ac62012-07-17 23:19:16 +00001498 if (File) {
1499 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1500 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1501 Filename <<
1502 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1503 }
1504 }
1505 // If the file is still not found, just go with the vanilla diagnostic
1506 if (!File)
1507 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1508 }
1509 if (!File)
1510 return;
Douglas Gregor11729f02011-11-30 18:12:06 +00001511 }
1512
Douglas Gregor97eec242011-09-15 22:00:41 +00001513 // If we are supposed to import a module rather than including the header,
1514 // do so now.
Ben Langmuirb537a3a2014-07-23 15:30:23 +00001515 if (SuggestedModule && getLangOpts().Modules &&
1516 SuggestedModule.getModule()->getTopLevelModuleName() !=
1517 getLangOpts().ImplementationOfModule) {
Douglas Gregor71944202011-11-30 00:36:36 +00001518 // Compute the module access path corresponding to this module.
1519 // FIXME: Should we have a second loadModule() overload to avoid this
1520 // extra lookup step?
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001521 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001522 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
Douglas Gregor71944202011-11-30 00:36:36 +00001523 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1524 FilenameTok.getLocation()));
1525 std::reverse(Path.begin(), Path.end());
1526
Douglas Gregor41e115a2011-11-30 18:02:36 +00001527 // Warn that we're replacing the include/import with a module import.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001528 SmallString<128> PathString;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001529 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1530 if (I)
1531 PathString += '.';
1532 PathString += Path[I].first->getName();
1533 }
1534 int IncludeKind = 0;
1535
1536 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1537 case tok::pp_include:
1538 IncludeKind = 0;
1539 break;
1540
1541 case tok::pp_import:
1542 IncludeKind = 1;
1543 break;
1544
Douglas Gregor4401fbe2011-11-30 18:03:26 +00001545 case tok::pp_include_next:
1546 IncludeKind = 2;
1547 break;
Douglas Gregor41e115a2011-11-30 18:02:36 +00001548
1549 case tok::pp___include_macros:
1550 IncludeKind = 3;
1551 break;
1552
1553 default:
1554 llvm_unreachable("unknown include directive kind");
Douglas Gregor41e115a2011-11-30 18:02:36 +00001555 }
1556
Douglas Gregor2537a362011-12-08 17:01:29 +00001557 // Determine whether we are actually building the module that this
1558 // include directive maps to.
1559 bool BuildingImportedModule
David Blaikiebbafb8a2012-03-11 07:00:24 +00001560 = Path[0].first->getName() == getLangOpts().CurrentModule;
Richard Smith34f30512013-11-23 04:06:09 +00001561
David Blaikiebbafb8a2012-03-11 07:00:24 +00001562 if (!BuildingImportedModule && getLangOpts().ObjC2) {
Douglas Gregor2537a362011-12-08 17:01:29 +00001563 // If we're not building the imported module, warn that we're going
1564 // to automatically turn this inclusion directive into a module import.
Douglas Gregorda82e702012-01-03 19:32:59 +00001565 // We only do this in Objective-C, where we have a module-import syntax.
Douglas Gregor2537a362011-12-08 17:01:29 +00001566 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd),
1567 /*IsTokenRange=*/false);
1568 Diag(HashLoc, diag::warn_auto_module_import)
1569 << IncludeKind << PathString
1570 << FixItHint::CreateReplacement(ReplaceRange,
Douglas Gregorc50d4922012-12-11 22:11:52 +00001571 "@import " + PathString.str().str() + ";");
Douglas Gregor2537a362011-12-08 17:01:29 +00001572 }
Douglas Gregor41e115a2011-11-30 18:02:36 +00001573
Richard Smithce587f52013-11-15 04:24:58 +00001574 // Load the module. Only make macros visible. We'll make the declarations
1575 // visible when the parser gets here.
1576 Module::NameVisibilityKind Visibility = Module::MacrosVisible;
Douglas Gregor7a626572012-11-29 23:55:25 +00001577 ModuleLoadResult Imported
Douglas Gregor98a52db2011-12-20 00:28:52 +00001578 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility,
1579 /*IsIncludeDirective=*/true);
Craig Topperd2d442c2014-05-17 23:10:59 +00001580 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
Argyrios Kyrtzidis051b4432012-09-29 01:06:01 +00001581 "the imported module is different than the suggested one");
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00001582
1583 if (!Imported && hadModuleLoaderFatalFailure()) {
1584 // With a fatal failure in the module loader, we abort parsing.
1585 Token &Result = IncludeTok;
1586 if (CurLexer) {
1587 Result.startToken();
1588 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1589 CurLexer->cutOffLexing();
1590 } else {
1591 assert(CurPTHLexer && "#include but no current lexer set!");
1592 CurPTHLexer->getEOF(Result);
1593 }
1594 return;
1595 }
Richard Smithce587f52013-11-15 04:24:58 +00001596
Douglas Gregor2537a362011-12-08 17:01:29 +00001597 // If this header isn't part of the module we're building, we're done.
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001598 if (!BuildingImportedModule && Imported) {
1599 if (Callbacks) {
1600 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1601 FilenameRange, File,
1602 SearchPath, RelativePath, Imported);
1603 }
Richard Smithce587f52013-11-15 04:24:58 +00001604
1605 if (IncludeKind != 3) {
1606 // Let the parser know that we hit a module import, and it should
1607 // make the module visible.
1608 // FIXME: Produce this as the current token directly, rather than
1609 // allocating a new token for it.
Richard Smith34f30512013-11-23 04:06:09 +00001610 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include,
1611 Imported);
Richard Smithce587f52013-11-15 04:24:58 +00001612 }
Douglas Gregor2537a362011-12-08 17:01:29 +00001613 return;
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001614 }
Douglas Gregor7a626572012-11-29 23:55:25 +00001615
1616 // If we failed to find a submodule that we expected to find, we can
1617 // continue. Otherwise, there's an error in the included file, so we
1618 // don't want to include it.
1619 if (!BuildingImportedModule && !Imported.isMissingExpected()) {
1620 return;
1621 }
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +00001622 }
1623
1624 if (Callbacks && SuggestedModule) {
1625 // We didn't notify the callback object that we've seen an inclusion
1626 // directive before. Now that we are parsing the include normally and not
1627 // turning it to a module import, notify the callback object.
1628 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
1629 FilenameRange, File,
1630 SearchPath, RelativePath,
Craig Topperd2d442c2014-05-17 23:10:59 +00001631 /*ImportedModule=*/nullptr);
Douglas Gregor97eec242011-09-15 22:00:41 +00001632 }
1633
Chris Lattnerc88a23e2008-09-26 20:12:23 +00001634 // The #included file will be considered to be a system header if either it is
1635 // in a system include directory, or if the #includer is a system include
1636 // header.
Mike Stump11289f42009-09-09 15:08:12 +00001637 SrcMgr::CharacteristicKind FileCharacter =
Chris Lattnerb03dc762008-09-26 21:18:42 +00001638 std::max(HeaderInfo.getFileDirFlavor(File),
Chris Lattnerc0334162009-01-19 07:59:15 +00001639 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
Mike Stump11289f42009-09-09 15:08:12 +00001640
Chris Lattner72286d62010-04-19 20:44:31 +00001641 // Ask HeaderInfo if we should enter this #include file. If not, #including
1642 // this file will have no effect.
1643 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001644 if (Callbacks)
Chris Lattner72286d62010-04-19 20:44:31 +00001645 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
Chris Lattner72286d62010-04-19 20:44:31 +00001646 return;
1647 }
1648
Chris Lattnerf64b3522008-03-09 01:54:53 +00001649 // Look up the file, create a File ID for it.
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +00001650 SourceLocation IncludePos = End;
1651 // If the filename string was the result of macro expansions, set the include
1652 // position on the file where it will be included and after the expansions.
1653 if (IncludePos.isMacroID())
1654 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1655 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
Peter Collingbourned395b932011-06-30 16:41:03 +00001656 assert(!FID.isInvalid() && "Expected valid file ID");
Chris Lattnerf64b3522008-03-09 01:54:53 +00001657
Richard Smith34f30512013-11-23 04:06:09 +00001658 // Determine if we're switching to building a new submodule, and which one.
1659 ModuleMap::KnownHeader BuildingModule;
1660 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) {
1661 Module *RequestingModule = getModuleForLocation(FilenameLoc);
1662 BuildingModule =
1663 HeaderInfo.getModuleMap().findModuleForHeader(File, RequestingModule);
1664 }
1665
1666 // If all is good, enter the new file!
Richard Smith67294e22014-01-31 20:47:44 +00001667 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1668 return;
Richard Smith34f30512013-11-23 04:06:09 +00001669
1670 // If we're walking into another part of the same module, let the parser
1671 // know that any future declarations are within that other submodule.
Richard Smith67294e22014-01-31 20:47:44 +00001672 if (BuildingModule) {
1673 assert(!CurSubmodule && "should not have marked this as a module yet");
1674 CurSubmodule = BuildingModule.getModule();
1675
Richard Smith34f30512013-11-23 04:06:09 +00001676 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin,
Richard Smith67294e22014-01-31 20:47:44 +00001677 CurSubmodule);
1678 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001679}
1680
James Dennettf6333ac2012-06-22 05:46:07 +00001681/// HandleIncludeNextDirective - Implements \#include_next.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001682///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001683void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1684 Token &IncludeNextTok) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001685 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Mike Stump11289f42009-09-09 15:08:12 +00001686
Chris Lattnerf64b3522008-03-09 01:54:53 +00001687 // #include_next is like #include, except that we start searching after
1688 // the current found directory. If we can't do this, issue a
1689 // diagnostic.
1690 const DirectoryLookup *Lookup = CurDirLookup;
1691 if (isInPrimaryFile()) {
Craig Topperd2d442c2014-05-17 23:10:59 +00001692 Lookup = nullptr;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001693 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Craig Topperd2d442c2014-05-17 23:10:59 +00001694 } else if (!Lookup) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001695 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1696 } else {
1697 // Start looking up in the next directory.
1698 ++Lookup;
1699 }
Mike Stump11289f42009-09-09 15:08:12 +00001700
Douglas Gregor796d76a2010-10-20 22:00:55 +00001701 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001702}
1703
James Dennettf6333ac2012-06-22 05:46:07 +00001704/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
Aaron Ballman0467f552012-03-18 03:10:37 +00001705void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1706 // The Microsoft #import directive takes a type library and generates header
1707 // files from it, and includes those. This is beyond the scope of what clang
1708 // does, so we ignore it and error out. However, #import can optionally have
1709 // trailing attributes that span multiple lines. We're going to eat those
1710 // so we can continue processing from there.
1711 Diag(Tok, diag::err_pp_import_directive_ms );
1712
1713 // Read tokens until we get to the end of the directive. Note that the
1714 // directive can be split over multiple lines using the backslash character.
1715 DiscardUntilEndOfDirective();
1716}
1717
James Dennettf6333ac2012-06-22 05:46:07 +00001718/// HandleImportDirective - Implements \#import.
Chris Lattnerf64b3522008-03-09 01:54:53 +00001719///
Douglas Gregor796d76a2010-10-20 22:00:55 +00001720void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1721 Token &ImportTok) {
Aaron Ballman0467f552012-03-18 03:10:37 +00001722 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
Alp Tokerbfa39342014-01-14 12:51:41 +00001723 if (LangOpts.MSVCCompat)
Aaron Ballman0467f552012-03-18 03:10:37 +00001724 return HandleMicrosoftImportDirective(ImportTok);
Chris Lattnerd4a96732009-03-06 04:28:03 +00001725 Diag(ImportTok, diag::ext_pp_import_directive);
Aaron Ballman0467f552012-03-18 03:10:37 +00001726 }
Craig Topperd2d442c2014-05-17 23:10:59 +00001727 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, true);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001728}
1729
Chris Lattner58a1eb02009-04-08 18:46:40 +00001730/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1731/// pseudo directive in the predefines buffer. This handles it by sucking all
1732/// tokens through the preprocessor and discarding them (only keeping the side
1733/// effects on the preprocessor).
Douglas Gregor796d76a2010-10-20 22:00:55 +00001734void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1735 Token &IncludeMacrosTok) {
Chris Lattner58a1eb02009-04-08 18:46:40 +00001736 // This directive should only occur in the predefines buffer. If not, emit an
1737 // error and reject it.
1738 SourceLocation Loc = IncludeMacrosTok.getLocation();
1739 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1740 Diag(IncludeMacrosTok.getLocation(),
1741 diag::pp_include_macros_out_of_predefines);
1742 DiscardUntilEndOfDirective();
1743 return;
1744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
Chris Lattnere01d82b2009-04-08 20:53:24 +00001746 // Treat this as a normal #include for checking purposes. If this is
1747 // successful, it will push a new lexer onto the include stack.
Craig Topperd2d442c2014-05-17 23:10:59 +00001748 HandleIncludeDirective(HashLoc, IncludeMacrosTok, nullptr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001749
Chris Lattnere01d82b2009-04-08 20:53:24 +00001750 Token TmpTok;
1751 do {
1752 Lex(TmpTok);
1753 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1754 } while (TmpTok.isNot(tok::hashhash));
Chris Lattner58a1eb02009-04-08 18:46:40 +00001755}
1756
Chris Lattnerf64b3522008-03-09 01:54:53 +00001757//===----------------------------------------------------------------------===//
1758// Preprocessor Macro Directive Handling.
1759//===----------------------------------------------------------------------===//
1760
1761/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1762/// definition has just been read. Lex the rest of the arguments and the
1763/// closing ), updating MI with what we learn. Return true if an error occurs
1764/// parsing the arg list.
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001765bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001766 SmallVector<IdentifierInfo*, 32> Arguments;
Mike Stump11289f42009-09-09 15:08:12 +00001767
Chris Lattnerf64b3522008-03-09 01:54:53 +00001768 while (1) {
1769 LexUnexpandedToken(Tok);
1770 switch (Tok.getKind()) {
1771 case tok::r_paren:
1772 // Found the end of the argument list.
Chris Lattnerf87c5102009-02-20 22:31:31 +00001773 if (Arguments.empty()) // #define FOO()
Chris Lattnerf64b3522008-03-09 01:54:53 +00001774 return false;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001775 // Otherwise we have #define FOO(A,)
1776 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1777 return true;
1778 case tok::ellipsis: // #define X(... -> C99 varargs
David Blaikiebbafb8a2012-03-11 07:00:24 +00001779 if (!LangOpts.C99)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001780 Diag(Tok, LangOpts.CPlusPlus11 ?
Richard Smithacd4d3d2011-10-15 01:18:56 +00001781 diag::warn_cxx98_compat_variadic_macro :
1782 diag::ext_variadic_macro);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001783
Joey Gouly1d58cdb2013-01-17 17:35:00 +00001784 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1785 if (LangOpts.OpenCL) {
1786 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1787 return true;
1788 }
1789
Chris Lattnerf64b3522008-03-09 01:54:53 +00001790 // Lex the token after the identifier.
1791 LexUnexpandedToken(Tok);
1792 if (Tok.isNot(tok::r_paren)) {
1793 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1794 return true;
1795 }
1796 // Add the __VA_ARGS__ identifier as an argument.
1797 Arguments.push_back(Ident__VA_ARGS__);
1798 MI->setIsC99Varargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001799 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001800 return false;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001801 case tok::eod: // #define X(
Chris Lattnerf64b3522008-03-09 01:54:53 +00001802 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1803 return true;
1804 default:
1805 // Handle keywords and identifiers here to accept things like
1806 // #define Foo(for) for.
1807 IdentifierInfo *II = Tok.getIdentifierInfo();
Craig Topperd2d442c2014-05-17 23:10:59 +00001808 if (!II) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001809 // #define X(1
1810 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1811 return true;
1812 }
1813
1814 // If this is already used as an argument, it is used multiple times (e.g.
1815 // #define X(A,A.
Mike Stump11289f42009-09-09 15:08:12 +00001816 if (std::find(Arguments.begin(), Arguments.end(), II) !=
Chris Lattnerf64b3522008-03-09 01:54:53 +00001817 Arguments.end()) { // C99 6.10.3p6
Chris Lattnerc5cdade2008-11-19 07:33:58 +00001818 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001819 return true;
1820 }
Mike Stump11289f42009-09-09 15:08:12 +00001821
Chris Lattnerf64b3522008-03-09 01:54:53 +00001822 // Add the argument to the macro info.
1823 Arguments.push_back(II);
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattnerf64b3522008-03-09 01:54:53 +00001825 // Lex the token after the identifier.
1826 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001827
Chris Lattnerf64b3522008-03-09 01:54:53 +00001828 switch (Tok.getKind()) {
1829 default: // #define X(A B
1830 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1831 return true;
1832 case tok::r_paren: // #define X(A)
Chris Lattner70946da2009-02-20 22:46:43 +00001833 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001834 return false;
1835 case tok::comma: // #define X(A,
1836 break;
1837 case tok::ellipsis: // #define X(A... -> GCC extension
1838 // Diagnose extension.
1839 Diag(Tok, diag::ext_named_variadic_macro);
Mike Stump11289f42009-09-09 15:08:12 +00001840
Chris Lattnerf64b3522008-03-09 01:54:53 +00001841 // Lex the token after the identifier.
1842 LexUnexpandedToken(Tok);
1843 if (Tok.isNot(tok::r_paren)) {
1844 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1845 return true;
1846 }
Mike Stump11289f42009-09-09 15:08:12 +00001847
Chris Lattnerf64b3522008-03-09 01:54:53 +00001848 MI->setIsGNUVarargs();
Chris Lattner70946da2009-02-20 22:46:43 +00001849 MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001850 return false;
1851 }
1852 }
1853 }
1854}
1855
James Dennettf6333ac2012-06-22 05:46:07 +00001856/// HandleDefineDirective - Implements \#define. This consumes the entire macro
Chris Lattnerf64b3522008-03-09 01:54:53 +00001857/// line then lets the caller lex the next real token.
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001858void Preprocessor::HandleDefineDirective(Token &DefineTok,
1859 bool ImmediatelyAfterHeaderGuard) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001860 ++NumDefined;
1861
1862 Token MacroNameTok;
1863 ReadMacroName(MacroNameTok, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001864
Chris Lattnerf64b3522008-03-09 01:54:53 +00001865 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001866 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00001867 return;
1868
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001869 Token LastTok = MacroNameTok;
1870
Chris Lattnerf64b3522008-03-09 01:54:53 +00001871 // If we are supposed to keep comments in #defines, reenable comment saving
1872 // mode.
Ted Kremenek59e003e2008-11-18 00:43:07 +00001873 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
Mike Stump11289f42009-09-09 15:08:12 +00001874
Chris Lattnerf64b3522008-03-09 01:54:53 +00001875 // Create the new macro.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001876 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001877
Chris Lattnerf64b3522008-03-09 01:54:53 +00001878 Token Tok;
1879 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001880
Chris Lattnerf64b3522008-03-09 01:54:53 +00001881 // If this is a function-like macro definition, parse the argument list,
1882 // marking each of the identifiers as being used as macro arguments. Also,
1883 // check other constraints on the first token of the macro body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001884 if (Tok.is(tok::eod)) {
Richard Trieu33a4b3d2013-06-12 21:20:57 +00001885 if (ImmediatelyAfterHeaderGuard) {
1886 // Save this macro information since it may part of a header guard.
1887 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
1888 MacroNameTok.getLocation());
1889 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00001890 // If there is no body to this macro, we have no special handling here.
Chris Lattner2425bcb2009-04-18 02:23:25 +00001891 } else if (Tok.hasLeadingSpace()) {
1892 // This is a normal token with leading space. Clear the leading space
1893 // marker on the first token to get proper expansion.
1894 Tok.clearFlag(Token::LeadingSpace);
1895 } else if (Tok.is(tok::l_paren)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001896 // This is a function-like macro definition. Read the argument list.
1897 MI->setIsFunctionLike();
Abramo Bagnarac9e48c02012-03-31 20:17:27 +00001898 if (ReadMacroDefinitionArgList(MI, LastTok)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001899 // Forget about MI.
Ted Kremenek6c7ea112008-12-15 19:56:42 +00001900 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001901 // Throw away the rest of the line.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00001902 if (CurPPLexer->ParsingPreprocessorDirective)
Chris Lattnerf64b3522008-03-09 01:54:53 +00001903 DiscardUntilEndOfDirective();
1904 return;
1905 }
1906
Chris Lattner249c38b2009-04-19 18:26:34 +00001907 // If this is a definition of a variadic C99 function-like macro, not using
1908 // the GNU named varargs extension, enabled __VA_ARGS__.
Mike Stump11289f42009-09-09 15:08:12 +00001909
Chris Lattner249c38b2009-04-19 18:26:34 +00001910 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1911 // This gets unpoisoned where it is allowed.
1912 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1913 if (MI->isC99Varargs())
1914 Ident__VA_ARGS__->setIsPoisoned(false);
Mike Stump11289f42009-09-09 15:08:12 +00001915
Chris Lattnerf64b3522008-03-09 01:54:53 +00001916 // Read the first token after the arg list for down below.
1917 LexUnexpandedToken(Tok);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001918 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00001919 // C99 requires whitespace between the macro definition and the body. Emit
1920 // a diagnostic for something like "#define X+".
Chris Lattner2425bcb2009-04-18 02:23:25 +00001921 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001922 } else {
Chris Lattner2425bcb2009-04-18 02:23:25 +00001923 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1924 // first character of a replacement list is not a character required by
1925 // subclause 5.2.1, then there shall be white-space separation between the
1926 // identifier and the replacement list.". 5.2.1 lists this set:
1927 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1928 // is irrelevant here.
1929 bool isInvalid = false;
1930 if (Tok.is(tok::at)) // @ is not in the list above.
1931 isInvalid = true;
1932 else if (Tok.is(tok::unknown)) {
1933 // If we have an unknown token, it is something strange like "`". Since
1934 // all of valid characters would have lexed into a single character
1935 // token of some sort, we know this is not a valid case.
1936 isInvalid = true;
1937 }
1938 if (isInvalid)
1939 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1940 else
1941 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
Chris Lattnerf64b3522008-03-09 01:54:53 +00001942 }
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001943
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001944 if (!Tok.is(tok::eod))
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001945 LastTok = Tok;
1946
Chris Lattnerf64b3522008-03-09 01:54:53 +00001947 // Read the rest of the macro body.
1948 if (MI->isObjectLike()) {
1949 // Object-like macros are very simple, just read their body.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001950 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001951 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001952 MI->AddTokenToBody(Tok);
1953 // Get the next token of the macro.
1954 LexUnexpandedToken(Tok);
1955 }
Mike Stump11289f42009-09-09 15:08:12 +00001956
Chris Lattnerf64b3522008-03-09 01:54:53 +00001957 } else {
Chris Lattner83bd8282009-05-25 17:16:10 +00001958 // Otherwise, read the body of a function-like macro. While we are at it,
1959 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1960 // parameters in function-like macro expansions.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001961 while (Tok.isNot(tok::eod)) {
Chris Lattnerd6e97af2009-04-21 04:46:33 +00001962 LastTok = Tok;
Chris Lattnerf64b3522008-03-09 01:54:53 +00001963
Eli Friedman14d3c792012-11-14 02:18:46 +00001964 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00001965 MI->AddTokenToBody(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattnerf64b3522008-03-09 01:54:53 +00001967 // Get the next token of the macro.
1968 LexUnexpandedToken(Tok);
1969 continue;
1970 }
Mike Stump11289f42009-09-09 15:08:12 +00001971
Richard Smith701a3522013-07-09 01:00:29 +00001972 // If we're in -traditional mode, then we should ignore stringification
1973 // and token pasting. Mark the tokens as unknown so as not to confuse
1974 // things.
1975 if (getLangOpts().TraditionalCPP) {
1976 Tok.setKind(tok::unknown);
1977 MI->AddTokenToBody(Tok);
1978
1979 // Get the next token of the macro.
1980 LexUnexpandedToken(Tok);
1981 continue;
1982 }
1983
Eli Friedman14d3c792012-11-14 02:18:46 +00001984 if (Tok.is(tok::hashhash)) {
1985
1986 // If we see token pasting, check if it looks like the gcc comma
1987 // pasting extension. We'll use this information to suppress
1988 // diagnostics later on.
1989
1990 // Get the next token of the macro.
1991 LexUnexpandedToken(Tok);
1992
1993 if (Tok.is(tok::eod)) {
1994 MI->AddTokenToBody(LastTok);
1995 break;
1996 }
1997
1998 unsigned NumTokens = MI->getNumTokens();
1999 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2000 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2001 MI->setHasCommaPasting();
2002
David Majnemer76faf1f2013-11-05 09:30:17 +00002003 // Things look ok, add the '##' token to the macro.
Eli Friedman14d3c792012-11-14 02:18:46 +00002004 MI->AddTokenToBody(LastTok);
Eli Friedman14d3c792012-11-14 02:18:46 +00002005 continue;
2006 }
2007
Chris Lattnerf64b3522008-03-09 01:54:53 +00002008 // Get the next token of the macro.
2009 LexUnexpandedToken(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00002010
Chris Lattner83bd8282009-05-25 17:16:10 +00002011 // Check for a valid macro arg identifier.
Craig Topperd2d442c2014-05-17 23:10:59 +00002012 if (Tok.getIdentifierInfo() == nullptr ||
Chris Lattner83bd8282009-05-25 17:16:10 +00002013 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2014
2015 // If this is assembler-with-cpp mode, we accept random gibberish after
2016 // the '#' because '#' is often a comment character. However, change
2017 // the kind of the token to tok::unknown so that the preprocessor isn't
2018 // confused.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002019 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
Chris Lattner83bd8282009-05-25 17:16:10 +00002020 LastTok.setKind(tok::unknown);
Eli Friedmancdf8b882013-06-18 21:33:38 +00002021 MI->AddTokenToBody(LastTok);
2022 continue;
Chris Lattner83bd8282009-05-25 17:16:10 +00002023 } else {
2024 Diag(Tok, diag::err_pp_stringize_not_parameter);
2025 ReleaseMacroInfo(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002026
Chris Lattner83bd8282009-05-25 17:16:10 +00002027 // Disable __VA_ARGS__ again.
2028 Ident__VA_ARGS__->setIsPoisoned(true);
2029 return;
2030 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002031 }
Mike Stump11289f42009-09-09 15:08:12 +00002032
Chris Lattner83bd8282009-05-25 17:16:10 +00002033 // Things look ok, add the '#' and param name tokens to the macro.
2034 MI->AddTokenToBody(LastTok);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002035 MI->AddTokenToBody(Tok);
Chris Lattner83bd8282009-05-25 17:16:10 +00002036 LastTok = Tok;
Mike Stump11289f42009-09-09 15:08:12 +00002037
Chris Lattnerf64b3522008-03-09 01:54:53 +00002038 // Get the next token of the macro.
2039 LexUnexpandedToken(Tok);
2040 }
2041 }
Mike Stump11289f42009-09-09 15:08:12 +00002042
2043
Chris Lattnerf64b3522008-03-09 01:54:53 +00002044 // Disable __VA_ARGS__ again.
2045 Ident__VA_ARGS__->setIsPoisoned(true);
2046
Chris Lattner57540c52011-04-15 05:22:18 +00002047 // Check that there is no paste (##) operator at the beginning or end of the
Chris Lattnerf64b3522008-03-09 01:54:53 +00002048 // replacement list.
2049 unsigned NumTokens = MI->getNumTokens();
2050 if (NumTokens != 0) {
2051 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2052 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002053 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002054 return;
2055 }
2056 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2057 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Ted Kremenek6c7ea112008-12-15 19:56:42 +00002058 ReleaseMacroInfo(MI);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002059 return;
2060 }
2061 }
Mike Stump11289f42009-09-09 15:08:12 +00002062
Chris Lattnerd6e97af2009-04-21 04:46:33 +00002063 MI->setDefinitionEndLoc(LastTok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00002064
Chris Lattnerf64b3522008-03-09 01:54:53 +00002065 // Finally, if this identifier already had a macro defined for it, verify that
Alexander Kornienko8b3f6232012-08-29 00:20:03 +00002066 // the macro bodies are identical, and issue diagnostics if they are not.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002067 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
Chris Lattner5244f342009-01-16 19:50:11 +00002068 // It is very common for system headers to have tons of macro redefinitions
2069 // and for warnings to be disabled in system headers. If this is the case,
2070 // then don't bother calling MacroInfo::isIdenticalTo.
Chris Lattner80c21df2009-03-13 21:17:23 +00002071 if (!getDiagnostics().getSuppressSystemWarnings() ||
Chris Lattner5244f342009-01-16 19:50:11 +00002072 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002073 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
Chris Lattner5244f342009-01-16 19:50:11 +00002074 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002075
Richard Smith7b242542013-03-06 00:46:00 +00002076 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2077 // C++ [cpp.predefined]p4, but allow it as an extension.
2078 if (OtherMI->isBuiltinMacro())
2079 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
Chris Lattnerc0a585d2010-08-17 15:55:45 +00002080 // Macros must be identical. This means all tokens and whitespace
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002081 // separation must be the same. C99 6.10.3p2.
Richard Smith7b242542013-03-06 00:46:00 +00002082 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00002083 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
Chris Lattner5244f342009-01-16 19:50:11 +00002084 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2085 << MacroNameTok.getIdentifierInfo();
2086 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2087 }
Chris Lattnerf64b3522008-03-09 01:54:53 +00002088 }
Argyrios Kyrtzidisb495cc12011-01-18 19:50:15 +00002089 if (OtherMI->isWarnIfUnused())
2090 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002091 }
Mike Stump11289f42009-09-09 15:08:12 +00002092
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002093 DefMacroDirective *MD =
2094 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
Mike Stump11289f42009-09-09 15:08:12 +00002095
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002096 assert(!MI->isUsed());
2097 // If we need warning for not using the macro, add its location in the
2098 // warn-because-unused-macro set. If it gets used it will be removed from set.
Eli Friedman5ba37d52013-08-22 00:27:10 +00002099 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002100 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002101 MI->setIsWarnIfUnused(true);
2102 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2103 }
2104
Chris Lattner928e9092009-04-12 01:39:54 +00002105 // If the callbacks want to know, tell them about the macro definition.
2106 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002107 Callbacks->MacroDefined(MacroNameTok, MD);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002108}
2109
James Dennettf6333ac2012-06-22 05:46:07 +00002110/// HandleUndefDirective - Implements \#undef.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002111///
2112void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2113 ++NumUndefined;
2114
2115 Token MacroNameTok;
2116 ReadMacroName(MacroNameTok, 2);
Mike Stump11289f42009-09-09 15:08:12 +00002117
Chris Lattnerf64b3522008-03-09 01:54:53 +00002118 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002119 if (MacroNameTok.is(tok::eod))
Chris Lattnerf64b3522008-03-09 01:54:53 +00002120 return;
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattnerf64b3522008-03-09 01:54:53 +00002122 // Check to see if this is the last token on the #undef line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002123 CheckEndOfDirective("undef");
Mike Stump11289f42009-09-09 15:08:12 +00002124
Chris Lattnerf64b3522008-03-09 01:54:53 +00002125 // Okay, we finally have a valid identifier to undef.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002126 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo());
Craig Topperd2d442c2014-05-17 23:10:59 +00002127 const MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002128
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002129 // If the callbacks want to know, tell them about the macro #undef.
2130 // Note: no matter if the macro was defined or not.
2131 if (Callbacks)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002132 Callbacks->MacroUndefined(MacroNameTok, MD);
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +00002133
Chris Lattnerf64b3522008-03-09 01:54:53 +00002134 // If the macro is not defined, this is a noop undef, just return.
Craig Topperd2d442c2014-05-17 23:10:59 +00002135 if (!MI)
2136 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002137
Argyrios Kyrtzidis22998892011-07-11 20:39:47 +00002138 if (!MI->isUsed() && MI->isWarnIfUnused())
Chris Lattnerf64b3522008-03-09 01:54:53 +00002139 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattnercd6d4b12009-04-21 03:42:09 +00002140
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002141 if (MI->isWarnIfUnused())
2142 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2143
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002144 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2145 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
Chris Lattnerf64b3522008-03-09 01:54:53 +00002146}
2147
2148
2149//===----------------------------------------------------------------------===//
2150// Preprocessor Conditional Directive Handling.
2151//===----------------------------------------------------------------------===//
2152
James Dennettf6333ac2012-06-22 05:46:07 +00002153/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2154/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2155/// true if any tokens have been returned or pp-directives activated before this
2156/// \#ifndef has been lexed.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002157///
2158void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2159 bool ReadAnyTokensBeforeDirective) {
2160 ++NumIf;
2161 Token DirectiveTok = Result;
2162
2163 Token MacroNameTok;
2164 ReadMacroName(MacroNameTok);
Mike Stump11289f42009-09-09 15:08:12 +00002165
Chris Lattnerf64b3522008-03-09 01:54:53 +00002166 // Error reading macro name? If so, diagnostic already issued.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002167 if (MacroNameTok.is(tok::eod)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002168 // Skip code until we get to #endif. This helps with recovery by not
2169 // emitting an error when the #endif is reached.
2170 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2171 /*Foundnonskip*/false, /*FoundElse*/false);
2172 return;
2173 }
Mike Stump11289f42009-09-09 15:08:12 +00002174
Chris Lattnerf64b3522008-03-09 01:54:53 +00002175 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002176 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
Chris Lattnerf64b3522008-03-09 01:54:53 +00002177
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002178 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002179 MacroDirective *MD = getMacroDirective(MII);
Craig Topperd2d442c2014-05-17 23:10:59 +00002180 MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002181
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002182 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002183 // If the start of a top-level #ifdef and if the macro is not defined,
2184 // inform MIOpt that this might be the start of a proper include guard.
2185 // Otherwise it is some other form of unknown conditional which we can't
2186 // handle.
Craig Topperd2d442c2014-05-17 23:10:59 +00002187 if (!ReadAnyTokensBeforeDirective && !MI) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002188 assert(isIfndef && "#ifdef shouldn't reach here");
Richard Trieu33a4b3d2013-06-12 21:20:57 +00002189 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002190 } else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002191 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002192 }
2193
Chris Lattnerf64b3522008-03-09 01:54:53 +00002194 // If there is a macro, process it.
2195 if (MI) // Mark it used.
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00002196 markMacroAsUsed(MI);
Mike Stump11289f42009-09-09 15:08:12 +00002197
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002198 if (Callbacks) {
2199 if (isIfndef)
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002200 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002201 else
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +00002202 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002203 }
2204
Chris Lattnerf64b3522008-03-09 01:54:53 +00002205 // Should we include the stuff contained by this directive?
2206 if (!MI == isIfndef) {
2207 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner8cf1f932009-12-14 04:54:40 +00002208 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2209 /*wasskip*/false, /*foundnonskip*/true,
2210 /*foundelse*/false);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002211 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002212 // No, skip the contents of this block.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002213 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00002214 /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002215 /*FoundElse*/false);
2216 }
2217}
2218
James Dennettf6333ac2012-06-22 05:46:07 +00002219/// HandleIfDirective - Implements the \#if directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002220///
2221void Preprocessor::HandleIfDirective(Token &IfToken,
2222 bool ReadAnyTokensBeforeDirective) {
2223 ++NumIf;
Mike Stump11289f42009-09-09 15:08:12 +00002224
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002225 // Parse and evaluate the conditional expression.
Craig Topperd2d442c2014-05-17 23:10:59 +00002226 IdentifierInfo *IfNDefMacro = nullptr;
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002227 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2228 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2229 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Nuno Lopes363212b2008-06-01 18:31:24 +00002230
2231 // If this condition is equivalent to #ifndef X, and if this is the first
2232 // directive seen, handle it for the multiple-include optimization.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002233 if (CurPPLexer->getConditionalStackDepth() == 0) {
Chris Lattneraa1cccbb2010-02-12 08:03:27 +00002234 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
Richard Smith089ee152013-06-16 05:05:39 +00002235 // FIXME: Pass in the location of the macro name, not the 'if' token.
2236 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
Nuno Lopes363212b2008-06-01 18:31:24 +00002237 else
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002238 CurPPLexer->MIOpt.EnterTopLevelConditional();
Nuno Lopes363212b2008-06-01 18:31:24 +00002239 }
2240
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002241 if (Callbacks)
2242 Callbacks->If(IfToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002243 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002244 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002245
Chris Lattnerf64b3522008-03-09 01:54:53 +00002246 // Should we include the stuff contained by this directive?
2247 if (ConditionalTrue) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002248 // Yes, remember that we are inside a conditional, then lex the next token.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002249 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002250 /*foundnonskip*/true, /*foundelse*/false);
2251 } else {
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002252 // No, skip the contents of this block.
Mike Stump11289f42009-09-09 15:08:12 +00002253 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnerf64b3522008-03-09 01:54:53 +00002254 /*FoundElse*/false);
2255 }
2256}
2257
James Dennettf6333ac2012-06-22 05:46:07 +00002258/// HandleEndifDirective - Implements the \#endif directive.
Chris Lattnerf64b3522008-03-09 01:54:53 +00002259///
2260void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2261 ++NumEndif;
Mike Stump11289f42009-09-09 15:08:12 +00002262
Chris Lattnerf64b3522008-03-09 01:54:53 +00002263 // Check that this is the whole directive.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002264 CheckEndOfDirective("endif");
Mike Stump11289f42009-09-09 15:08:12 +00002265
Chris Lattnerf64b3522008-03-09 01:54:53 +00002266 PPConditionalInfo CondInfo;
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002267 if (CurPPLexer->popConditionalLevel(CondInfo)) {
Chris Lattnerf64b3522008-03-09 01:54:53 +00002268 // No conditionals on the stack: this is an #endif without an #if.
Chris Lattner907dfe92008-11-18 07:59:24 +00002269 Diag(EndifToken, diag::err_pp_endif_without_if);
2270 return;
Chris Lattnerf64b3522008-03-09 01:54:53 +00002271 }
Mike Stump11289f42009-09-09 15:08:12 +00002272
Chris Lattnerf64b3522008-03-09 01:54:53 +00002273 // If this the end of a top-level #endif, inform MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002274 if (CurPPLexer->getConditionalStackDepth() == 0)
2275 CurPPLexer->MIOpt.ExitTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002276
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002277 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
Chris Lattnerf64b3522008-03-09 01:54:53 +00002278 "This code should only be reachable in the non-skipping case!");
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002279
2280 if (Callbacks)
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002281 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002282}
2283
James Dennettf6333ac2012-06-22 05:46:07 +00002284/// HandleElseDirective - Implements the \#else directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002285///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002286void Preprocessor::HandleElseDirective(Token &Result) {
2287 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002288
Chris Lattnerf64b3522008-03-09 01:54:53 +00002289 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnerce2ab6f2009-04-14 05:07:49 +00002290 CheckEndOfDirective("else");
Mike Stump11289f42009-09-09 15:08:12 +00002291
Chris Lattnerf64b3522008-03-09 01:54:53 +00002292 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002293 if (CurPPLexer->popConditionalLevel(CI)) {
2294 Diag(Result, diag::pp_err_else_without_if);
2295 return;
2296 }
Mike Stump11289f42009-09-09 15:08:12 +00002297
Chris Lattnerf64b3522008-03-09 01:54:53 +00002298 // If this is a top-level #else, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002299 if (CurPPLexer->getConditionalStackDepth() == 0)
2300 CurPPLexer->MIOpt.EnterTopLevelConditional();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002301
2302 // If this is a #else with a #else before it, report the error.
2303 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Mike Stump11289f42009-09-09 15:08:12 +00002304
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002305 if (Callbacks)
2306 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2307
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002308 // Finally, skip the rest of the contents of this block.
2309 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002310 /*FoundElse*/true, Result.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002311}
2312
James Dennettf6333ac2012-06-22 05:46:07 +00002313/// HandleElifDirective - Implements the \#elif directive.
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002314///
Chris Lattnerf64b3522008-03-09 01:54:53 +00002315void Preprocessor::HandleElifDirective(Token &ElifToken) {
2316 ++NumElse;
Mike Stump11289f42009-09-09 15:08:12 +00002317
Chris Lattnerf64b3522008-03-09 01:54:53 +00002318 // #elif directive in a non-skipping conditional... start skipping.
2319 // We don't care what the condition is, because we will always skip it (since
2320 // the block immediately before it was included).
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002321 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002322 DiscardUntilEndOfDirective();
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002323 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
Chris Lattnerf64b3522008-03-09 01:54:53 +00002324
2325 PPConditionalInfo CI;
Chris Lattner907dfe92008-11-18 07:59:24 +00002326 if (CurPPLexer->popConditionalLevel(CI)) {
2327 Diag(ElifToken, diag::pp_err_elif_without_if);
2328 return;
2329 }
Mike Stump11289f42009-09-09 15:08:12 +00002330
Chris Lattnerf64b3522008-03-09 01:54:53 +00002331 // If this is a top-level #elif, inform the MIOpt.
Ted Kremenek30cd88c2008-11-18 00:34:22 +00002332 if (CurPPLexer->getConditionalStackDepth() == 0)
2333 CurPPLexer->MIOpt.EnterTopLevelConditional();
Mike Stump11289f42009-09-09 15:08:12 +00002334
Chris Lattnerf64b3522008-03-09 01:54:53 +00002335 // If this is a #elif with a #else before it, report the error.
2336 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Argyrios Kyrtzidisc793a612012-03-05 05:48:09 +00002337
2338 if (Callbacks)
2339 Callbacks->Elif(ElifToken.getLocation(),
John Thompsonb1028562013-07-18 00:00:36 +00002340 SourceRange(ConditionalBegin, ConditionalEnd),
John Thompson87f9fef2013-12-07 08:41:15 +00002341 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
Chris Lattnerf64b3522008-03-09 01:54:53 +00002342
Craig Silverstein8e3d95e2010-11-06 01:19:03 +00002343 // Finally, skip the rest of the contents of this block.
2344 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
Argyrios Kyrtzidis18bcfd52011-09-27 17:32:05 +00002345 /*FoundElse*/CI.FoundElse,
2346 ElifToken.getLocation());
Chris Lattnerf64b3522008-03-09 01:54:53 +00002347}