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