]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Lex/PPDirectives.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303571, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Lex / PPDirectives.cpp
1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
2 //
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 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Implements # directive processing for the Preprocessor.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/Module.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TokenKinds.h"
23 #include "clang/Lex/CodeCompletionHandler.h"
24 #include "clang/Lex/HeaderSearch.h"
25 #include "clang/Lex/LexDiagnostic.h"
26 #include "clang/Lex/LiteralSupport.h"
27 #include "clang/Lex/MacroInfo.h"
28 #include "clang/Lex/ModuleLoader.h"
29 #include "clang/Lex/ModuleMap.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "clang/Lex/Pragma.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Lex/PTHLexer.h"
34 #include "clang/Lex/Token.h"
35 #include "llvm/ADT/ArrayRef.h"
36 #include "llvm/ADT/SmallString.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/ADT/StringSwitch.h"
40 #include "llvm/ADT/StringRef.h"
41 #include "llvm/Support/AlignOf.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/Path.h"
44 #include <algorithm>
45 #include <cassert>
46 #include <cstring>
47 #include <new>
48 #include <string>
49 #include <utility>
50
51 using namespace clang;
52
53 //===----------------------------------------------------------------------===//
54 // Utility Methods for Preprocessor Directive Handling.
55 //===----------------------------------------------------------------------===//
56
57 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
58   auto *MIChain = new (BP) MacroInfoChain{L, MIChainHead};
59   MIChainHead = MIChain;
60   return &MIChain->MI;
61 }
62
63 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
64                                                            SourceLocation Loc) {
65   return new (BP) DefMacroDirective(MI, Loc);
66 }
67
68 UndefMacroDirective *
69 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
70   return new (BP) UndefMacroDirective(UndefLoc);
71 }
72
73 VisibilityMacroDirective *
74 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
75                                                bool isPublic) {
76   return new (BP) VisibilityMacroDirective(Loc, isPublic);
77 }
78
79 /// \brief Read and discard all tokens remaining on the current line until
80 /// the tok::eod token is found.
81 void Preprocessor::DiscardUntilEndOfDirective() {
82   Token Tmp;
83   do {
84     LexUnexpandedToken(Tmp);
85     assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
86   } while (Tmp.isNot(tok::eod));
87 }
88
89 /// \brief Enumerates possible cases of #define/#undef a reserved identifier.
90 enum MacroDiag {
91   MD_NoWarn,        //> Not a reserved identifier
92   MD_KeywordDef,    //> Macro hides keyword, enabled by default
93   MD_ReservedMacro  //> #define of #undef reserved id, disabled by default
94 };
95
96 /// \brief Checks if the specified identifier is reserved in the specified
97 /// language.
98 /// This function does not check if the identifier is a keyword.
99 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
100   // C++ [macro.names], C11 7.1.3:
101   // All identifiers that begin with an underscore and either an uppercase
102   // letter or another underscore are always reserved for any use.
103   if (Text.size() >= 2 && Text[0] == '_' &&
104       (isUppercase(Text[1]) || Text[1] == '_'))
105       return true;
106   // C++ [global.names]
107   // Each name that contains a double underscore ... is reserved to the
108   // implementation for any use.
109   if (Lang.CPlusPlus) {
110     if (Text.find("__") != StringRef::npos)
111       return true;
112   }
113   return false;
114 }
115
116 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
117   const LangOptions &Lang = PP.getLangOpts();
118   StringRef Text = II->getName();
119   if (isReservedId(Text, Lang))
120     return MD_ReservedMacro;
121   if (II->isKeyword(Lang))
122     return MD_KeywordDef;
123   if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
124     return MD_KeywordDef;
125   return MD_NoWarn;
126 }
127
128 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
129   const LangOptions &Lang = PP.getLangOpts();
130   StringRef Text = II->getName();
131   // Do not warn on keyword undef.  It is generally harmless and widely used.
132   if (isReservedId(Text, Lang))
133     return MD_ReservedMacro;
134   return MD_NoWarn;
135 }
136
137 // Return true if we want to issue a diagnostic by default if we
138 // encounter this name in a #include with the wrong case. For now,
139 // this includes the standard C and C++ headers, Posix headers,
140 // and Boost headers. Improper case for these #includes is a
141 // potential portability issue.
142 static bool warnByDefaultOnWrongCase(StringRef Include) {
143   // If the first component of the path is "boost", treat this like a standard header
144   // for the purposes of diagnostics.
145   if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
146     return true;
147
148   // "condition_variable" is the longest standard header name at 18 characters.
149   // If the include file name is longer than that, it can't be a standard header.
150   static const size_t MaxStdHeaderNameLen = 18u;
151   if (Include.size() > MaxStdHeaderNameLen)
152     return false;
153
154   // Lowercase and normalize the search string.
155   SmallString<32> LowerInclude{Include};
156   for (char &Ch : LowerInclude) {
157     // In the ASCII range?
158     if (static_cast<unsigned char>(Ch) > 0x7f)
159       return false; // Can't be a standard header
160     // ASCII lowercase:
161     if (Ch >= 'A' && Ch <= 'Z')
162       Ch += 'a' - 'A';
163     // Normalize path separators for comparison purposes.
164     else if (::llvm::sys::path::is_separator(Ch))
165       Ch = '/';
166   }
167
168   // The standard C/C++ and Posix headers
169   return llvm::StringSwitch<bool>(LowerInclude)
170     // C library headers
171     .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
172     .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
173     .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
174     .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
175     .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
176     .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
177
178     // C++ headers for C library facilities
179     .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
180     .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
181     .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
182     .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
183     .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
184     .Case("cwctype", true)
185
186     // C++ library headers
187     .Cases("algorithm", "fstream", "list", "regex", "thread", true)
188     .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
189     .Cases("atomic", "future", "map", "set", "type_traits", true)
190     .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
191     .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
192     .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
193     .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
194     .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
195     .Cases("deque", "istream", "queue", "string", "valarray", true)
196     .Cases("exception", "iterator", "random", "strstream", "vector", true)
197     .Cases("forward_list", "limits", "ratio", "system_error", true)
198
199     // POSIX headers (which aren't also C headers)
200     .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
201     .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
202     .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
203     .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
204     .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
205     .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
206     .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
207     .Cases("sys/resource.h", "sys/select.h",  "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
208     .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
209     .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
210     .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
211     .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
212     .Default(false);
213 }
214
215 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
216                                   bool *ShadowFlag) {
217   // Missing macro name?
218   if (MacroNameTok.is(tok::eod))
219     return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
220
221   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
222   if (!II) {
223     bool Invalid = false;
224     std::string Spelling = getSpelling(MacroNameTok, &Invalid);
225     if (Invalid)
226       return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
227     II = getIdentifierInfo(Spelling);
228
229     if (!II->isCPlusPlusOperatorKeyword())
230       return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
231
232     // C++ 2.5p2: Alternative tokens behave the same as its primary token
233     // except for their spellings.
234     Diag(MacroNameTok, getLangOpts().MicrosoftExt
235                            ? diag::ext_pp_operator_used_as_macro_name
236                            : diag::err_pp_operator_used_as_macro_name)
237         << II << MacroNameTok.getKind();
238
239     // Allow #defining |and| and friends for Microsoft compatibility or
240     // recovery when legacy C headers are included in C++.
241     MacroNameTok.setIdentifierInfo(II);
242   }
243
244   if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
245     // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
246     return Diag(MacroNameTok, diag::err_defined_macro_name);
247   }
248
249   if (isDefineUndef == MU_Undef) {
250     auto *MI = getMacroInfo(II);
251     if (MI && MI->isBuiltinMacro()) {
252       // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
253       // and C++ [cpp.predefined]p4], but allow it as an extension.
254       Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
255     }
256   }
257
258   // If defining/undefining reserved identifier or a keyword, we need to issue
259   // a warning.
260   SourceLocation MacroNameLoc = MacroNameTok.getLocation();
261   if (ShadowFlag)
262     *ShadowFlag = false;
263   if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
264       (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
265     MacroDiag D = MD_NoWarn;
266     if (isDefineUndef == MU_Define) {
267       D = shouldWarnOnMacroDef(*this, II);
268     }
269     else if (isDefineUndef == MU_Undef)
270       D = shouldWarnOnMacroUndef(*this, II);
271     if (D == MD_KeywordDef) {
272       // We do not want to warn on some patterns widely used in configuration
273       // scripts.  This requires analyzing next tokens, so do not issue warnings
274       // now, only inform caller.
275       if (ShadowFlag)
276         *ShadowFlag = true;
277     }
278     if (D == MD_ReservedMacro)
279       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
280   }
281
282   // Okay, we got a good identifier.
283   return false;
284 }
285
286 /// \brief Lex and validate a macro name, which occurs after a
287 /// \#define or \#undef.
288 ///
289 /// This sets the token kind to eod and discards the rest of the macro line if
290 /// the macro name is invalid.
291 ///
292 /// \param MacroNameTok Token that is expected to be a macro name.
293 /// \param isDefineUndef Context in which macro is used.
294 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
295 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
296                                  bool *ShadowFlag) {
297   // Read the token, don't allow macro expansion on it.
298   LexUnexpandedToken(MacroNameTok);
299
300   if (MacroNameTok.is(tok::code_completion)) {
301     if (CodeComplete)
302       CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
303     setCodeCompletionReached();
304     LexUnexpandedToken(MacroNameTok);
305   }
306
307   if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
308     return;
309
310   // Invalid macro name, read and discard the rest of the line and set the
311   // token kind to tok::eod if necessary.
312   if (MacroNameTok.isNot(tok::eod)) {
313     MacroNameTok.setKind(tok::eod);
314     DiscardUntilEndOfDirective();
315   }
316 }
317
318 /// \brief Ensure that the next token is a tok::eod token.
319 ///
320 /// If not, emit a diagnostic and consume up until the eod.  If EnableMacros is
321 /// true, then we consider macros that expand to zero tokens as being ok.
322 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
323   Token Tmp;
324   // Lex unexpanded tokens for most directives: macros might expand to zero
325   // tokens, causing us to miss diagnosing invalid lines.  Some directives (like
326   // #line) allow empty macros.
327   if (EnableMacros)
328     Lex(Tmp);
329   else
330     LexUnexpandedToken(Tmp);
331
332   // There should be no tokens after the directive, but we allow them as an
333   // extension.
334   while (Tmp.is(tok::comment))  // Skip comments in -C mode.
335     LexUnexpandedToken(Tmp);
336
337   if (Tmp.isNot(tok::eod)) {
338     // Add a fixit in GNU/C99/C++ mode.  Don't offer a fixit for strict-C89,
339     // or if this is a macro-style preprocessing directive, because it is more
340     // trouble than it is worth to insert /**/ and check that there is no /**/
341     // in the range also.
342     FixItHint Hint;
343     if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
344         !CurTokenLexer)
345       Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
346     Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
347     DiscardUntilEndOfDirective();
348   }
349 }
350
351 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
352 /// decided that the subsequent tokens are in the \#if'd out portion of the
353 /// file.  Lex the rest of the file, until we see an \#endif.  If
354 /// FoundNonSkipPortion is true, then we have already emitted code for part of
355 /// this \#if directive, so \#else/\#elif blocks should never be entered.
356 /// If ElseOk is true, then \#else directives are ok, if not, then we have
357 /// already seen one so a \#else directive is a duplicate.  When this returns,
358 /// the caller can lex the first valid token.
359 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
360                                                 bool FoundNonSkipPortion,
361                                                 bool FoundElse,
362                                                 SourceLocation ElseLoc) {
363   ++NumSkipped;
364   assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
365
366   CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
367                                  FoundNonSkipPortion, FoundElse);
368
369   if (CurPTHLexer) {
370     PTHSkipExcludedConditionalBlock();
371     return;
372   }
373
374   // Enter raw mode to disable identifier lookup (and thus macro expansion),
375   // disabling warnings, etc.
376   CurPPLexer->LexingRawMode = true;
377   Token Tok;
378   while (true) {
379     CurLexer->Lex(Tok);
380
381     if (Tok.is(tok::code_completion)) {
382       if (CodeComplete)
383         CodeComplete->CodeCompleteInConditionalExclusion();
384       setCodeCompletionReached();
385       continue;
386     }
387
388     // If this is the end of the buffer, we have an error.
389     if (Tok.is(tok::eof)) {
390       // Emit errors for each unterminated conditional on the stack, including
391       // the current one.
392       while (!CurPPLexer->ConditionalStack.empty()) {
393         if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
394           Diag(CurPPLexer->ConditionalStack.back().IfLoc,
395                diag::err_pp_unterminated_conditional);
396         CurPPLexer->ConditionalStack.pop_back();
397       }
398
399       // Just return and let the caller lex after this #include.
400       break;
401     }
402
403     // If this token is not a preprocessor directive, just skip it.
404     if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
405       continue;
406
407     // We just parsed a # character at the start of a line, so we're in
408     // directive mode.  Tell the lexer this so any newlines we see will be
409     // converted into an EOD token (this terminates the macro).
410     CurPPLexer->ParsingPreprocessorDirective = true;
411     if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
412
413
414     // Read the next token, the directive flavor.
415     LexUnexpandedToken(Tok);
416
417     // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
418     // something bogus), skip it.
419     if (Tok.isNot(tok::raw_identifier)) {
420       CurPPLexer->ParsingPreprocessorDirective = false;
421       // Restore comment saving mode.
422       if (CurLexer) CurLexer->resetExtendedTokenMode();
423       continue;
424     }
425
426     // If the first letter isn't i or e, it isn't intesting to us.  We know that
427     // this is safe in the face of spelling differences, because there is no way
428     // to spell an i/e in a strange way that is another letter.  Skipping this
429     // allows us to avoid looking up the identifier info for #define/#undef and
430     // other common directives.
431     StringRef RI = Tok.getRawIdentifier();
432
433     char FirstChar = RI[0];
434     if (FirstChar >= 'a' && FirstChar <= 'z' &&
435         FirstChar != 'i' && FirstChar != 'e') {
436       CurPPLexer->ParsingPreprocessorDirective = false;
437       // Restore comment saving mode.
438       if (CurLexer) CurLexer->resetExtendedTokenMode();
439       continue;
440     }
441
442     // Get the identifier name without trigraphs or embedded newlines.  Note
443     // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
444     // when skipping.
445     char DirectiveBuf[20];
446     StringRef Directive;
447     if (!Tok.needsCleaning() && RI.size() < 20) {
448       Directive = RI;
449     } else {
450       std::string DirectiveStr = getSpelling(Tok);
451       size_t IdLen = DirectiveStr.size();
452       if (IdLen >= 20) {
453         CurPPLexer->ParsingPreprocessorDirective = false;
454         // Restore comment saving mode.
455         if (CurLexer) CurLexer->resetExtendedTokenMode();
456         continue;
457       }
458       memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
459       Directive = StringRef(DirectiveBuf, IdLen);
460     }
461
462     if (Directive.startswith("if")) {
463       StringRef Sub = Directive.substr(2);
464       if (Sub.empty() ||   // "if"
465           Sub == "def" ||   // "ifdef"
466           Sub == "ndef") {  // "ifndef"
467         // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
468         // bother parsing the condition.
469         DiscardUntilEndOfDirective();
470         CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
471                                        /*foundnonskip*/false,
472                                        /*foundelse*/false);
473       }
474     } else if (Directive[0] == 'e') {
475       StringRef Sub = Directive.substr(1);
476       if (Sub == "ndif") {  // "endif"
477         PPConditionalInfo CondInfo;
478         CondInfo.WasSkipping = true; // Silence bogus warning.
479         bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
480         (void)InCond;  // Silence warning in no-asserts mode.
481         assert(!InCond && "Can't be skipping if not in a conditional!");
482
483         // If we popped the outermost skipping block, we're done skipping!
484         if (!CondInfo.WasSkipping) {
485           // Restore the value of LexingRawMode so that trailing comments
486           // are handled correctly, if we've reached the outermost block.
487           CurPPLexer->LexingRawMode = false;
488           CheckEndOfDirective("endif");
489           CurPPLexer->LexingRawMode = true;
490           if (Callbacks)
491             Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
492           break;
493         } else {
494           DiscardUntilEndOfDirective();
495         }
496       } else if (Sub == "lse") { // "else".
497         // #else directive in a skipping conditional.  If not in some other
498         // skipping conditional, and if #else hasn't already been seen, enter it
499         // as a non-skipping conditional.
500         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
501
502         // If this is a #else with a #else before it, report the error.
503         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
504
505         // Note that we've seen a #else in this conditional.
506         CondInfo.FoundElse = true;
507
508         // If the conditional is at the top level, and the #if block wasn't
509         // entered, enter the #else block now.
510         if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
511           CondInfo.FoundNonSkip = true;
512           // Restore the value of LexingRawMode so that trailing comments
513           // are handled correctly.
514           CurPPLexer->LexingRawMode = false;
515           CheckEndOfDirective("else");
516           CurPPLexer->LexingRawMode = true;
517           if (Callbacks)
518             Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
519           break;
520         } else {
521           DiscardUntilEndOfDirective();  // C99 6.10p4.
522         }
523       } else if (Sub == "lif") {  // "elif".
524         PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
525
526         // If this is a #elif with a #else before it, report the error.
527         if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
528
529         // If this is in a skipping block or if we're already handled this #if
530         // block, don't bother parsing the condition.
531         if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
532           DiscardUntilEndOfDirective();
533         } else {
534           const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
535           // Restore the value of LexingRawMode so that identifiers are
536           // looked up, etc, inside the #elif expression.
537           assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
538           CurPPLexer->LexingRawMode = false;
539           IdentifierInfo *IfNDefMacro = nullptr;
540           const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
541           CurPPLexer->LexingRawMode = true;
542           if (Callbacks) {
543             const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
544             Callbacks->Elif(Tok.getLocation(),
545                             SourceRange(CondBegin, CondEnd),
546                             (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
547           }
548           // If this condition is true, enter it!
549           if (CondValue) {
550             CondInfo.FoundNonSkip = true;
551             break;
552           }
553         }
554       }
555     }
556
557     CurPPLexer->ParsingPreprocessorDirective = false;
558     // Restore comment saving mode.
559     if (CurLexer) CurLexer->resetExtendedTokenMode();
560   }
561
562   // Finally, if we are out of the conditional (saw an #endif or ran off the end
563   // of the file, just stop skipping and return to lexing whatever came after
564   // the #if block.
565   CurPPLexer->LexingRawMode = false;
566
567   if (Callbacks) {
568     SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
569     Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
570   }
571 }
572
573 void Preprocessor::PTHSkipExcludedConditionalBlock() {
574   while (true) {
575     assert(CurPTHLexer);
576     assert(CurPTHLexer->LexingRawMode == false);
577
578     // Skip to the next '#else', '#elif', or #endif.
579     if (CurPTHLexer->SkipBlock()) {
580       // We have reached an #endif.  Both the '#' and 'endif' tokens
581       // have been consumed by the PTHLexer.  Just pop off the condition level.
582       PPConditionalInfo CondInfo;
583       bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
584       (void)InCond;  // Silence warning in no-asserts mode.
585       assert(!InCond && "Can't be skipping if not in a conditional!");
586       break;
587     }
588
589     // We have reached a '#else' or '#elif'.  Lex the next token to get
590     // the directive flavor.
591     Token Tok;
592     LexUnexpandedToken(Tok);
593
594     // We can actually look up the IdentifierInfo here since we aren't in
595     // raw mode.
596     tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
597
598     if (K == tok::pp_else) {
599       // #else: Enter the else condition.  We aren't in a nested condition
600       //  since we skip those. We're always in the one matching the last
601       //  blocked we skipped.
602       PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
603       // Note that we've seen a #else in this conditional.
604       CondInfo.FoundElse = true;
605
606       // If the #if block wasn't entered then enter the #else block now.
607       if (!CondInfo.FoundNonSkip) {
608         CondInfo.FoundNonSkip = true;
609
610         // Scan until the eod token.
611         CurPTHLexer->ParsingPreprocessorDirective = true;
612         DiscardUntilEndOfDirective();
613         CurPTHLexer->ParsingPreprocessorDirective = false;
614
615         break;
616       }
617
618       // Otherwise skip this block.
619       continue;
620     }
621
622     assert(K == tok::pp_elif);
623     PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
624
625     // If this is a #elif with a #else before it, report the error.
626     if (CondInfo.FoundElse)
627       Diag(Tok, diag::pp_err_elif_after_else);
628
629     // If this is in a skipping block or if we're already handled this #if
630     // block, don't bother parsing the condition.  We just skip this block.
631     if (CondInfo.FoundNonSkip)
632       continue;
633
634     // Evaluate the condition of the #elif.
635     IdentifierInfo *IfNDefMacro = nullptr;
636     CurPTHLexer->ParsingPreprocessorDirective = true;
637     bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
638     CurPTHLexer->ParsingPreprocessorDirective = false;
639
640     // If this condition is true, enter it!
641     if (ShouldEnter) {
642       CondInfo.FoundNonSkip = true;
643       break;
644     }
645
646     // Otherwise, skip this block and go to the next one.
647   }
648 }
649
650 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
651   if (!SourceMgr.isInMainFile(Loc)) {
652     // Try to determine the module of the include directive.
653     // FIXME: Look into directly passing the FileEntry from LookupFile instead.
654     FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
655     if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
656       // The include comes from an included file.
657       return HeaderInfo.getModuleMap()
658           .findModuleForHeader(EntryOfIncl)
659           .getModule();
660     }
661   }
662
663   // This is either in the main file or not in a file at all. It belongs
664   // to the current module, if there is one.
665   return getLangOpts().CurrentModule.empty()
666              ? nullptr
667              : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
668 }
669
670 const FileEntry *
671 Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
672                                                      Module *M,
673                                                      SourceLocation Loc) {
674   assert(M && "no module to include");
675
676   // If we have a module import syntax, we shouldn't include a header to
677   // make a particular module visible.
678   if (getLangOpts().ObjC2)
679     return nullptr;
680
681   Module *TopM = M->getTopLevelModule();
682   Module *IncM = getModuleForLocation(IncLoc);
683
684   // Walk up through the include stack, looking through textual headers of M
685   // until we hit a non-textual header that we can #include. (We assume textual
686   // headers of a module with non-textual headers aren't meant to be used to
687   // import entities from the module.)
688   auto &SM = getSourceManager();
689   while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
690     auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
691     auto *FE = SM.getFileEntryForID(ID);
692
693     bool InTextualHeader = false;
694     for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
695       if (!Header.getModule()->isSubModuleOf(TopM))
696         continue;
697
698       if (!(Header.getRole() & ModuleMap::TextualHeader)) {
699         // If this is an accessible, non-textual header of M's top-level module
700         // that transitively includes the given location and makes the
701         // corresponding module visible, this is the thing to #include.
702         if (Header.isAccessibleFrom(IncM))
703           return FE;
704
705         // It's in a private header; we can't #include it.
706         // FIXME: If there's a public header in some module that re-exports it,
707         // then we could suggest including that, but it's not clear that's the
708         // expected way to make this entity visible.
709         continue;
710       }
711
712       InTextualHeader = true;
713     }
714
715     if (!InTextualHeader)
716       break;
717
718     Loc = SM.getIncludeLoc(ID);
719   }
720
721   return nullptr;
722 }
723
724 const FileEntry *Preprocessor::LookupFile(
725     SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
726     const DirectoryLookup *FromDir, const FileEntry *FromFile,
727     const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath,
728     SmallVectorImpl<char> *RelativePath,
729     ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool SkipCache) {
730   Module *RequestingModule = getModuleForLocation(FilenameLoc);
731   bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
732
733   // If the header lookup mechanism may be relative to the current inclusion
734   // stack, record the parent #includes.
735   SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
736       Includers;
737   bool BuildSystemModule = false;
738   if (!FromDir && !FromFile) {
739     FileID FID = getCurrentFileLexer()->getFileID();
740     const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
741
742     // If there is no file entry associated with this file, it must be the
743     // predefines buffer or the module includes buffer. Any other file is not
744     // lexed with a normal lexer, so it won't be scanned for preprocessor
745     // directives.
746     //
747     // If we have the predefines buffer, resolve #include references (which come
748     // from the -include command line argument) from the current working
749     // directory instead of relative to the main file.
750     //
751     // If we have the module includes buffer, resolve #include references (which
752     // come from header declarations in the module map) relative to the module
753     // map file.
754     if (!FileEnt) {
755       if (FID == SourceMgr.getMainFileID() && MainFileDir) {
756         Includers.push_back(std::make_pair(nullptr, MainFileDir));
757         BuildSystemModule = getCurrentModule()->IsSystem;
758       } else if ((FileEnt =
759                     SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
760         Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
761     } else {
762       Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
763     }
764
765     // MSVC searches the current include stack from top to bottom for
766     // headers included by quoted include directives.
767     // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
768     if (LangOpts.MSVCCompat && !isAngled) {
769       for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
770         if (IsFileLexer(ISEntry))
771           if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
772             Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
773       }
774     }
775   }
776
777   CurDir = CurDirLookup;
778
779   if (FromFile) {
780     // We're supposed to start looking from after a particular file. Search
781     // the include path until we find that file or run out of files.
782     const DirectoryLookup *TmpCurDir = CurDir;
783     const DirectoryLookup *TmpFromDir = nullptr;
784     while (const FileEntry *FE = HeaderInfo.LookupFile(
785                Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
786                Includers, SearchPath, RelativePath, RequestingModule,
787                SuggestedModule, /*IsMapped=*/nullptr, SkipCache)) {
788       // Keep looking as if this file did a #include_next.
789       TmpFromDir = TmpCurDir;
790       ++TmpFromDir;
791       if (FE == FromFile) {
792         // Found it.
793         FromDir = TmpFromDir;
794         CurDir = TmpCurDir;
795         break;
796       }
797     }
798   }
799
800   // Do a standard file entry lookup.
801   const FileEntry *FE = HeaderInfo.LookupFile(
802       Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
803       RelativePath, RequestingModule, SuggestedModule, IsMapped, SkipCache,
804       BuildSystemModule);
805   if (FE) {
806     if (SuggestedModule && !LangOpts.AsmPreprocessor)
807       HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
808           RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
809           Filename, FE);
810     return FE;
811   }
812
813   const FileEntry *CurFileEnt;
814   // Otherwise, see if this is a subframework header.  If so, this is relative
815   // to one of the headers on the #include stack.  Walk the list of the current
816   // headers on the #include stack and pass them to HeaderInfo.
817   if (IsFileLexer()) {
818     if ((CurFileEnt = CurPPLexer->getFileEntry())) {
819       if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
820                                                     SearchPath, RelativePath,
821                                                     RequestingModule,
822                                                     SuggestedModule))) {
823         if (SuggestedModule && !LangOpts.AsmPreprocessor)
824           HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
825               RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
826               Filename, FE);
827         return FE;
828       }
829     }
830   }
831
832   for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
833     if (IsFileLexer(ISEntry)) {
834       if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
835         if ((FE = HeaderInfo.LookupSubframeworkHeader(
836                 Filename, CurFileEnt, SearchPath, RelativePath,
837                 RequestingModule, SuggestedModule))) {
838           if (SuggestedModule && !LangOpts.AsmPreprocessor)
839             HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
840                 RequestingModule, RequestingModuleIsModuleInterface,
841                 FilenameLoc, Filename, FE);
842           return FE;
843         }
844       }
845     }
846   }
847
848   // Otherwise, we really couldn't find the file.
849   return nullptr;
850 }
851
852 //===----------------------------------------------------------------------===//
853 // Preprocessor Directive Handling.
854 //===----------------------------------------------------------------------===//
855
856 class Preprocessor::ResetMacroExpansionHelper {
857 public:
858   ResetMacroExpansionHelper(Preprocessor *pp)
859     : PP(pp), save(pp->DisableMacroExpansion) {
860     if (pp->MacroExpansionInDirectivesOverride)
861       pp->DisableMacroExpansion = false;
862   }
863
864   ~ResetMacroExpansionHelper() {
865     PP->DisableMacroExpansion = save;
866   }
867
868 private:
869   Preprocessor *PP;
870   bool save;
871 };
872
873 /// HandleDirective - This callback is invoked when the lexer sees a # token
874 /// at the start of a line.  This consumes the directive, modifies the
875 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
876 /// read is the correct one.
877 void Preprocessor::HandleDirective(Token &Result) {
878   // FIXME: Traditional: # with whitespace before it not recognized by K&R?
879
880   // We just parsed a # character at the start of a line, so we're in directive
881   // mode.  Tell the lexer this so any newlines we see will be converted into an
882   // EOD token (which terminates the directive).
883   CurPPLexer->ParsingPreprocessorDirective = true;
884   if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
885
886   bool ImmediatelyAfterTopLevelIfndef =
887       CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
888   CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
889
890   ++NumDirectives;
891
892   // We are about to read a token.  For the multiple-include optimization FA to
893   // work, we have to remember if we had read any tokens *before* this
894   // pp-directive.
895   bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
896
897   // Save the '#' token in case we need to return it later.
898   Token SavedHash = Result;
899
900   // Read the next token, the directive flavor.  This isn't expanded due to
901   // C99 6.10.3p8.
902   LexUnexpandedToken(Result);
903
904   // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.:
905   //   #define A(x) #x
906   //   A(abc
907   //     #warning blah
908   //   def)
909   // If so, the user is relying on undefined behavior, emit a diagnostic. Do
910   // not support this for #include-like directives, since that can result in
911   // terrible diagnostics, and does not work in GCC.
912   if (InMacroArgs) {
913     if (IdentifierInfo *II = Result.getIdentifierInfo()) {
914       switch (II->getPPKeywordID()) {
915       case tok::pp_include:
916       case tok::pp_import:
917       case tok::pp_include_next:
918       case tok::pp___include_macros:
919       case tok::pp_pragma:
920         Diag(Result, diag::err_embedded_directive) << II->getName();
921         DiscardUntilEndOfDirective();
922         return;
923       default:
924         break;
925       }
926     }
927     Diag(Result, diag::ext_embedded_directive);
928   }
929
930   // Temporarily enable macro expansion if set so
931   // and reset to previous state when returning from this function.
932   ResetMacroExpansionHelper helper(this);
933
934   switch (Result.getKind()) {
935   case tok::eod:
936     return;   // null directive.
937   case tok::code_completion:
938     if (CodeComplete)
939       CodeComplete->CodeCompleteDirective(
940                                     CurPPLexer->getConditionalStackDepth() > 0);
941     setCodeCompletionReached();
942     return;
943   case tok::numeric_constant:  // # 7  GNU line marker directive.
944     if (getLangOpts().AsmPreprocessor)
945       break;  // # 4 is not a preprocessor directive in .S files.
946     return HandleDigitDirective(Result);
947   default:
948     IdentifierInfo *II = Result.getIdentifierInfo();
949     if (!II) break; // Not an identifier.
950
951     // Ask what the preprocessor keyword ID is.
952     switch (II->getPPKeywordID()) {
953     default: break;
954     // C99 6.10.1 - Conditional Inclusion.
955     case tok::pp_if:
956       return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
957     case tok::pp_ifdef:
958       return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
959     case tok::pp_ifndef:
960       return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
961     case tok::pp_elif:
962       return HandleElifDirective(Result);
963     case tok::pp_else:
964       return HandleElseDirective(Result);
965     case tok::pp_endif:
966       return HandleEndifDirective(Result);
967
968     // C99 6.10.2 - Source File Inclusion.
969     case tok::pp_include:
970       // Handle #include.
971       return HandleIncludeDirective(SavedHash.getLocation(), Result);
972     case tok::pp___include_macros:
973       // Handle -imacros.
974       return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
975
976     // C99 6.10.3 - Macro Replacement.
977     case tok::pp_define:
978       return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
979     case tok::pp_undef:
980       return HandleUndefDirective();
981
982     // C99 6.10.4 - Line Control.
983     case tok::pp_line:
984       return HandleLineDirective();
985
986     // C99 6.10.5 - Error Directive.
987     case tok::pp_error:
988       return HandleUserDiagnosticDirective(Result, false);
989
990     // C99 6.10.6 - Pragma Directive.
991     case tok::pp_pragma:
992       return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
993
994     // GNU Extensions.
995     case tok::pp_import:
996       return HandleImportDirective(SavedHash.getLocation(), Result);
997     case tok::pp_include_next:
998       return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
999
1000     case tok::pp_warning:
1001       Diag(Result, diag::ext_pp_warning_directive);
1002       return HandleUserDiagnosticDirective(Result, true);
1003     case tok::pp_ident:
1004       return HandleIdentSCCSDirective(Result);
1005     case tok::pp_sccs:
1006       return HandleIdentSCCSDirective(Result);
1007     case tok::pp_assert:
1008       //isExtension = true;  // FIXME: implement #assert
1009       break;
1010     case tok::pp_unassert:
1011       //isExtension = true;  // FIXME: implement #unassert
1012       break;
1013
1014     case tok::pp___public_macro:
1015       if (getLangOpts().Modules)
1016         return HandleMacroPublicDirective(Result);
1017       break;
1018
1019     case tok::pp___private_macro:
1020       if (getLangOpts().Modules)
1021         return HandleMacroPrivateDirective();
1022       break;
1023     }
1024     break;
1025   }
1026
1027   // If this is a .S file, treat unknown # directives as non-preprocessor
1028   // directives.  This is important because # may be a comment or introduce
1029   // various pseudo-ops.  Just return the # token and push back the following
1030   // token to be lexed next time.
1031   if (getLangOpts().AsmPreprocessor) {
1032     auto Toks = llvm::make_unique<Token[]>(2);
1033     // Return the # and the token after it.
1034     Toks[0] = SavedHash;
1035     Toks[1] = Result;
1036
1037     // If the second token is a hashhash token, then we need to translate it to
1038     // unknown so the token lexer doesn't try to perform token pasting.
1039     if (Result.is(tok::hashhash))
1040       Toks[1].setKind(tok::unknown);
1041
1042     // Enter this token stream so that we re-lex the tokens.  Make sure to
1043     // enable macro expansion, in case the token after the # is an identifier
1044     // that is expanded.
1045     EnterTokenStream(std::move(Toks), 2, false);
1046     return;
1047   }
1048
1049   // If we reached here, the preprocessing token is not valid!
1050   Diag(Result, diag::err_pp_invalid_directive);
1051
1052   // Read the rest of the PP line.
1053   DiscardUntilEndOfDirective();
1054
1055   // Okay, we're done parsing the directive.
1056 }
1057
1058 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1059 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1060 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1061                          unsigned DiagID, Preprocessor &PP,
1062                          bool IsGNULineDirective=false) {
1063   if (DigitTok.isNot(tok::numeric_constant)) {
1064     PP.Diag(DigitTok, DiagID);
1065
1066     if (DigitTok.isNot(tok::eod))
1067       PP.DiscardUntilEndOfDirective();
1068     return true;
1069   }
1070
1071   SmallString<64> IntegerBuffer;
1072   IntegerBuffer.resize(DigitTok.getLength());
1073   const char *DigitTokBegin = &IntegerBuffer[0];
1074   bool Invalid = false;
1075   unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1076   if (Invalid)
1077     return true;
1078
1079   // Verify that we have a simple digit-sequence, and compute the value.  This
1080   // is always a simple digit string computed in decimal, so we do this manually
1081   // here.
1082   Val = 0;
1083   for (unsigned i = 0; i != ActualLength; ++i) {
1084     // C++1y [lex.fcon]p1:
1085     //   Optional separating single quotes in a digit-sequence are ignored
1086     if (DigitTokBegin[i] == '\'')
1087       continue;
1088
1089     if (!isDigit(DigitTokBegin[i])) {
1090       PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1091               diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1092       PP.DiscardUntilEndOfDirective();
1093       return true;
1094     }
1095
1096     unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1097     if (NextVal < Val) { // overflow.
1098       PP.Diag(DigitTok, DiagID);
1099       PP.DiscardUntilEndOfDirective();
1100       return true;
1101     }
1102     Val = NextVal;
1103   }
1104
1105   if (DigitTokBegin[0] == '0' && Val)
1106     PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1107       << IsGNULineDirective;
1108
1109   return false;
1110 }
1111
1112 /// \brief Handle a \#line directive: C99 6.10.4.
1113 ///
1114 /// The two acceptable forms are:
1115 /// \verbatim
1116 ///   # line digit-sequence
1117 ///   # line digit-sequence "s-char-sequence"
1118 /// \endverbatim
1119 void Preprocessor::HandleLineDirective() {
1120   // Read the line # and string argument.  Per C99 6.10.4p5, these tokens are
1121   // expanded.
1122   Token DigitTok;
1123   Lex(DigitTok);
1124
1125   // Validate the number and convert it to an unsigned.
1126   unsigned LineNo;
1127   if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1128     return;
1129
1130   if (LineNo == 0)
1131     Diag(DigitTok, diag::ext_pp_line_zero);
1132
1133   // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1134   // number greater than 2147483647".  C90 requires that the line # be <= 32767.
1135   unsigned LineLimit = 32768U;
1136   if (LangOpts.C99 || LangOpts.CPlusPlus11)
1137     LineLimit = 2147483648U;
1138   if (LineNo >= LineLimit)
1139     Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1140   else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1141     Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1142
1143   int FilenameID = -1;
1144   Token StrTok;
1145   Lex(StrTok);
1146
1147   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
1148   // string followed by eod.
1149   if (StrTok.is(tok::eod))
1150     ; // ok
1151   else if (StrTok.isNot(tok::string_literal)) {
1152     Diag(StrTok, diag::err_pp_line_invalid_filename);
1153     return DiscardUntilEndOfDirective();
1154   } else if (StrTok.hasUDSuffix()) {
1155     Diag(StrTok, diag::err_invalid_string_udl);
1156     return DiscardUntilEndOfDirective();
1157   } else {
1158     // Parse and validate the string, converting it into a unique ID.
1159     StringLiteralParser Literal(StrTok, *this);
1160     assert(Literal.isAscii() && "Didn't allow wide strings in");
1161     if (Literal.hadError)
1162       return DiscardUntilEndOfDirective();
1163     if (Literal.Pascal) {
1164       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1165       return DiscardUntilEndOfDirective();
1166     }
1167     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1168
1169     // Verify that there is nothing after the string, other than EOD.  Because
1170     // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1171     CheckEndOfDirective("line", true);
1172   }
1173
1174   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
1175
1176   if (Callbacks)
1177     Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1178                            PPCallbacks::RenameFile,
1179                            SrcMgr::C_User);
1180 }
1181
1182 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1183 /// marker directive.
1184 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1185                                 bool &IsSystemHeader, bool &IsExternCHeader,
1186                                 Preprocessor &PP) {
1187   unsigned FlagVal;
1188   Token FlagTok;
1189   PP.Lex(FlagTok);
1190   if (FlagTok.is(tok::eod)) return false;
1191   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1192     return true;
1193
1194   if (FlagVal == 1) {
1195     IsFileEntry = true;
1196
1197     PP.Lex(FlagTok);
1198     if (FlagTok.is(tok::eod)) return false;
1199     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1200       return true;
1201   } else if (FlagVal == 2) {
1202     IsFileExit = true;
1203
1204     SourceManager &SM = PP.getSourceManager();
1205     // If we are leaving the current presumed file, check to make sure the
1206     // presumed include stack isn't empty!
1207     FileID CurFileID =
1208       SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1209     PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1210     if (PLoc.isInvalid())
1211       return true;
1212
1213     // If there is no include loc (main file) or if the include loc is in a
1214     // different physical file, then we aren't in a "1" line marker flag region.
1215     SourceLocation IncLoc = PLoc.getIncludeLoc();
1216     if (IncLoc.isInvalid() ||
1217         SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1218       PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1219       PP.DiscardUntilEndOfDirective();
1220       return true;
1221     }
1222
1223     PP.Lex(FlagTok);
1224     if (FlagTok.is(tok::eod)) return false;
1225     if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1226       return true;
1227   }
1228
1229   // We must have 3 if there are still flags.
1230   if (FlagVal != 3) {
1231     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1232     PP.DiscardUntilEndOfDirective();
1233     return true;
1234   }
1235
1236   IsSystemHeader = true;
1237
1238   PP.Lex(FlagTok);
1239   if (FlagTok.is(tok::eod)) return false;
1240   if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1241     return true;
1242
1243   // We must have 4 if there is yet another flag.
1244   if (FlagVal != 4) {
1245     PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1246     PP.DiscardUntilEndOfDirective();
1247     return true;
1248   }
1249
1250   IsExternCHeader = true;
1251
1252   PP.Lex(FlagTok);
1253   if (FlagTok.is(tok::eod)) return false;
1254
1255   // There are no more valid flags here.
1256   PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1257   PP.DiscardUntilEndOfDirective();
1258   return true;
1259 }
1260
1261 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1262 /// one of the following forms:
1263 ///
1264 ///     # 42
1265 ///     # 42 "file" ('1' | '2')?
1266 ///     # 42 "file" ('1' | '2')? '3' '4'?
1267 ///
1268 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1269   // Validate the number and convert it to an unsigned.  GNU does not have a
1270   // line # limit other than it fit in 32-bits.
1271   unsigned LineNo;
1272   if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1273                    *this, true))
1274     return;
1275
1276   Token StrTok;
1277   Lex(StrTok);
1278
1279   bool IsFileEntry = false, IsFileExit = false;
1280   bool IsSystemHeader = false, IsExternCHeader = false;
1281   int FilenameID = -1;
1282
1283   // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
1284   // string followed by eod.
1285   if (StrTok.is(tok::eod))
1286     ; // ok
1287   else if (StrTok.isNot(tok::string_literal)) {
1288     Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1289     return DiscardUntilEndOfDirective();
1290   } else if (StrTok.hasUDSuffix()) {
1291     Diag(StrTok, diag::err_invalid_string_udl);
1292     return DiscardUntilEndOfDirective();
1293   } else {
1294     // Parse and validate the string, converting it into a unique ID.
1295     StringLiteralParser Literal(StrTok, *this);
1296     assert(Literal.isAscii() && "Didn't allow wide strings in");
1297     if (Literal.hadError)
1298       return DiscardUntilEndOfDirective();
1299     if (Literal.Pascal) {
1300       Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1301       return DiscardUntilEndOfDirective();
1302     }
1303     FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1304
1305     // If a filename was present, read any flags that are present.
1306     if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1307                             IsSystemHeader, IsExternCHeader, *this))
1308       return;
1309   }
1310
1311   // Create a line note with this information.
1312   SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1313                         IsFileEntry, IsFileExit,
1314                         IsSystemHeader, IsExternCHeader);
1315
1316   // If the preprocessor has callbacks installed, notify them of the #line
1317   // change.  This is used so that the line marker comes out in -E mode for
1318   // example.
1319   if (Callbacks) {
1320     PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1321     if (IsFileEntry)
1322       Reason = PPCallbacks::EnterFile;
1323     else if (IsFileExit)
1324       Reason = PPCallbacks::ExitFile;
1325     SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1326     if (IsExternCHeader)
1327       FileKind = SrcMgr::C_ExternCSystem;
1328     else if (IsSystemHeader)
1329       FileKind = SrcMgr::C_System;
1330
1331     Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1332   }
1333 }
1334
1335 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1336 ///
1337 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1338                                                  bool isWarning) {
1339   // PTH doesn't emit #warning or #error directives.
1340   if (CurPTHLexer)
1341     return CurPTHLexer->DiscardToEndOfLine();
1342
1343   // Read the rest of the line raw.  We do this because we don't want macros
1344   // to be expanded and we don't require that the tokens be valid preprocessing
1345   // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does
1346   // collapse multiple consequtive white space between tokens, but this isn't
1347   // specified by the standard.
1348   SmallString<128> Message;
1349   CurLexer->ReadToEndOfLine(&Message);
1350
1351   // Find the first non-whitespace character, so that we can make the
1352   // diagnostic more succinct.
1353   StringRef Msg = StringRef(Message).ltrim(' ');
1354
1355   if (isWarning)
1356     Diag(Tok, diag::pp_hash_warning) << Msg;
1357   else
1358     Diag(Tok, diag::err_pp_hash_error) << Msg;
1359 }
1360
1361 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1362 ///
1363 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1364   // Yes, this directive is an extension.
1365   Diag(Tok, diag::ext_pp_ident_directive);
1366
1367   // Read the string argument.
1368   Token StrTok;
1369   Lex(StrTok);
1370
1371   // If the token kind isn't a string, it's a malformed directive.
1372   if (StrTok.isNot(tok::string_literal) &&
1373       StrTok.isNot(tok::wide_string_literal)) {
1374     Diag(StrTok, diag::err_pp_malformed_ident);
1375     if (StrTok.isNot(tok::eod))
1376       DiscardUntilEndOfDirective();
1377     return;
1378   }
1379
1380   if (StrTok.hasUDSuffix()) {
1381     Diag(StrTok, diag::err_invalid_string_udl);
1382     return DiscardUntilEndOfDirective();
1383   }
1384
1385   // Verify that there is nothing after the string, other than EOD.
1386   CheckEndOfDirective("ident");
1387
1388   if (Callbacks) {
1389     bool Invalid = false;
1390     std::string Str = getSpelling(StrTok, &Invalid);
1391     if (!Invalid)
1392       Callbacks->Ident(Tok.getLocation(), Str);
1393   }
1394 }
1395
1396 /// \brief Handle a #public directive.
1397 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1398   Token MacroNameTok;
1399   ReadMacroName(MacroNameTok, MU_Undef);
1400
1401   // Error reading macro name?  If so, diagnostic already issued.
1402   if (MacroNameTok.is(tok::eod))
1403     return;
1404
1405   // Check to see if this is the last token on the #__public_macro line.
1406   CheckEndOfDirective("__public_macro");
1407
1408   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1409   // Okay, we finally have a valid identifier to undef.
1410   MacroDirective *MD = getLocalMacroDirective(II);
1411
1412   // If the macro is not defined, this is an error.
1413   if (!MD) {
1414     Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1415     return;
1416   }
1417
1418   // Note that this macro has now been exported.
1419   appendMacroDirective(II, AllocateVisibilityMacroDirective(
1420                                 MacroNameTok.getLocation(), /*IsPublic=*/true));
1421 }
1422
1423 /// \brief Handle a #private directive.
1424 void Preprocessor::HandleMacroPrivateDirective() {
1425   Token MacroNameTok;
1426   ReadMacroName(MacroNameTok, MU_Undef);
1427
1428   // Error reading macro name?  If so, diagnostic already issued.
1429   if (MacroNameTok.is(tok::eod))
1430     return;
1431
1432   // Check to see if this is the last token on the #__private_macro line.
1433   CheckEndOfDirective("__private_macro");
1434
1435   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1436   // Okay, we finally have a valid identifier to undef.
1437   MacroDirective *MD = getLocalMacroDirective(II);
1438
1439   // If the macro is not defined, this is an error.
1440   if (!MD) {
1441     Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1442     return;
1443   }
1444
1445   // Note that this macro has now been marked private.
1446   appendMacroDirective(II, AllocateVisibilityMacroDirective(
1447                                MacroNameTok.getLocation(), /*IsPublic=*/false));
1448 }
1449
1450 //===----------------------------------------------------------------------===//
1451 // Preprocessor Include Directive Handling.
1452 //===----------------------------------------------------------------------===//
1453
1454 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1455 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1456 /// true if the input filename was in <>'s or false if it were in ""'s.  The
1457 /// caller is expected to provide a buffer that is large enough to hold the
1458 /// spelling of the filename, but is also expected to handle the case when
1459 /// this method decides to use a different buffer.
1460 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1461                                               StringRef &Buffer) {
1462   // Get the text form of the filename.
1463   assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1464
1465   // Make sure the filename is <x> or "x".
1466   bool isAngled;
1467   if (Buffer[0] == '<') {
1468     if (Buffer.back() != '>') {
1469       Diag(Loc, diag::err_pp_expects_filename);
1470       Buffer = StringRef();
1471       return true;
1472     }
1473     isAngled = true;
1474   } else if (Buffer[0] == '"') {
1475     if (Buffer.back() != '"') {
1476       Diag(Loc, diag::err_pp_expects_filename);
1477       Buffer = StringRef();
1478       return true;
1479     }
1480     isAngled = false;
1481   } else {
1482     Diag(Loc, diag::err_pp_expects_filename);
1483     Buffer = StringRef();
1484     return true;
1485   }
1486
1487   // Diagnose #include "" as invalid.
1488   if (Buffer.size() <= 2) {
1489     Diag(Loc, diag::err_pp_empty_filename);
1490     Buffer = StringRef();
1491     return true;
1492   }
1493
1494   // Skip the brackets.
1495   Buffer = Buffer.substr(1, Buffer.size()-2);
1496   return isAngled;
1497 }
1498
1499 // \brief Handle cases where the \#include name is expanded from a macro
1500 // as multiple tokens, which need to be glued together.
1501 //
1502 // This occurs for code like:
1503 // \code
1504 //    \#define FOO <a/b.h>
1505 //    \#include FOO
1506 // \endcode
1507 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1508 //
1509 // This code concatenates and consumes tokens up to the '>' token.  It returns
1510 // false if the > was found, otherwise it returns true if it finds and consumes
1511 // the EOD marker.
1512 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1513                                           SourceLocation &End) {
1514   Token CurTok;
1515
1516   Lex(CurTok);
1517   while (CurTok.isNot(tok::eod)) {
1518     End = CurTok.getLocation();
1519
1520     // FIXME: Provide code completion for #includes.
1521     if (CurTok.is(tok::code_completion)) {
1522       setCodeCompletionReached();
1523       Lex(CurTok);
1524       continue;
1525     }
1526
1527     // Append the spelling of this token to the buffer. If there was a space
1528     // before it, add it now.
1529     if (CurTok.hasLeadingSpace())
1530       FilenameBuffer.push_back(' ');
1531
1532     // Get the spelling of the token, directly into FilenameBuffer if possible.
1533     size_t PreAppendSize = FilenameBuffer.size();
1534     FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1535
1536     const char *BufPtr = &FilenameBuffer[PreAppendSize];
1537     unsigned ActualLen = getSpelling(CurTok, BufPtr);
1538
1539     // If the token was spelled somewhere else, copy it into FilenameBuffer.
1540     if (BufPtr != &FilenameBuffer[PreAppendSize])
1541       memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1542
1543     // Resize FilenameBuffer to the correct size.
1544     if (CurTok.getLength() != ActualLen)
1545       FilenameBuffer.resize(PreAppendSize+ActualLen);
1546
1547     // If we found the '>' marker, return success.
1548     if (CurTok.is(tok::greater))
1549       return false;
1550
1551     Lex(CurTok);
1552   }
1553
1554   // If we hit the eod marker, emit an error and return true so that the caller
1555   // knows the EOD has been read.
1556   Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1557   return true;
1558 }
1559
1560 /// \brief Push a token onto the token stream containing an annotation.
1561 void Preprocessor::EnterAnnotationToken(SourceRange Range,
1562                                         tok::TokenKind Kind,
1563                                         void *AnnotationVal) {
1564   // FIXME: Produce this as the current token directly, rather than
1565   // allocating a new token for it.
1566   auto Tok = llvm::make_unique<Token[]>(1);
1567   Tok[0].startToken();
1568   Tok[0].setKind(Kind);
1569   Tok[0].setLocation(Range.getBegin());
1570   Tok[0].setAnnotationEndLoc(Range.getEnd());
1571   Tok[0].setAnnotationValue(AnnotationVal);
1572   EnterTokenStream(std::move(Tok), 1, true);
1573 }
1574
1575 /// \brief Produce a diagnostic informing the user that a #include or similar
1576 /// was implicitly treated as a module import.
1577 static void diagnoseAutoModuleImport(
1578     Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1579     ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1580     SourceLocation PathEnd) {
1581   assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1582
1583   SmallString<128> PathString;
1584   for (size_t I = 0, N = Path.size(); I != N; ++I) {
1585     if (I)
1586       PathString += '.';
1587     PathString += Path[I].first->getName();
1588   }
1589   int IncludeKind = 0;
1590
1591   switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1592   case tok::pp_include:
1593     IncludeKind = 0;
1594     break;
1595
1596   case tok::pp_import:
1597     IncludeKind = 1;
1598     break;
1599
1600   case tok::pp_include_next:
1601     IncludeKind = 2;
1602     break;
1603
1604   case tok::pp___include_macros:
1605     IncludeKind = 3;
1606     break;
1607
1608   default:
1609     llvm_unreachable("unknown include directive kind");
1610   }
1611
1612   CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1613                                /*IsTokenRange=*/false);
1614   PP.Diag(HashLoc, diag::warn_auto_module_import)
1615       << IncludeKind << PathString
1616       << FixItHint::CreateReplacement(ReplaceRange,
1617                                       ("@import " + PathString + ";").str());
1618 }
1619
1620 // Given a vector of path components and a string containing the real
1621 // path to the file, build a properly-cased replacement in the vector,
1622 // and return true if the replacement should be suggested.
1623 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1624                             StringRef RealPathName) {
1625   auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1626   auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1627   int Cnt = 0;
1628   bool SuggestReplacement = false;
1629   // Below is a best-effort to handle ".." in paths. It is admittedly
1630   // not 100% correct in the presence of symlinks.
1631   for (auto &Component : llvm::reverse(Components)) {
1632     if ("." == Component) {
1633     } else if (".." == Component) {
1634       ++Cnt;
1635     } else if (Cnt) {
1636       --Cnt;
1637     } else if (RealPathComponentIter != RealPathComponentEnd) {
1638       if (Component != *RealPathComponentIter) {
1639         // If these path components differ by more than just case, then we
1640         // may be looking at symlinked paths. Bail on this diagnostic to avoid
1641         // noisy false positives.
1642         SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1643         if (!SuggestReplacement)
1644           break;
1645         Component = *RealPathComponentIter;
1646       }
1647       ++RealPathComponentIter;
1648     }
1649   }
1650   return SuggestReplacement;
1651 }
1652
1653 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1654 /// the file to be included from the lexer, then include it!  This is a common
1655 /// routine with functionality shared between \#include, \#include_next and
1656 /// \#import.  LookupFrom is set when this is a \#include_next directive, it
1657 /// specifies the file to start searching from.
1658 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1659                                           Token &IncludeTok,
1660                                           const DirectoryLookup *LookupFrom,
1661                                           const FileEntry *LookupFromFile,
1662                                           bool isImport) {
1663   Token FilenameTok;
1664   CurPPLexer->LexIncludeFilename(FilenameTok);
1665
1666   // Reserve a buffer to get the spelling.
1667   SmallString<128> FilenameBuffer;
1668   StringRef Filename;
1669   SourceLocation End;
1670   SourceLocation CharEnd; // the end of this directive, in characters
1671
1672   switch (FilenameTok.getKind()) {
1673   case tok::eod:
1674     // If the token kind is EOD, the error has already been diagnosed.
1675     return;
1676
1677   case tok::angle_string_literal:
1678   case tok::string_literal:
1679     Filename = getSpelling(FilenameTok, FilenameBuffer);
1680     End = FilenameTok.getLocation();
1681     CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1682     break;
1683
1684   case tok::less:
1685     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
1686     // case, glue the tokens together into FilenameBuffer and interpret those.
1687     FilenameBuffer.push_back('<');
1688     if (ConcatenateIncludeName(FilenameBuffer, End))
1689       return;   // Found <eod> but no ">"?  Diagnostic already emitted.
1690     Filename = FilenameBuffer;
1691     CharEnd = End.getLocWithOffset(1);
1692     break;
1693   default:
1694     Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1695     DiscardUntilEndOfDirective();
1696     return;
1697   }
1698
1699   CharSourceRange FilenameRange
1700     = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1701   StringRef OriginalFilename = Filename;
1702   bool isAngled =
1703     GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1704   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1705   // error.
1706   if (Filename.empty()) {
1707     DiscardUntilEndOfDirective();
1708     return;
1709   }
1710
1711   // Verify that there is nothing after the filename, other than EOD.  Note that
1712   // we allow macros that expand to nothing after the filename, because this
1713   // falls into the category of "#include pp-tokens new-line" specified in
1714   // C99 6.10.2p4.
1715   CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1716
1717   // Check that we don't have infinite #include recursion.
1718   if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1719     Diag(FilenameTok, diag::err_pp_include_too_deep);
1720     return;
1721   }
1722
1723   // Complain about attempts to #include files in an audit pragma.
1724   if (PragmaARCCFCodeAuditedLoc.isValid()) {
1725     Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1726     Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1727
1728     // Immediately leave the pragma.
1729     PragmaARCCFCodeAuditedLoc = SourceLocation();
1730   }
1731
1732   // Complain about attempts to #include files in an assume-nonnull pragma.
1733   if (PragmaAssumeNonNullLoc.isValid()) {
1734     Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1735     Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1736
1737     // Immediately leave the pragma.
1738     PragmaAssumeNonNullLoc = SourceLocation();
1739   }
1740
1741   if (HeaderInfo.HasIncludeAliasMap()) {
1742     // Map the filename with the brackets still attached.  If the name doesn't
1743     // map to anything, fall back on the filename we've already gotten the
1744     // spelling for.
1745     StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1746     if (!NewName.empty())
1747       Filename = NewName;
1748   }
1749
1750   // Search include directories.
1751   bool IsMapped = false;
1752   const DirectoryLookup *CurDir;
1753   SmallString<1024> SearchPath;
1754   SmallString<1024> RelativePath;
1755   // We get the raw path only if we have 'Callbacks' to which we later pass
1756   // the path.
1757   ModuleMap::KnownHeader SuggestedModule;
1758   SourceLocation FilenameLoc = FilenameTok.getLocation();
1759   SmallString<128> NormalizedPath;
1760   if (LangOpts.MSVCCompat) {
1761     NormalizedPath = Filename.str();
1762 #ifndef LLVM_ON_WIN32
1763     llvm::sys::path::native(NormalizedPath);
1764 #endif
1765   }
1766   const FileEntry *File = LookupFile(
1767       FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1768       isAngled, LookupFrom, LookupFromFile, CurDir,
1769       Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1770       &SuggestedModule, &IsMapped);
1771
1772   if (!File) {
1773     if (Callbacks) {
1774       // Give the clients a chance to recover.
1775       SmallString<128> RecoveryPath;
1776       if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1777         if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1778           // Add the recovery path to the list of search paths.
1779           DirectoryLookup DL(DE, SrcMgr::C_User, false);
1780           HeaderInfo.AddSearchPath(DL, isAngled);
1781
1782           // Try the lookup again, skipping the cache.
1783           File = LookupFile(
1784               FilenameLoc,
1785               LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1786               LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1787               &SuggestedModule, &IsMapped, /*SkipCache*/ true);
1788         }
1789       }
1790     }
1791
1792     if (!SuppressIncludeNotFoundError) {
1793       // If the file could not be located and it was included via angle
1794       // brackets, we can attempt a lookup as though it were a quoted path to
1795       // provide the user with a possible fixit.
1796       if (isAngled) {
1797         File = LookupFile(
1798             FilenameLoc,
1799             LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1800             LookupFrom, LookupFromFile, CurDir,
1801             Callbacks ? &SearchPath : nullptr,
1802             Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1803         if (File) {
1804           SourceRange Range(FilenameTok.getLocation(), CharEnd);
1805           Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1806             Filename <<
1807             FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1808         }
1809       }
1810
1811       // If the file is still not found, just go with the vanilla diagnostic
1812       if (!File)
1813         Diag(FilenameTok, diag::err_pp_file_not_found) << Filename
1814                                                        << FilenameRange;
1815     }
1816   }
1817
1818   // Should we enter the source file? Set to false if either the source file is
1819   // known to have no effect beyond its effect on module visibility -- that is,
1820   // if it's got an include guard that is already defined or is a modular header
1821   // we've imported or already built.
1822   bool ShouldEnter = true;
1823
1824   // Determine whether we should try to import the module for this #include, if
1825   // there is one. Don't do so if precompiled module support is disabled or we
1826   // are processing this module textually (because we're building the module).
1827   if (File && SuggestedModule && getLangOpts().Modules &&
1828       SuggestedModule.getModule()->getTopLevelModuleName() !=
1829           getLangOpts().CurrentModule) {
1830     // If this include corresponds to a module but that module is
1831     // unavailable, diagnose the situation and bail out.
1832     // FIXME: Remove this; loadModule does the same check (but produces
1833     // slightly worse diagnostics).
1834     if (!SuggestedModule.getModule()->isAvailable()) {
1835       Module::Requirement Requirement;
1836       Module::UnresolvedHeaderDirective MissingHeader;
1837       Module *M = SuggestedModule.getModule();
1838       // Identify the cause.
1839       (void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
1840                            MissingHeader);
1841       if (MissingHeader.FileNameLoc.isValid()) {
1842         Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1843             << MissingHeader.IsUmbrella << MissingHeader.FileName;
1844       } else {
1845         Diag(M->DefinitionLoc, diag::err_module_unavailable)
1846             << M->getFullModuleName() << Requirement.second << Requirement.first;
1847       }
1848       Diag(FilenameTok.getLocation(),
1849            diag::note_implicit_top_level_module_import_here)
1850           << M->getTopLevelModuleName();
1851       return;
1852     }
1853
1854     // Compute the module access path corresponding to this module.
1855     // FIXME: Should we have a second loadModule() overload to avoid this
1856     // extra lookup step?
1857     SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1858     for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1859       Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1860                                     FilenameTok.getLocation()));
1861     std::reverse(Path.begin(), Path.end());
1862
1863     // Warn that we're replacing the include/import with a module import.
1864     // We only do this in Objective-C, where we have a module-import syntax.
1865     if (getLangOpts().ObjC2)
1866       diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1867
1868     // Load the module to import its macros. We'll make the declarations
1869     // visible when the parser gets here.
1870     // FIXME: Pass SuggestedModule in here rather than converting it to a path
1871     // and making the module loader convert it back again.
1872     ModuleLoadResult Imported = TheModuleLoader.loadModule(
1873         IncludeTok.getLocation(), Path, Module::Hidden,
1874         /*IsIncludeDirective=*/true);
1875     assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1876            "the imported module is different than the suggested one");
1877
1878     if (Imported)
1879       ShouldEnter = false;
1880     else if (Imported.isMissingExpected()) {
1881       // We failed to find a submodule that we assumed would exist (because it
1882       // was in the directory of an umbrella header, for instance), but no
1883       // actual module containing it exists (because the umbrella header is
1884       // incomplete).  Treat this as a textual inclusion.
1885       SuggestedModule = ModuleMap::KnownHeader();
1886     } else if (Imported.isConfigMismatch()) {
1887       // On a configuration mismatch, enter the header textually. We still know
1888       // that it's part of the corresponding module.
1889     } else {
1890       // We hit an error processing the import. Bail out.
1891       if (hadModuleLoaderFatalFailure()) {
1892         // With a fatal failure in the module loader, we abort parsing.
1893         Token &Result = IncludeTok;
1894         if (CurLexer) {
1895           Result.startToken();
1896           CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1897           CurLexer->cutOffLexing();
1898         } else {
1899           assert(CurPTHLexer && "#include but no current lexer set!");
1900           CurPTHLexer->getEOF(Result);
1901         }
1902       }
1903       return;
1904     }
1905   }
1906
1907   if (Callbacks) {
1908     // Notify the callback object that we've seen an inclusion directive.
1909     Callbacks->InclusionDirective(
1910         HashLoc, IncludeTok,
1911         LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1912         FilenameRange, File, SearchPath, RelativePath,
1913         ShouldEnter ? nullptr : SuggestedModule.getModule());
1914   }
1915
1916   if (!File)
1917     return;
1918
1919   // The #included file will be considered to be a system header if either it is
1920   // in a system include directory, or if the #includer is a system include
1921   // header.
1922   SrcMgr::CharacteristicKind FileCharacter =
1923     std::max(HeaderInfo.getFileDirFlavor(File),
1924              SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1925
1926   // FIXME: If we have a suggested module, and we've already visited this file,
1927   // don't bother entering it again. We know it has no further effect.
1928
1929   // Issue a diagnostic if the name of the file on disk has a different case
1930   // than the one we're about to open.
1931   const bool CheckIncludePathPortability =
1932       !IsMapped && File && !File->tryGetRealPathName().empty();
1933
1934   if (CheckIncludePathPortability) {
1935     StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
1936     StringRef RealPathName = File->tryGetRealPathName();
1937     SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
1938                                           llvm::sys::path::end(Name));
1939
1940     if (trySimplifyPath(Components, RealPathName)) {
1941       SmallString<128> Path;
1942       Path.reserve(Name.size()+2);
1943       Path.push_back(isAngled ? '<' : '"');
1944       bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
1945       for (auto Component : Components) {
1946         if (isLeadingSeparator)
1947           isLeadingSeparator = false;
1948         else
1949           Path.append(Component);
1950         // Append the separator the user used, or the close quote
1951         Path.push_back(
1952           Path.size() <= Filename.size() ? Filename[Path.size()-1] :
1953             (isAngled ? '>' : '"'));
1954       }
1955       // For user files and known standard headers, by default we issue a diagnostic.
1956       // For other system headers, we don't. They can be controlled separately.
1957       auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
1958           diag::pp_nonportable_path : diag::pp_nonportable_system_path;
1959       SourceRange Range(FilenameTok.getLocation(), CharEnd);
1960       Diag(FilenameTok, DiagId) << Path <<
1961         FixItHint::CreateReplacement(Range, Path);
1962     }
1963   }
1964
1965   // Ask HeaderInfo if we should enter this #include file.  If not, #including
1966   // this file will have no effect.
1967   bool SkipHeader = false;
1968   if (ShouldEnter &&
1969       !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1970                                          getLangOpts().Modules,
1971                                          SuggestedModule.getModule())) {
1972     ShouldEnter = false;
1973     SkipHeader = true;
1974     if (Callbacks)
1975       Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1976   }
1977
1978   // If we don't need to enter the file, stop now.
1979   if (!ShouldEnter) {
1980     // If this is a module import, make it visible if needed.
1981     if (auto *M = SuggestedModule.getModule()) {
1982       // When building a pch, -fmodule-name tells the compiler to textually
1983       // include headers in the specified module. But it is possible that
1984       // ShouldEnter is false because we are skipping the header. In that
1985       // case, We are not importing the specified module.
1986       if (SkipHeader && getLangOpts().CompilingPCH &&
1987           M->getTopLevelModuleName() == getLangOpts().CurrentModule)
1988         return;
1989
1990       makeModuleVisible(M, HashLoc);
1991
1992       if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1993           tok::pp___include_macros)
1994         EnterAnnotationToken(SourceRange(HashLoc, End),
1995                              tok::annot_module_include, M);
1996     }
1997     return;
1998   }
1999
2000   // Look up the file, create a File ID for it.
2001   SourceLocation IncludePos = End;
2002   // If the filename string was the result of macro expansions, set the include
2003   // position on the file where it will be included and after the expansions.
2004   if (IncludePos.isMacroID())
2005     IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
2006   FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2007   assert(FID.isValid() && "Expected valid file ID");
2008
2009   // If all is good, enter the new file!
2010   if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2011     return;
2012
2013   // Determine if we're switching to building a new submodule, and which one.
2014   if (auto *M = SuggestedModule.getModule()) {
2015     // When building a pch, -fmodule-name tells the compiler to textually
2016     // include headers in the specified module. We are not building the
2017     // specified module.
2018     if (getLangOpts().CompilingPCH &&
2019         M->getTopLevelModuleName() == getLangOpts().CurrentModule)
2020       return;
2021
2022     assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2023     CurLexerSubmodule = M;
2024
2025     // Let the macro handling code know that any future macros are within
2026     // the new submodule.
2027     EnterSubmodule(M, HashLoc, /*ForPragma*/false);
2028
2029     // Let the parser know that any future declarations are within the new
2030     // submodule.
2031     // FIXME: There's no point doing this if we're handling a #__include_macros
2032     // directive.
2033     EnterAnnotationToken(SourceRange(HashLoc, End), tok::annot_module_begin, M);
2034   }
2035 }
2036
2037 /// HandleIncludeNextDirective - Implements \#include_next.
2038 ///
2039 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2040                                               Token &IncludeNextTok) {
2041   Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2042
2043   // #include_next is like #include, except that we start searching after
2044   // the current found directory.  If we can't do this, issue a
2045   // diagnostic.
2046   const DirectoryLookup *Lookup = CurDirLookup;
2047   const FileEntry *LookupFromFile = nullptr;
2048   if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2049     // If the main file is a header, then it's either for PCH/AST generation,
2050     // or libclang opened it. Either way, handle it as a normal include below
2051     // and do not complain about include_next.
2052   } else if (isInPrimaryFile()) {
2053     Lookup = nullptr;
2054     Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2055   } else if (CurLexerSubmodule) {
2056     // Start looking up in the directory *after* the one in which the current
2057     // file would be found, if any.
2058     assert(CurPPLexer && "#include_next directive in macro?");
2059     LookupFromFile = CurPPLexer->getFileEntry();
2060     Lookup = nullptr;
2061   } else if (!Lookup) {
2062     Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2063   } else {
2064     // Start looking up in the next directory.
2065     ++Lookup;
2066   }
2067
2068   return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2069                                 LookupFromFile);
2070 }
2071
2072 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2073 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2074   // The Microsoft #import directive takes a type library and generates header
2075   // files from it, and includes those.  This is beyond the scope of what clang
2076   // does, so we ignore it and error out.  However, #import can optionally have
2077   // trailing attributes that span multiple lines.  We're going to eat those
2078   // so we can continue processing from there.
2079   Diag(Tok, diag::err_pp_import_directive_ms );
2080
2081   // Read tokens until we get to the end of the directive.  Note that the
2082   // directive can be split over multiple lines using the backslash character.
2083   DiscardUntilEndOfDirective();
2084 }
2085
2086 /// HandleImportDirective - Implements \#import.
2087 ///
2088 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2089                                          Token &ImportTok) {
2090   if (!LangOpts.ObjC1) {  // #import is standard for ObjC.
2091     if (LangOpts.MSVCCompat)
2092       return HandleMicrosoftImportDirective(ImportTok);
2093     Diag(ImportTok, diag::ext_pp_import_directive);
2094   }
2095   return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2096 }
2097
2098 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2099 /// pseudo directive in the predefines buffer.  This handles it by sucking all
2100 /// tokens through the preprocessor and discarding them (only keeping the side
2101 /// effects on the preprocessor).
2102 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2103                                                 Token &IncludeMacrosTok) {
2104   // This directive should only occur in the predefines buffer.  If not, emit an
2105   // error and reject it.
2106   SourceLocation Loc = IncludeMacrosTok.getLocation();
2107   if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2108     Diag(IncludeMacrosTok.getLocation(),
2109          diag::pp_include_macros_out_of_predefines);
2110     DiscardUntilEndOfDirective();
2111     return;
2112   }
2113
2114   // Treat this as a normal #include for checking purposes.  If this is
2115   // successful, it will push a new lexer onto the include stack.
2116   HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2117
2118   Token TmpTok;
2119   do {
2120     Lex(TmpTok);
2121     assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2122   } while (TmpTok.isNot(tok::hashhash));
2123 }
2124
2125 //===----------------------------------------------------------------------===//
2126 // Preprocessor Macro Directive Handling.
2127 //===----------------------------------------------------------------------===//
2128
2129 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
2130 /// definition has just been read.  Lex the rest of the arguments and the
2131 /// closing ), updating MI with what we learn.  Return true if an error occurs
2132 /// parsing the arg list.
2133 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
2134   SmallVector<IdentifierInfo*, 32> Arguments;
2135
2136   while (true) {
2137     LexUnexpandedToken(Tok);
2138     switch (Tok.getKind()) {
2139     case tok::r_paren:
2140       // Found the end of the argument list.
2141       if (Arguments.empty())  // #define FOO()
2142         return false;
2143       // Otherwise we have #define FOO(A,)
2144       Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2145       return true;
2146     case tok::ellipsis:  // #define X(... -> C99 varargs
2147       if (!LangOpts.C99)
2148         Diag(Tok, LangOpts.CPlusPlus11 ?
2149              diag::warn_cxx98_compat_variadic_macro :
2150              diag::ext_variadic_macro);
2151
2152       // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2153       if (LangOpts.OpenCL) {
2154         Diag(Tok, diag::err_pp_opencl_variadic_macros);
2155         return true;
2156       }
2157
2158       // Lex the token after the identifier.
2159       LexUnexpandedToken(Tok);
2160       if (Tok.isNot(tok::r_paren)) {
2161         Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2162         return true;
2163       }
2164       // Add the __VA_ARGS__ identifier as an argument.
2165       Arguments.push_back(Ident__VA_ARGS__);
2166       MI->setIsC99Varargs();
2167       MI->setArgumentList(Arguments, BP);
2168       return false;
2169     case tok::eod:  // #define X(
2170       Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2171       return true;
2172     default:
2173       // Handle keywords and identifiers here to accept things like
2174       // #define Foo(for) for.
2175       IdentifierInfo *II = Tok.getIdentifierInfo();
2176       if (!II) {
2177         // #define X(1
2178         Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2179         return true;
2180       }
2181
2182       // If this is already used as an argument, it is used multiple times (e.g.
2183       // #define X(A,A.
2184       if (std::find(Arguments.begin(), Arguments.end(), II) !=
2185           Arguments.end()) {  // C99 6.10.3p6
2186         Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2187         return true;
2188       }
2189
2190       // Add the argument to the macro info.
2191       Arguments.push_back(II);
2192
2193       // Lex the token after the identifier.
2194       LexUnexpandedToken(Tok);
2195
2196       switch (Tok.getKind()) {
2197       default:          // #define X(A B
2198         Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2199         return true;
2200       case tok::r_paren: // #define X(A)
2201         MI->setArgumentList(Arguments, BP);
2202         return false;
2203       case tok::comma:  // #define X(A,
2204         break;
2205       case tok::ellipsis:  // #define X(A... -> GCC extension
2206         // Diagnose extension.
2207         Diag(Tok, diag::ext_named_variadic_macro);
2208
2209         // Lex the token after the identifier.
2210         LexUnexpandedToken(Tok);
2211         if (Tok.isNot(tok::r_paren)) {
2212           Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2213           return true;
2214         }
2215
2216         MI->setIsGNUVarargs();
2217         MI->setArgumentList(Arguments, BP);
2218         return false;
2219       }
2220     }
2221   }
2222 }
2223
2224 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2225                                    const LangOptions &LOptions) {
2226   if (MI->getNumTokens() == 1) {
2227     const Token &Value = MI->getReplacementToken(0);
2228
2229     // Macro that is identity, like '#define inline inline' is a valid pattern.
2230     if (MacroName.getKind() == Value.getKind())
2231       return true;
2232
2233     // Macro that maps a keyword to the same keyword decorated with leading/
2234     // trailing underscores is a valid pattern:
2235     //    #define inline __inline
2236     //    #define inline __inline__
2237     //    #define inline _inline (in MS compatibility mode)
2238     StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2239     if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2240       if (!II->isKeyword(LOptions))
2241         return false;
2242       StringRef ValueText = II->getName();
2243       StringRef TrimmedValue = ValueText;
2244       if (!ValueText.startswith("__")) {
2245         if (ValueText.startswith("_"))
2246           TrimmedValue = TrimmedValue.drop_front(1);
2247         else
2248           return false;
2249       } else {
2250         TrimmedValue = TrimmedValue.drop_front(2);
2251         if (TrimmedValue.endswith("__"))
2252           TrimmedValue = TrimmedValue.drop_back(2);
2253       }
2254       return TrimmedValue.equals(MacroText);
2255     } else {
2256       return false;
2257     }
2258   }
2259
2260   // #define inline
2261   return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2262                            tok::kw_const) &&
2263          MI->getNumTokens() == 0;
2264 }
2265
2266 /// HandleDefineDirective - Implements \#define.  This consumes the entire macro
2267 /// line then lets the caller lex the next real token.
2268 void Preprocessor::HandleDefineDirective(Token &DefineTok,
2269                                          bool ImmediatelyAfterHeaderGuard) {
2270   ++NumDefined;
2271
2272   Token MacroNameTok;
2273   bool MacroShadowsKeyword;
2274   ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2275
2276   // Error reading macro name?  If so, diagnostic already issued.
2277   if (MacroNameTok.is(tok::eod))
2278     return;
2279
2280   Token LastTok = MacroNameTok;
2281
2282   // If we are supposed to keep comments in #defines, reenable comment saving
2283   // mode.
2284   if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2285
2286   // Create the new macro.
2287   MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
2288
2289   Token Tok;
2290   LexUnexpandedToken(Tok);
2291
2292   // If this is a function-like macro definition, parse the argument list,
2293   // marking each of the identifiers as being used as macro arguments.  Also,
2294   // check other constraints on the first token of the macro body.
2295   if (Tok.is(tok::eod)) {
2296     if (ImmediatelyAfterHeaderGuard) {
2297       // Save this macro information since it may part of a header guard.
2298       CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2299                                         MacroNameTok.getLocation());
2300     }
2301     // If there is no body to this macro, we have no special handling here.
2302   } else if (Tok.hasLeadingSpace()) {
2303     // This is a normal token with leading space.  Clear the leading space
2304     // marker on the first token to get proper expansion.
2305     Tok.clearFlag(Token::LeadingSpace);
2306   } else if (Tok.is(tok::l_paren)) {
2307     // This is a function-like macro definition.  Read the argument list.
2308     MI->setIsFunctionLike();
2309     if (ReadMacroDefinitionArgList(MI, LastTok)) {
2310       // Throw away the rest of the line.
2311       if (CurPPLexer->ParsingPreprocessorDirective)
2312         DiscardUntilEndOfDirective();
2313       return;
2314     }
2315
2316     // If this is a definition of a variadic C99 function-like macro, not using
2317     // the GNU named varargs extension, enabled __VA_ARGS__.
2318
2319     // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2320     // This gets unpoisoned where it is allowed.
2321     assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2322     if (MI->isC99Varargs())
2323       Ident__VA_ARGS__->setIsPoisoned(false);
2324
2325     // Read the first token after the arg list for down below.
2326     LexUnexpandedToken(Tok);
2327   } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2328     // C99 requires whitespace between the macro definition and the body.  Emit
2329     // a diagnostic for something like "#define X+".
2330     Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2331   } else {
2332     // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2333     // first character of a replacement list is not a character required by
2334     // subclause 5.2.1, then there shall be white-space separation between the
2335     // identifier and the replacement list.".  5.2.1 lists this set:
2336     //   "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2337     // is irrelevant here.
2338     bool isInvalid = false;
2339     if (Tok.is(tok::at)) // @ is not in the list above.
2340       isInvalid = true;
2341     else if (Tok.is(tok::unknown)) {
2342       // If we have an unknown token, it is something strange like "`".  Since
2343       // all of valid characters would have lexed into a single character
2344       // token of some sort, we know this is not a valid case.
2345       isInvalid = true;
2346     }
2347     if (isInvalid)
2348       Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2349     else
2350       Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2351   }
2352
2353   if (!Tok.is(tok::eod))
2354     LastTok = Tok;
2355
2356   // Read the rest of the macro body.
2357   if (MI->isObjectLike()) {
2358     // Object-like macros are very simple, just read their body.
2359     while (Tok.isNot(tok::eod)) {
2360       LastTok = Tok;
2361       MI->AddTokenToBody(Tok);
2362       // Get the next token of the macro.
2363       LexUnexpandedToken(Tok);
2364     }
2365   } else {
2366     // Otherwise, read the body of a function-like macro.  While we are at it,
2367     // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2368     // parameters in function-like macro expansions.
2369     while (Tok.isNot(tok::eod)) {
2370       LastTok = Tok;
2371
2372       if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2373         MI->AddTokenToBody(Tok);
2374
2375         // Get the next token of the macro.
2376         LexUnexpandedToken(Tok);
2377         continue;
2378       }
2379
2380       // If we're in -traditional mode, then we should ignore stringification
2381       // and token pasting. Mark the tokens as unknown so as not to confuse
2382       // things.
2383       if (getLangOpts().TraditionalCPP) {
2384         Tok.setKind(tok::unknown);
2385         MI->AddTokenToBody(Tok);
2386
2387         // Get the next token of the macro.
2388         LexUnexpandedToken(Tok);
2389         continue;
2390       }
2391
2392       if (Tok.is(tok::hashhash)) {
2393         // If we see token pasting, check if it looks like the gcc comma
2394         // pasting extension.  We'll use this information to suppress
2395         // diagnostics later on.
2396
2397         // Get the next token of the macro.
2398         LexUnexpandedToken(Tok);
2399
2400         if (Tok.is(tok::eod)) {
2401           MI->AddTokenToBody(LastTok);
2402           break;
2403         }
2404
2405         unsigned NumTokens = MI->getNumTokens();
2406         if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2407             MI->getReplacementToken(NumTokens-1).is(tok::comma))
2408           MI->setHasCommaPasting();
2409
2410         // Things look ok, add the '##' token to the macro.
2411         MI->AddTokenToBody(LastTok);
2412         continue;
2413       }
2414
2415       // Get the next token of the macro.
2416       LexUnexpandedToken(Tok);
2417
2418       // Check for a valid macro arg identifier.
2419       if (Tok.getIdentifierInfo() == nullptr ||
2420           MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2421
2422         // If this is assembler-with-cpp mode, we accept random gibberish after
2423         // the '#' because '#' is often a comment character.  However, change
2424         // the kind of the token to tok::unknown so that the preprocessor isn't
2425         // confused.
2426         if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2427           LastTok.setKind(tok::unknown);
2428           MI->AddTokenToBody(LastTok);
2429           continue;
2430         } else {
2431           Diag(Tok, diag::err_pp_stringize_not_parameter)
2432             << LastTok.is(tok::hashat);
2433
2434           // Disable __VA_ARGS__ again.
2435           Ident__VA_ARGS__->setIsPoisoned(true);
2436           return;
2437         }
2438       }
2439
2440       // Things look ok, add the '#' and param name tokens to the macro.
2441       MI->AddTokenToBody(LastTok);
2442       MI->AddTokenToBody(Tok);
2443       LastTok = Tok;
2444
2445       // Get the next token of the macro.
2446       LexUnexpandedToken(Tok);
2447     }
2448   }
2449
2450   if (MacroShadowsKeyword &&
2451       !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2452     Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2453   }
2454
2455   // Disable __VA_ARGS__ again.
2456   Ident__VA_ARGS__->setIsPoisoned(true);
2457
2458   // Check that there is no paste (##) operator at the beginning or end of the
2459   // replacement list.
2460   unsigned NumTokens = MI->getNumTokens();
2461   if (NumTokens != 0) {
2462     if (MI->getReplacementToken(0).is(tok::hashhash)) {
2463       Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2464       return;
2465     }
2466     if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2467       Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2468       return;
2469     }
2470   }
2471
2472   MI->setDefinitionEndLoc(LastTok.getLocation());
2473
2474   // Finally, if this identifier already had a macro defined for it, verify that
2475   // the macro bodies are identical, and issue diagnostics if they are not.
2476   if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2477     // In Objective-C, ignore attempts to directly redefine the builtin
2478     // definitions of the ownership qualifiers.  It's still possible to
2479     // #undef them.
2480     auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2481       return II->isStr("__strong") ||
2482              II->isStr("__weak") ||
2483              II->isStr("__unsafe_unretained") ||
2484              II->isStr("__autoreleasing");
2485     };
2486    if (getLangOpts().ObjC1 &&
2487         SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2488           == getPredefinesFileID() &&
2489         isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2490       // Warn if it changes the tokens.
2491       if ((!getDiagnostics().getSuppressSystemWarnings() ||
2492            !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2493           !MI->isIdenticalTo(*OtherMI, *this,
2494                              /*Syntactic=*/LangOpts.MicrosoftExt)) {
2495         Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2496       }
2497       assert(!OtherMI->isWarnIfUnused());
2498       return;
2499     }
2500
2501     // It is very common for system headers to have tons of macro redefinitions
2502     // and for warnings to be disabled in system headers.  If this is the case,
2503     // then don't bother calling MacroInfo::isIdenticalTo.
2504     if (!getDiagnostics().getSuppressSystemWarnings() ||
2505         !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2506       if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2507         Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2508
2509       // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2510       // C++ [cpp.predefined]p4, but allow it as an extension.
2511       if (OtherMI->isBuiltinMacro())
2512         Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2513       // Macros must be identical.  This means all tokens and whitespace
2514       // separation must be the same.  C99 6.10.3p2.
2515       else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2516                !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2517         Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2518           << MacroNameTok.getIdentifierInfo();
2519         Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2520       }
2521     }
2522     if (OtherMI->isWarnIfUnused())
2523       WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2524   }
2525
2526   DefMacroDirective *MD =
2527       appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2528
2529   assert(!MI->isUsed());
2530   // If we need warning for not using the macro, add its location in the
2531   // warn-because-unused-macro set. If it gets used it will be removed from set.
2532   if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2533       !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2534     MI->setIsWarnIfUnused(true);
2535     WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2536   }
2537
2538   // If the callbacks want to know, tell them about the macro definition.
2539   if (Callbacks)
2540     Callbacks->MacroDefined(MacroNameTok, MD);
2541 }
2542
2543 /// HandleUndefDirective - Implements \#undef.
2544 ///
2545 void Preprocessor::HandleUndefDirective() {
2546   ++NumUndefined;
2547
2548   Token MacroNameTok;
2549   ReadMacroName(MacroNameTok, MU_Undef);
2550
2551   // Error reading macro name?  If so, diagnostic already issued.
2552   if (MacroNameTok.is(tok::eod))
2553     return;
2554
2555   // Check to see if this is the last token on the #undef line.
2556   CheckEndOfDirective("undef");
2557
2558   // Okay, we have a valid identifier to undef.
2559   auto *II = MacroNameTok.getIdentifierInfo();
2560   auto MD = getMacroDefinition(II);
2561   UndefMacroDirective *Undef = nullptr;
2562   
2563   // If the macro is not defined, this is a noop undef.
2564   if (const MacroInfo *MI = MD.getMacroInfo()) {
2565     if (!MI->isUsed() && MI->isWarnIfUnused())
2566       Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2567
2568     if (MI->isWarnIfUnused())
2569       WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2570
2571     Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2572   }
2573
2574   // If the callbacks want to know, tell them about the macro #undef.
2575   // Note: no matter if the macro was defined or not.
2576   if (Callbacks)
2577     Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2578
2579   if (Undef)
2580     appendMacroDirective(II, Undef);
2581 }
2582
2583 //===----------------------------------------------------------------------===//
2584 // Preprocessor Conditional Directive Handling.
2585 //===----------------------------------------------------------------------===//
2586
2587 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive.  isIfndef
2588 /// is true when this is a \#ifndef directive.  ReadAnyTokensBeforeDirective is
2589 /// true if any tokens have been returned or pp-directives activated before this
2590 /// \#ifndef has been lexed.
2591 ///
2592 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2593                                         bool ReadAnyTokensBeforeDirective) {
2594   ++NumIf;
2595   Token DirectiveTok = Result;
2596
2597   Token MacroNameTok;
2598   ReadMacroName(MacroNameTok);
2599
2600   // Error reading macro name?  If so, diagnostic already issued.
2601   if (MacroNameTok.is(tok::eod)) {
2602     // Skip code until we get to #endif.  This helps with recovery by not
2603     // emitting an error when the #endif is reached.
2604     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2605                                  /*Foundnonskip*/false, /*FoundElse*/false);
2606     return;
2607   }
2608
2609   // Check to see if this is the last token on the #if[n]def line.
2610   CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2611
2612   IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2613   auto MD = getMacroDefinition(MII);
2614   MacroInfo *MI = MD.getMacroInfo();
2615
2616   if (CurPPLexer->getConditionalStackDepth() == 0) {
2617     // If the start of a top-level #ifdef and if the macro is not defined,
2618     // inform MIOpt that this might be the start of a proper include guard.
2619     // Otherwise it is some other form of unknown conditional which we can't
2620     // handle.
2621     if (!ReadAnyTokensBeforeDirective && !MI) {
2622       assert(isIfndef && "#ifdef shouldn't reach here");
2623       CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2624     } else
2625       CurPPLexer->MIOpt.EnterTopLevelConditional();
2626   }
2627
2628   // If there is a macro, process it.
2629   if (MI)  // Mark it used.
2630     markMacroAsUsed(MI);
2631
2632   if (Callbacks) {
2633     if (isIfndef)
2634       Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2635     else
2636       Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2637   }
2638
2639   // Should we include the stuff contained by this directive?
2640   if (!MI == isIfndef) {
2641     // Yes, remember that we are inside a conditional, then lex the next token.
2642     CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2643                                      /*wasskip*/false, /*foundnonskip*/true,
2644                                      /*foundelse*/false);
2645   } else {
2646     // No, skip the contents of this block.
2647     SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2648                                  /*Foundnonskip*/false,
2649                                  /*FoundElse*/false);
2650   }
2651 }
2652
2653 /// HandleIfDirective - Implements the \#if directive.
2654 ///
2655 void Preprocessor::HandleIfDirective(Token &IfToken,
2656                                      bool ReadAnyTokensBeforeDirective) {
2657   ++NumIf;
2658
2659   // Parse and evaluate the conditional expression.
2660   IdentifierInfo *IfNDefMacro = nullptr;
2661   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2662   const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2663   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2664
2665   // If this condition is equivalent to #ifndef X, and if this is the first
2666   // directive seen, handle it for the multiple-include optimization.
2667   if (CurPPLexer->getConditionalStackDepth() == 0) {
2668     if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2669       // FIXME: Pass in the location of the macro name, not the 'if' token.
2670       CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2671     else
2672       CurPPLexer->MIOpt.EnterTopLevelConditional();
2673   }
2674
2675   if (Callbacks)
2676     Callbacks->If(IfToken.getLocation(),
2677                   SourceRange(ConditionalBegin, ConditionalEnd),
2678                   (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2679
2680   // Should we include the stuff contained by this directive?
2681   if (ConditionalTrue) {
2682     // Yes, remember that we are inside a conditional, then lex the next token.
2683     CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2684                                    /*foundnonskip*/true, /*foundelse*/false);
2685   } else {
2686     // No, skip the contents of this block.
2687     SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2688                                  /*FoundElse*/false);
2689   }
2690 }
2691
2692 /// HandleEndifDirective - Implements the \#endif directive.
2693 ///
2694 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2695   ++NumEndif;
2696
2697   // Check that this is the whole directive.
2698   CheckEndOfDirective("endif");
2699
2700   PPConditionalInfo CondInfo;
2701   if (CurPPLexer->popConditionalLevel(CondInfo)) {
2702     // No conditionals on the stack: this is an #endif without an #if.
2703     Diag(EndifToken, diag::err_pp_endif_without_if);
2704     return;
2705   }
2706
2707   // If this the end of a top-level #endif, inform MIOpt.
2708   if (CurPPLexer->getConditionalStackDepth() == 0)
2709     CurPPLexer->MIOpt.ExitTopLevelConditional();
2710
2711   assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2712          "This code should only be reachable in the non-skipping case!");
2713
2714   if (Callbacks)
2715     Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2716 }
2717
2718 /// HandleElseDirective - Implements the \#else directive.
2719 ///
2720 void Preprocessor::HandleElseDirective(Token &Result) {
2721   ++NumElse;
2722
2723   // #else directive in a non-skipping conditional... start skipping.
2724   CheckEndOfDirective("else");
2725
2726   PPConditionalInfo CI;
2727   if (CurPPLexer->popConditionalLevel(CI)) {
2728     Diag(Result, diag::pp_err_else_without_if);
2729     return;
2730   }
2731
2732   // If this is a top-level #else, inform the MIOpt.
2733   if (CurPPLexer->getConditionalStackDepth() == 0)
2734     CurPPLexer->MIOpt.EnterTopLevelConditional();
2735
2736   // If this is a #else with a #else before it, report the error.
2737   if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2738
2739   if (Callbacks)
2740     Callbacks->Else(Result.getLocation(), CI.IfLoc);
2741
2742   // Finally, skip the rest of the contents of this block.
2743   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2744                                /*FoundElse*/true, Result.getLocation());
2745 }
2746
2747 /// HandleElifDirective - Implements the \#elif directive.
2748 ///
2749 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2750   ++NumElse;
2751
2752   // #elif directive in a non-skipping conditional... start skipping.
2753   // We don't care what the condition is, because we will always skip it (since
2754   // the block immediately before it was included).
2755   const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2756   DiscardUntilEndOfDirective();
2757   const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2758
2759   PPConditionalInfo CI;
2760   if (CurPPLexer->popConditionalLevel(CI)) {
2761     Diag(ElifToken, diag::pp_err_elif_without_if);
2762     return;
2763   }
2764
2765   // If this is a top-level #elif, inform the MIOpt.
2766   if (CurPPLexer->getConditionalStackDepth() == 0)
2767     CurPPLexer->MIOpt.EnterTopLevelConditional();
2768
2769   // If this is a #elif with a #else before it, report the error.
2770   if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2771
2772   if (Callbacks)
2773     Callbacks->Elif(ElifToken.getLocation(),
2774                     SourceRange(ConditionalBegin, ConditionalEnd),
2775                     PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2776
2777   // Finally, skip the rest of the contents of this block.
2778   SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2779                                /*FoundElse*/CI.FoundElse,
2780                                ElifToken.getLocation());
2781 }