annotate clang/lib/Lex/PPDirectives.cpp @ 176:de4ac79aef9d

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