2008-03-09 11:13:06 +08:00
|
|
|
//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
|
2008-03-09 09:54:53 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-06-22 13:46:07 +08:00
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Implements # directive processing for the Preprocessor.
|
|
|
|
///
|
2008-03-09 09:54:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/Module.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/CodeCompletionHandler.h"
|
2008-03-09 09:54:53 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2011-09-16 06:00:41 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Lex/ModuleMap.h"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2010-09-10 06:45:38 +08:00
|
|
|
#include "clang/Lex/Pragma.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Lex/PTHLexer.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-06-14 04:40:21 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/AlignOf.h"
|
2011-12-01 02:02:36 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-08-09 05:31:04 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <new>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2016-02-13 06:53:10 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility Methods for Preprocessor Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-17 23:55:45 +08:00
|
|
|
MacroInfo *Preprocessor::AllocateMacroInfo() {
|
2014-07-24 09:13:23 +08:00
|
|
|
MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
|
2010-10-20 06:15:20 +08:00
|
|
|
MIChain->Next = MIChainHead;
|
|
|
|
MIChainHead = MIChain;
|
2014-07-24 09:13:23 +08:00
|
|
|
return &MIChain->MI;
|
2010-08-17 23:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
|
|
|
|
MacroInfo *MI = AllocateMacroInfo();
|
2008-12-16 03:56:42 +08:00
|
|
|
new (MI) MacroInfo(L);
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
2013-03-23 05:12:51 +08:00
|
|
|
MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
|
|
|
|
unsigned SubModuleID) {
|
2014-03-02 21:02:01 +08:00
|
|
|
static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
|
|
|
|
"alignment for MacroInfo is less than the ID");
|
2013-04-30 13:05:35 +08:00
|
|
|
DeserializedMacroInfoChain *MIChain =
|
|
|
|
BP.Allocate<DeserializedMacroInfoChain>();
|
|
|
|
MIChain->Next = DeserialMIChainHead;
|
|
|
|
DeserialMIChainHead = MIChain;
|
|
|
|
|
|
|
|
MacroInfo *MI = &MIChain->MI;
|
2013-03-23 05:12:51 +08:00
|
|
|
new (MI) MacroInfo(L);
|
|
|
|
MI->FromASTFile = true;
|
|
|
|
MI->setOwningModuleID(SubModuleID);
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
2015-04-24 07:29:05 +08:00
|
|
|
DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
|
|
|
|
SourceLocation Loc) {
|
2015-04-24 04:40:50 +08:00
|
|
|
return new (BP) DefMacroDirective(MI, Loc);
|
2013-03-27 01:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UndefMacroDirective *
|
2015-04-24 07:29:05 +08:00
|
|
|
Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
|
2015-04-24 04:40:50 +08:00
|
|
|
return new (BP) UndefMacroDirective(UndefLoc);
|
2013-03-27 01:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityMacroDirective *
|
|
|
|
Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
|
|
|
|
bool isPublic) {
|
2014-07-25 12:40:03 +08:00
|
|
|
return new (BP) VisibilityMacroDirective(Loc, isPublic);
|
2010-08-17 23:55:45 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// \brief Read and discard all tokens remaining on the current line until
|
|
|
|
/// the tok::eod token is found.
|
2008-03-09 09:54:53 +08:00
|
|
|
void Preprocessor::DiscardUntilEndOfDirective() {
|
|
|
|
Token Tmp;
|
|
|
|
do {
|
|
|
|
LexUnexpandedToken(Tmp);
|
2011-02-22 21:49:06 +08:00
|
|
|
assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
|
2011-02-28 10:37:51 +08:00
|
|
|
} while (Tmp.isNot(tok::eod));
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
/// \brief Enumerates possible cases of #define/#undef a reserved identifier.
|
|
|
|
enum MacroDiag {
|
|
|
|
MD_NoWarn, //> Not a reserved identifier
|
|
|
|
MD_KeywordDef, //> Macro hides keyword, enabled by default
|
|
|
|
MD_ReservedMacro //> #define of #undef reserved id, disabled by default
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Checks if the specified identifier is reserved in the specified
|
|
|
|
/// language.
|
|
|
|
/// This function does not check if the identifier is a keyword.
|
|
|
|
static bool isReservedId(StringRef Text, const LangOptions &Lang) {
|
|
|
|
// C++ [macro.names], C11 7.1.3:
|
|
|
|
// All identifiers that begin with an underscore and either an uppercase
|
|
|
|
// letter or another underscore are always reserved for any use.
|
|
|
|
if (Text.size() >= 2 && Text[0] == '_' &&
|
|
|
|
(isUppercase(Text[1]) || Text[1] == '_'))
|
|
|
|
return true;
|
|
|
|
// C++ [global.names]
|
|
|
|
// Each name that contains a double underscore ... is reserved to the
|
|
|
|
// implementation for any use.
|
|
|
|
if (Lang.CPlusPlus) {
|
|
|
|
if (Text.find("__") != StringRef::npos)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
|
2014-12-11 20:18:08 +08:00
|
|
|
const LangOptions &Lang = PP.getLangOpts();
|
|
|
|
StringRef Text = II->getName();
|
2014-12-18 19:14:21 +08:00
|
|
|
if (isReservedId(Text, Lang))
|
|
|
|
return MD_ReservedMacro;
|
2014-12-11 20:18:08 +08:00
|
|
|
if (II->isKeyword(Lang))
|
2014-12-18 19:14:21 +08:00
|
|
|
return MD_KeywordDef;
|
|
|
|
if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
|
|
|
|
return MD_KeywordDef;
|
|
|
|
return MD_NoWarn;
|
2014-12-11 20:18:08 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
|
|
|
|
const LangOptions &Lang = PP.getLangOpts();
|
|
|
|
StringRef Text = II->getName();
|
|
|
|
// Do not warn on keyword undef. It is generally harmless and widely used.
|
|
|
|
if (isReservedId(Text, Lang))
|
|
|
|
return MD_ReservedMacro;
|
|
|
|
return MD_NoWarn;
|
|
|
|
}
|
|
|
|
|
2016-06-14 04:40:21 +08:00
|
|
|
// Return true if we want to issue a diagnostic by default if we
|
|
|
|
// encounter this name in a #include with the wrong case. For now,
|
|
|
|
// this includes the standard C and C++ headers, Posix headers,
|
|
|
|
// and Boost headers. Improper case for these #includes is a
|
|
|
|
// potential portability issue.
|
|
|
|
static bool warnByDefaultOnWrongCase(StringRef Include) {
|
|
|
|
// If the first component of the path is "boost", treat this like a standard header
|
|
|
|
// for the purposes of diagnostics.
|
|
|
|
if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// "condition_variable" is the longest standard header name at 18 characters.
|
|
|
|
// If the include file name is longer than that, it can't be a standard header.
|
2016-06-14 05:55:33 +08:00
|
|
|
static const size_t MaxStdHeaderNameLen = 18u;
|
2016-06-14 04:40:21 +08:00
|
|
|
if (Include.size() > MaxStdHeaderNameLen)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Lowercase and normalize the search string.
|
|
|
|
SmallString<32> LowerInclude{Include};
|
|
|
|
for (char &Ch : LowerInclude) {
|
|
|
|
// In the ASCII range?
|
2016-06-16 10:30:33 +08:00
|
|
|
if (static_cast<unsigned char>(Ch) > 0x7f)
|
2016-06-14 04:40:21 +08:00
|
|
|
return false; // Can't be a standard header
|
|
|
|
// ASCII lowercase:
|
|
|
|
if (Ch >= 'A' && Ch <= 'Z')
|
|
|
|
Ch += 'a' - 'A';
|
|
|
|
// Normalize path separators for comparison purposes.
|
|
|
|
else if (::llvm::sys::path::is_separator(Ch))
|
|
|
|
Ch = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// The standard C/C++ and Posix headers
|
|
|
|
return llvm::StringSwitch<bool>(LowerInclude)
|
|
|
|
// C library headers
|
|
|
|
.Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
|
|
|
|
.Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
|
|
|
|
.Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
|
|
|
|
.Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
|
|
|
|
.Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
|
|
|
|
.Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
|
|
|
|
|
|
|
|
// C++ headers for C library facilities
|
|
|
|
.Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
|
|
|
|
.Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
|
|
|
|
.Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
|
|
|
|
.Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
|
|
|
|
.Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
|
|
|
|
.Case("cwctype", true)
|
|
|
|
|
|
|
|
// C++ library headers
|
|
|
|
.Cases("algorithm", "fstream", "list", "regex", "thread", true)
|
|
|
|
.Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
|
|
|
|
.Cases("atomic", "future", "map", "set", "type_traits", true)
|
|
|
|
.Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
|
|
|
|
.Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
|
|
|
|
.Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
|
|
|
|
.Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
|
|
|
|
.Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
|
|
|
|
.Cases("deque", "istream", "queue", "string", "valarray", true)
|
|
|
|
.Cases("exception", "iterator", "random", "strstream", "vector", true)
|
|
|
|
.Cases("forward_list", "limits", "ratio", "system_error", true)
|
|
|
|
|
|
|
|
// POSIX headers (which aren't also C headers)
|
|
|
|
.Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
|
|
|
|
.Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
|
|
|
|
.Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
|
|
|
|
.Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
|
|
|
|
.Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
|
|
|
|
.Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
|
|
|
|
.Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
|
|
|
|
.Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
|
|
|
|
.Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
|
|
|
|
.Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
|
|
|
|
.Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
|
|
|
|
.Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
|
|
|
|
bool *ShadowFlag) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// Missing macro name?
|
2014-05-21 14:13:51 +08:00
|
|
|
if (MacroNameTok.is(tok::eod))
|
|
|
|
return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!II) {
|
2010-03-17 06:30:13 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
std::string Spelling = getSpelling(MacroNameTok, &Invalid);
|
|
|
|
if (Invalid)
|
2014-05-21 14:13:51 +08:00
|
|
|
return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
|
2014-05-31 11:38:08 +08:00
|
|
|
II = getIdentifierInfo(Spelling);
|
2012-03-01 06:54:43 +08:00
|
|
|
|
2014-05-31 11:38:08 +08:00
|
|
|
if (!II->isCPlusPlusOperatorKeyword())
|
|
|
|
return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
|
2012-03-01 06:54:43 +08:00
|
|
|
|
2014-06-01 00:32:22 +08:00
|
|
|
// C++ 2.5p2: Alternative tokens behave the same as its primary token
|
|
|
|
// except for their spellings.
|
|
|
|
Diag(MacroNameTok, getLangOpts().MicrosoftExt
|
|
|
|
? diag::ext_pp_operator_used_as_macro_name
|
|
|
|
: diag::err_pp_operator_used_as_macro_name)
|
|
|
|
<< II << MacroNameTok.getKind();
|
2014-05-21 14:13:51 +08:00
|
|
|
|
2014-05-31 11:38:17 +08:00
|
|
|
// Allow #defining |and| and friends for Microsoft compatibility or
|
|
|
|
// recovery when legacy C headers are included in C++.
|
2014-05-31 11:38:08 +08:00
|
|
|
MacroNameTok.setIdentifierInfo(II);
|
2014-05-21 14:13:51 +08:00
|
|
|
}
|
|
|
|
|
2014-10-25 01:31:32 +08:00
|
|
|
if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
|
2013-03-06 08:46:00 +08:00
|
|
|
// Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
|
2014-05-21 14:13:51 +08:00
|
|
|
return Diag(MacroNameTok, diag::err_defined_macro_name);
|
|
|
|
}
|
|
|
|
|
2015-04-30 07:20:19 +08:00
|
|
|
if (isDefineUndef == MU_Undef) {
|
|
|
|
auto *MI = getMacroInfo(II);
|
|
|
|
if (MI && MI->isBuiltinMacro()) {
|
|
|
|
// Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
|
|
|
|
// and C++ [cpp.predefined]p4], but allow it as an extension.
|
|
|
|
Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
// If defining/undefining reserved identifier or a keyword, we need to issue
|
|
|
|
// a warning.
|
2014-12-11 20:18:08 +08:00
|
|
|
SourceLocation MacroNameLoc = MacroNameTok.getLocation();
|
2014-12-18 19:14:21 +08:00
|
|
|
if (ShadowFlag)
|
|
|
|
*ShadowFlag = false;
|
2014-12-11 20:18:08 +08:00
|
|
|
if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
|
|
|
|
(strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
|
2014-12-18 19:14:21 +08:00
|
|
|
MacroDiag D = MD_NoWarn;
|
|
|
|
if (isDefineUndef == MU_Define) {
|
|
|
|
D = shouldWarnOnMacroDef(*this, II);
|
|
|
|
}
|
|
|
|
else if (isDefineUndef == MU_Undef)
|
|
|
|
D = shouldWarnOnMacroUndef(*this, II);
|
|
|
|
if (D == MD_KeywordDef) {
|
|
|
|
// We do not want to warn on some patterns widely used in configuration
|
|
|
|
// scripts. This requires analyzing next tokens, so do not issue warnings
|
|
|
|
// now, only inform caller.
|
|
|
|
if (ShadowFlag)
|
|
|
|
*ShadowFlag = true;
|
|
|
|
}
|
|
|
|
if (D == MD_ReservedMacro)
|
|
|
|
Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
|
2014-12-11 20:18:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-21 14:13:51 +08:00
|
|
|
// Okay, we got a good identifier.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Lex and validate a macro name, which occurs after a
|
|
|
|
/// \#define or \#undef.
|
|
|
|
///
|
2014-10-25 01:31:32 +08:00
|
|
|
/// This sets the token kind to eod and discards the rest of the macro line if
|
|
|
|
/// the macro name is invalid.
|
|
|
|
///
|
|
|
|
/// \param MacroNameTok Token that is expected to be a macro name.
|
2014-12-18 19:14:21 +08:00
|
|
|
/// \param isDefineUndef Context in which macro is used.
|
|
|
|
/// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
|
|
|
|
void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
|
|
|
|
bool *ShadowFlag) {
|
2014-05-21 14:13:51 +08:00
|
|
|
// Read the token, don't allow macro expansion on it.
|
|
|
|
LexUnexpandedToken(MacroNameTok);
|
|
|
|
|
|
|
|
if (MacroNameTok.is(tok::code_completion)) {
|
|
|
|
if (CodeComplete)
|
2014-10-25 01:31:32 +08:00
|
|
|
CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
|
2014-05-21 14:13:51 +08:00
|
|
|
setCodeCompletionReached();
|
|
|
|
LexUnexpandedToken(MacroNameTok);
|
|
|
|
}
|
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
|
2014-05-21 14:13:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Invalid macro name, read and discard the rest of the line and set the
|
|
|
|
// token kind to tok::eod if necessary.
|
|
|
|
if (MacroNameTok.isNot(tok::eod)) {
|
|
|
|
MacroNameTok.setKind(tok::eod);
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// \brief Ensure that the next token is a tok::eod token.
|
|
|
|
///
|
|
|
|
/// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
|
2009-04-18 07:30:53 +08:00
|
|
|
/// true, then we consider macros that expand to zero tokens as being ok.
|
|
|
|
void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
|
2008-03-09 09:54:53 +08:00
|
|
|
Token Tmp;
|
2009-04-18 07:30:53 +08:00
|
|
|
// Lex unexpanded tokens for most directives: macros might expand to zero
|
|
|
|
// tokens, causing us to miss diagnosing invalid lines. Some directives (like
|
|
|
|
// #line) allow empty macros.
|
|
|
|
if (EnableMacros)
|
|
|
|
Lex(Tmp);
|
|
|
|
else
|
|
|
|
LexUnexpandedToken(Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// There should be no tokens after the directive, but we allow them as an
|
|
|
|
// extension.
|
|
|
|
while (Tmp.is(tok::comment)) // Skip comments in -C mode.
|
|
|
|
LexUnexpandedToken(Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tmp.isNot(tok::eod)) {
|
2009-04-14 13:15:20 +08:00
|
|
|
// Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
|
2011-02-22 21:49:00 +08:00
|
|
|
// or if this is a macro-style preprocessing directive, because it is more
|
|
|
|
// trouble than it is worth to insert /**/ and check that there is no /**/
|
|
|
|
// in the range also.
|
2010-04-01 01:46:05 +08:00
|
|
|
FixItHint Hint;
|
2012-03-11 15:00:24 +08:00
|
|
|
if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
|
2011-02-22 21:49:00 +08:00
|
|
|
!CurTokenLexer)
|
2010-04-01 01:46:05 +08:00
|
|
|
Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
|
|
|
|
Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
|
2008-03-09 09:54:53 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
|
|
|
|
/// decided that the subsequent tokens are in the \#if'd out portion of the
|
|
|
|
/// file. Lex the rest of the file, until we see an \#endif. If
|
2008-03-09 09:54:53 +08:00
|
|
|
/// FoundNonSkipPortion is true, then we have already emitted code for part of
|
2012-06-22 13:46:07 +08:00
|
|
|
/// this \#if directive, so \#else/\#elif blocks should never be entered.
|
|
|
|
/// If ElseOk is true, then \#else directives are ok, if not, then we have
|
|
|
|
/// already seen one so a \#else directive is a duplicate. When this returns,
|
|
|
|
/// the caller can lex the first valid token.
|
2008-03-09 09:54:53 +08:00
|
|
|
void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
|
|
|
|
bool FoundNonSkipPortion,
|
2011-09-28 01:32:05 +08:00
|
|
|
bool FoundElse,
|
|
|
|
SourceLocation ElseLoc) {
|
2008-03-09 09:54:53 +08:00
|
|
|
++NumSkipped;
|
2013-05-15 15:37:26 +08:00
|
|
|
assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
|
2008-03-09 09:54:53 +08:00
|
|
|
FoundNonSkipPortion, FoundElse);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
if (CurPTHLexer) {
|
|
|
|
PTHSkipExcludedConditionalBlock();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Enter raw mode to disable identifier lookup (and thus macro expansion),
|
|
|
|
// disabling warnings, etc.
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->LexingRawMode = true;
|
2008-03-09 09:54:53 +08:00
|
|
|
Token Tok;
|
2016-09-08 05:53:17 +08:00
|
|
|
while (true) {
|
2010-01-19 06:33:01 +08:00
|
|
|
CurLexer->Lex(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 03:08:16 +08:00
|
|
|
if (Tok.is(tok::code_completion)) {
|
|
|
|
if (CodeComplete)
|
|
|
|
CodeComplete->CodeCompleteInConditionalExclusion();
|
2011-09-04 11:32:15 +08:00
|
|
|
setCodeCompletionReached();
|
2010-08-25 03:08:16 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is the end of the buffer, we have an error.
|
|
|
|
if (Tok.is(tok::eof)) {
|
|
|
|
// Emit errors for each unterminated conditional on the stack, including
|
|
|
|
// the current one.
|
2008-11-18 08:34:22 +08:00
|
|
|
while (!CurPPLexer->ConditionalStack.empty()) {
|
2011-09-04 11:32:15 +08:00
|
|
|
if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
|
2010-08-13 01:04:55 +08:00
|
|
|
Diag(CurPPLexer->ConditionalStack.back().IfLoc,
|
|
|
|
diag::err_pp_unterminated_conditional);
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ConditionalStack.pop_back();
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Just return and let the caller lex after this #include.
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this token is not a preprocessor directive, just skip it.
|
|
|
|
if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// We just parsed a # character at the start of a line, so we're in
|
|
|
|
// directive mode. Tell the lexer this so any newlines we see will be
|
2011-02-28 10:37:51 +08:00
|
|
|
// converted into an EOD token (this terminates the macro).
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ParsingPreprocessorDirective = true;
|
2013-02-22 08:32:00 +08:00
|
|
|
if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the next token, the directive flavor.
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
|
|
|
|
// something bogus), skip it.
|
2010-12-22 16:23:18 +08:00
|
|
|
if (Tok.isNot(tok::raw_identifier)) {
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ParsingPreprocessorDirective = false;
|
2008-03-09 09:54:53 +08:00
|
|
|
// Restore comment saving mode.
|
2013-02-22 02:53:19 +08:00
|
|
|
if (CurLexer) CurLexer->resetExtendedTokenMode();
|
2008-03-09 09:54:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the first letter isn't i or e, it isn't intesting to us. We know that
|
|
|
|
// this is safe in the face of spelling differences, because there is no way
|
|
|
|
// to spell an i/e in a strange way that is another letter. Skipping this
|
|
|
|
// allows us to avoid looking up the identifier info for #define/#undef and
|
|
|
|
// other common directives.
|
2014-05-17 12:53:25 +08:00
|
|
|
StringRef RI = Tok.getRawIdentifier();
|
2010-12-22 16:23:18 +08:00
|
|
|
|
2014-05-17 12:53:25 +08:00
|
|
|
char FirstChar = RI[0];
|
2009-09-09 23:08:12 +08:00
|
|
|
if (FirstChar >= 'a' && FirstChar <= 'z' &&
|
2008-03-09 09:54:53 +08:00
|
|
|
FirstChar != 'i' && FirstChar != 'e') {
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ParsingPreprocessorDirective = false;
|
2008-03-09 09:54:53 +08:00
|
|
|
// Restore comment saving mode.
|
2013-02-22 02:53:19 +08:00
|
|
|
if (CurLexer) CurLexer->resetExtendedTokenMode();
|
2008-03-09 09:54:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Get the identifier name without trigraphs or embedded newlines. Note
|
|
|
|
// that we can't use Tok.getIdentifierInfo() because its lookup is disabled
|
|
|
|
// when skipping.
|
2009-12-31 21:32:38 +08:00
|
|
|
char DirectiveBuf[20];
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Directive;
|
2014-05-17 12:53:25 +08:00
|
|
|
if (!Tok.needsCleaning() && RI.size() < 20) {
|
|
|
|
Directive = RI;
|
2008-03-09 09:54:53 +08:00
|
|
|
} else {
|
|
|
|
std::string DirectiveStr = getSpelling(Tok);
|
2009-12-31 21:32:38 +08:00
|
|
|
unsigned IdLen = DirectiveStr.size();
|
2008-03-09 09:54:53 +08:00
|
|
|
if (IdLen >= 20) {
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ParsingPreprocessorDirective = false;
|
2008-03-09 09:54:53 +08:00
|
|
|
// Restore comment saving mode.
|
2013-02-22 02:53:19 +08:00
|
|
|
if (CurLexer) CurLexer->resetExtendedTokenMode();
|
2008-03-09 09:54:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-31 21:32:38 +08:00
|
|
|
memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
|
2011-07-23 18:55:15 +08:00
|
|
|
Directive = StringRef(DirectiveBuf, IdLen);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-31 21:32:38 +08:00
|
|
|
if (Directive.startswith("if")) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Sub = Directive.substr(2);
|
2009-12-31 21:32:38 +08:00
|
|
|
if (Sub.empty() || // "if"
|
|
|
|
Sub == "def" || // "ifdef"
|
|
|
|
Sub == "ndef") { // "ifndef"
|
2008-03-09 09:54:53 +08:00
|
|
|
// We know the entire #if/#ifdef/#ifndef block will be skipped, don't
|
|
|
|
// bother parsing the condition.
|
|
|
|
DiscardUntilEndOfDirective();
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
|
2008-03-09 09:54:53 +08:00
|
|
|
/*foundnonskip*/false,
|
2011-01-04 01:40:17 +08:00
|
|
|
/*foundelse*/false);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-12-31 21:32:38 +08:00
|
|
|
} else if (Directive[0] == 'e') {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Sub = Directive.substr(1);
|
2009-12-31 21:32:38 +08:00
|
|
|
if (Sub == "ndif") { // "endif"
|
2008-03-09 09:54:53 +08:00
|
|
|
PPConditionalInfo CondInfo;
|
|
|
|
CondInfo.WasSkipping = true; // Silence bogus warning.
|
2008-11-18 08:34:22 +08:00
|
|
|
bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
|
2010-12-23 09:01:28 +08:00
|
|
|
(void)InCond; // Silence warning in no-asserts mode.
|
2008-03-09 09:54:53 +08:00
|
|
|
assert(!InCond && "Can't be skipping if not in a conditional!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If we popped the outermost skipping block, we're done skipping!
|
2012-03-05 13:48:09 +08:00
|
|
|
if (!CondInfo.WasSkipping) {
|
2012-06-25 07:56:26 +08:00
|
|
|
// Restore the value of LexingRawMode so that trailing comments
|
|
|
|
// are handled correctly, if we've reached the outermost block.
|
|
|
|
CurPPLexer->LexingRawMode = false;
|
2012-06-21 08:35:03 +08:00
|
|
|
CheckEndOfDirective("endif");
|
2012-06-25 07:56:26 +08:00
|
|
|
CurPPLexer->LexingRawMode = true;
|
2012-03-05 13:48:09 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
|
2008-03-09 09:54:53 +08:00
|
|
|
break;
|
2012-06-21 08:35:03 +08:00
|
|
|
} else {
|
|
|
|
DiscardUntilEndOfDirective();
|
2012-03-05 13:48:09 +08:00
|
|
|
}
|
2009-12-31 21:32:38 +08:00
|
|
|
} else if (Sub == "lse") { // "else".
|
2008-03-09 09:54:53 +08:00
|
|
|
// #else directive in a skipping conditional. If not in some other
|
|
|
|
// skipping conditional, and if #else hasn't already been seen, enter it
|
|
|
|
// as a non-skipping conditional.
|
2008-11-18 08:34:22 +08:00
|
|
|
PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is a #else with a #else before it, report the error.
|
|
|
|
if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Note that we've seen a #else in this conditional.
|
|
|
|
CondInfo.FoundElse = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If the conditional is at the top level, and the #if block wasn't
|
|
|
|
// entered, enter the #else block now.
|
|
|
|
if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
|
|
|
|
CondInfo.FoundNonSkip = true;
|
2012-06-25 07:56:26 +08:00
|
|
|
// Restore the value of LexingRawMode so that trailing comments
|
|
|
|
// are handled correctly.
|
|
|
|
CurPPLexer->LexingRawMode = false;
|
2011-05-21 12:26:04 +08:00
|
|
|
CheckEndOfDirective("else");
|
2012-06-25 07:56:26 +08:00
|
|
|
CurPPLexer->LexingRawMode = true;
|
2012-03-05 13:48:09 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
|
2008-03-09 09:54:53 +08:00
|
|
|
break;
|
2011-05-21 12:26:04 +08:00
|
|
|
} else {
|
|
|
|
DiscardUntilEndOfDirective(); // C99 6.10p4.
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-12-31 21:32:38 +08:00
|
|
|
} else if (Sub == "lif") { // "elif".
|
2008-11-18 08:34:22 +08:00
|
|
|
PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2013-12-05 04:19:30 +08:00
|
|
|
// If this is a #elif with a #else before it, report the error.
|
|
|
|
if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is in a skipping block or if we're already handled this #if
|
|
|
|
// block, don't bother parsing the condition.
|
|
|
|
if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
} else {
|
2013-12-05 04:19:30 +08:00
|
|
|
const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
|
2008-03-09 09:54:53 +08:00
|
|
|
// Restore the value of LexingRawMode so that identifiers are
|
|
|
|
// looked up, etc, inside the #elif expression.
|
2008-11-18 08:34:22 +08:00
|
|
|
assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
|
|
|
|
CurPPLexer->LexingRawMode = false;
|
2014-05-18 07:10:59 +08:00
|
|
|
IdentifierInfo *IfNDefMacro = nullptr;
|
2013-12-05 04:19:30 +08:00
|
|
|
const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->LexingRawMode = true;
|
2013-12-05 04:19:30 +08:00
|
|
|
if (Callbacks) {
|
|
|
|
const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
|
2012-03-05 13:48:09 +08:00
|
|
|
Callbacks->Elif(Tok.getLocation(),
|
2013-12-05 04:19:30 +08:00
|
|
|
SourceRange(CondBegin, CondEnd),
|
2013-12-07 16:41:15 +08:00
|
|
|
(CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
|
2013-12-05 04:19:30 +08:00
|
|
|
}
|
|
|
|
// If this condition is true, enter it!
|
|
|
|
if (CondValue) {
|
|
|
|
CondInfo.FoundNonSkip = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ParsingPreprocessorDirective = false;
|
2008-03-09 09:54:53 +08:00
|
|
|
// Restore comment saving mode.
|
2013-02-22 02:53:19 +08:00
|
|
|
if (CurLexer) CurLexer->resetExtendedTokenMode();
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, if we are out of the conditional (saw an #endif or ran off the end
|
|
|
|
// of the file, just stop skipping and return to lexing whatever came after
|
|
|
|
// the #if block.
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->LexingRawMode = false;
|
2011-09-28 01:32:05 +08:00
|
|
|
|
|
|
|
if (Callbacks) {
|
|
|
|
SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
|
|
|
|
Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
void Preprocessor::PTHSkipExcludedConditionalBlock() {
|
2016-09-08 05:53:17 +08:00
|
|
|
while (true) {
|
2008-12-13 02:34:08 +08:00
|
|
|
assert(CurPTHLexer);
|
|
|
|
assert(CurPTHLexer->LexingRawMode == false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// Skip to the next '#else', '#elif', or #endif.
|
|
|
|
if (CurPTHLexer->SkipBlock()) {
|
|
|
|
// We have reached an #endif. Both the '#' and 'endif' tokens
|
|
|
|
// have been consumed by the PTHLexer. Just pop off the condition level.
|
|
|
|
PPConditionalInfo CondInfo;
|
|
|
|
bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
|
2010-12-23 09:01:28 +08:00
|
|
|
(void)InCond; // Silence warning in no-asserts mode.
|
2008-12-13 02:34:08 +08:00
|
|
|
assert(!InCond && "Can't be skipping if not in a conditional!");
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// We have reached a '#else' or '#elif'. Lex the next token to get
|
|
|
|
// the directive flavor.
|
|
|
|
Token Tok;
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// We can actually look up the IdentifierInfo here since we aren't in
|
|
|
|
// raw mode.
|
|
|
|
tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
|
|
|
|
|
|
|
|
if (K == tok::pp_else) {
|
|
|
|
// #else: Enter the else condition. We aren't in a nested condition
|
|
|
|
// since we skip those. We're always in the one matching the last
|
|
|
|
// blocked we skipped.
|
|
|
|
PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
|
|
|
|
// Note that we've seen a #else in this conditional.
|
|
|
|
CondInfo.FoundElse = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// If the #if block wasn't entered then enter the #else block now.
|
|
|
|
if (!CondInfo.FoundNonSkip) {
|
|
|
|
CondInfo.FoundNonSkip = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// Scan until the eod token.
|
2008-12-23 09:30:52 +08:00
|
|
|
CurPTHLexer->ParsingPreprocessorDirective = true;
|
2009-04-14 01:57:49 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2008-12-23 09:30:52 +08:00
|
|
|
CurPTHLexer->ParsingPreprocessorDirective = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// Otherwise skip this block.
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
assert(K == tok::pp_elif);
|
|
|
|
PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
|
|
|
|
|
|
|
|
// If this is a #elif with a #else before it, report the error.
|
|
|
|
if (CondInfo.FoundElse)
|
|
|
|
Diag(Tok, diag::pp_err_elif_after_else);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-13 02:34:08 +08:00
|
|
|
// If this is in a skipping block or if we're already handled this #if
|
2009-09-09 23:08:12 +08:00
|
|
|
// block, don't bother parsing the condition. We just skip this block.
|
2008-12-13 02:34:08 +08:00
|
|
|
if (CondInfo.FoundNonSkip)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Evaluate the condition of the #elif.
|
2014-05-18 07:10:59 +08:00
|
|
|
IdentifierInfo *IfNDefMacro = nullptr;
|
2008-12-13 02:34:08 +08:00
|
|
|
CurPTHLexer->ParsingPreprocessorDirective = true;
|
|
|
|
bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
|
|
|
|
CurPTHLexer->ParsingPreprocessorDirective = false;
|
|
|
|
|
|
|
|
// If this condition is true, enter it!
|
|
|
|
if (ShouldEnter) {
|
|
|
|
CondInfo.FoundNonSkip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, skip this block and go to the next one.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 06:58:06 +08:00
|
|
|
Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
|
2016-02-20 06:25:36 +08:00
|
|
|
if (!SourceMgr.isInMainFile(Loc)) {
|
|
|
|
// Try to determine the module of the include directive.
|
|
|
|
// FIXME: Look into directly passing the FileEntry from LookupFile instead.
|
|
|
|
FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
|
|
|
|
if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
|
|
|
|
// The include comes from an included file.
|
|
|
|
return HeaderInfo.getModuleMap()
|
|
|
|
.findModuleForHeader(EntryOfIncl)
|
|
|
|
.getModule();
|
|
|
|
}
|
2013-09-24 17:14:14 +08:00
|
|
|
}
|
2016-02-20 06:25:36 +08:00
|
|
|
|
|
|
|
// This is either in the main file or not in a file at all. It belongs
|
|
|
|
// to the current module, if there is one.
|
|
|
|
return getLangOpts().CurrentModule.empty()
|
|
|
|
? nullptr
|
|
|
|
: HeaderInfo.lookupModule(getLangOpts().CurrentModule);
|
2013-09-24 17:14:14 +08:00
|
|
|
}
|
|
|
|
|
2015-04-24 06:58:06 +08:00
|
|
|
Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) {
|
|
|
|
return HeaderInfo.getModuleMap().inferModuleFromLocation(
|
|
|
|
FullSourceLoc(Loc, SourceMgr));
|
|
|
|
}
|
|
|
|
|
2016-04-28 05:57:05 +08:00
|
|
|
const FileEntry *
|
|
|
|
Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
// If we have a module import syntax, we shouldn't include a header to
|
|
|
|
// make a particular module visible.
|
|
|
|
if (getLangOpts().ObjC2)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Figure out which module we'd want to import.
|
|
|
|
Module *M = getModuleContainingLocation(Loc);
|
|
|
|
if (!M)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Module *TopM = M->getTopLevelModule();
|
|
|
|
Module *IncM = getModuleForLocation(IncLoc);
|
|
|
|
|
|
|
|
// Walk up through the include stack, looking through textual headers of M
|
|
|
|
// until we hit a non-textual header that we can #include. (We assume textual
|
|
|
|
// headers of a module with non-textual headers aren't meant to be used to
|
|
|
|
// import entities from the module.)
|
|
|
|
auto &SM = getSourceManager();
|
|
|
|
while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
|
|
|
|
auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
|
|
|
|
auto *FE = SM.getFileEntryForID(ID);
|
|
|
|
|
|
|
|
bool InTextualHeader = false;
|
|
|
|
for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
|
|
|
|
if (!Header.getModule()->isSubModuleOf(TopM))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(Header.getRole() & ModuleMap::TextualHeader)) {
|
|
|
|
// If this is an accessible, non-textual header of M's top-level module
|
|
|
|
// that transitively includes the given location and makes the
|
|
|
|
// corresponding module visible, this is the thing to #include.
|
|
|
|
if (Header.isAccessibleFrom(IncM))
|
|
|
|
return FE;
|
|
|
|
|
|
|
|
// It's in a private header; we can't #include it.
|
|
|
|
// FIXME: If there's a public header in some module that re-exports it,
|
|
|
|
// then we could suggest including that, but it's not clear that's the
|
|
|
|
// expected way to make this entity visible.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
InTextualHeader = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InTextualHeader)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Loc = SM.getIncludeLoc(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2011-03-17 02:34:36 +08:00
|
|
|
const FileEntry *Preprocessor::LookupFile(
|
2013-06-21 05:14:14 +08:00
|
|
|
SourceLocation FilenameLoc,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename,
|
2011-03-17 02:34:36 +08:00
|
|
|
bool isAngled,
|
|
|
|
const DirectoryLookup *FromDir,
|
2014-10-20 08:15:49 +08:00
|
|
|
const FileEntry *FromFile,
|
2011-03-17 02:34:36 +08:00
|
|
|
const DirectoryLookup *&CurDir,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<char> *SearchPath,
|
2011-09-16 06:00:41 +08:00
|
|
|
SmallVectorImpl<char> *RelativePath,
|
2013-06-21 05:14:14 +08:00
|
|
|
ModuleMap::KnownHeader *SuggestedModule,
|
2011-11-21 01:46:46 +08:00
|
|
|
bool SkipCache) {
|
2016-06-14 05:55:33 +08:00
|
|
|
Module *RequestingModule = getModuleForLocation(FilenameLoc);
|
2016-03-15 01:52:37 +08:00
|
|
|
bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
|
2015-10-17 05:42:56 +08:00
|
|
|
|
2013-12-28 03:46:16 +08:00
|
|
|
// If the header lookup mechanism may be relative to the current inclusion
|
|
|
|
// stack, record the parent #includes.
|
2014-08-12 16:25:57 +08:00
|
|
|
SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
|
|
|
|
Includers;
|
2016-05-17 10:15:12 +08:00
|
|
|
bool BuildSystemModule = false;
|
2014-10-20 08:15:49 +08:00
|
|
|
if (!FromDir && !FromFile) {
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID = getCurrentFileLexer()->getFileID();
|
2013-12-28 03:46:16 +08:00
|
|
|
const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-05 03:45:07 +08:00
|
|
|
// If there is no file entry associated with this file, it must be the
|
2014-12-02 08:08:08 +08:00
|
|
|
// predefines buffer or the module includes buffer. Any other file is not
|
|
|
|
// lexed with a normal lexer, so it won't be scanned for preprocessor
|
|
|
|
// directives.
|
|
|
|
//
|
|
|
|
// If we have the predefines buffer, resolve #include references (which come
|
|
|
|
// from the -include command line argument) from the current working
|
|
|
|
// directory instead of relative to the main file.
|
|
|
|
//
|
|
|
|
// If we have the module includes buffer, resolve #include references (which
|
|
|
|
// come from header declarations in the module map) relative to the module
|
|
|
|
// map file.
|
2014-08-12 16:25:57 +08:00
|
|
|
if (!FileEnt) {
|
2016-05-17 10:15:12 +08:00
|
|
|
if (FID == SourceMgr.getMainFileID() && MainFileDir) {
|
2014-12-02 08:08:08 +08:00
|
|
|
Includers.push_back(std::make_pair(nullptr, MainFileDir));
|
2016-05-17 10:15:12 +08:00
|
|
|
BuildSystemModule = getCurrentModule()->IsSystem;
|
|
|
|
} else if ((FileEnt =
|
2014-12-02 08:08:08 +08:00
|
|
|
SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
|
2014-08-12 16:25:57 +08:00
|
|
|
Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
|
|
|
|
} else {
|
|
|
|
Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
|
|
|
|
}
|
2013-12-28 03:46:16 +08:00
|
|
|
|
|
|
|
// MSVC searches the current include stack from top to bottom for
|
|
|
|
// headers included by quoted include directives.
|
|
|
|
// See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
|
2014-01-14 20:51:41 +08:00
|
|
|
if (LangOpts.MSVCCompat && !isAngled) {
|
2013-12-28 03:46:16 +08:00
|
|
|
for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
|
|
|
|
IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
|
|
|
|
if (IsFileLexer(ISEntry))
|
2015-12-18 18:30:12 +08:00
|
|
|
if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
|
2014-08-12 16:25:57 +08:00
|
|
|
Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
|
2013-12-28 03:46:16 +08:00
|
|
|
}
|
2009-02-05 03:45:07 +08:00
|
|
|
}
|
2008-03-09 12:17:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 12:17:44 +08:00
|
|
|
CurDir = CurDirLookup;
|
2014-10-20 08:15:49 +08:00
|
|
|
|
|
|
|
if (FromFile) {
|
|
|
|
// We're supposed to start looking from after a particular file. Search
|
|
|
|
// the include path until we find that file or run out of files.
|
|
|
|
const DirectoryLookup *TmpCurDir = CurDir;
|
|
|
|
const DirectoryLookup *TmpFromDir = nullptr;
|
|
|
|
while (const FileEntry *FE = HeaderInfo.LookupFile(
|
|
|
|
Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
|
2015-10-17 05:42:56 +08:00
|
|
|
Includers, SearchPath, RelativePath, RequestingModule,
|
|
|
|
SuggestedModule, SkipCache)) {
|
2014-10-20 08:15:49 +08:00
|
|
|
// Keep looking as if this file did a #include_next.
|
|
|
|
TmpFromDir = TmpCurDir;
|
|
|
|
++TmpFromDir;
|
|
|
|
if (FE == FromFile) {
|
|
|
|
// Found it.
|
|
|
|
FromDir = TmpFromDir;
|
|
|
|
CurDir = TmpCurDir;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a standard file entry lookup.
|
2011-03-17 02:34:36 +08:00
|
|
|
const FileEntry *FE = HeaderInfo.LookupFile(
|
2013-12-28 03:46:16 +08:00
|
|
|
Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
|
2016-05-17 10:15:12 +08:00
|
|
|
RelativePath, RequestingModule, SuggestedModule, SkipCache,
|
|
|
|
BuildSystemModule);
|
2013-06-21 05:14:14 +08:00
|
|
|
if (FE) {
|
2014-03-14 22:53:17 +08:00
|
|
|
if (SuggestedModule && !LangOpts.AsmPreprocessor)
|
2013-12-20 20:09:36 +08:00
|
|
|
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
|
2016-03-15 01:52:37 +08:00
|
|
|
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
|
|
|
|
Filename, FE);
|
2013-06-21 05:14:14 +08:00
|
|
|
return FE;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-12-28 03:46:16 +08:00
|
|
|
const FileEntry *CurFileEnt;
|
2008-03-09 12:17:44 +08:00
|
|
|
// Otherwise, see if this is a subframework header. If so, this is relative
|
|
|
|
// to one of the headers on the #include stack. Walk the list of the current
|
|
|
|
// headers on the #include stack and pass them to HeaderInfo.
|
2008-11-21 00:19:53 +08:00
|
|
|
if (IsFileLexer()) {
|
2015-12-18 18:30:12 +08:00
|
|
|
if ((CurFileEnt = CurPPLexer->getFileEntry())) {
|
2011-03-17 02:34:36 +08:00
|
|
|
if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
|
2013-02-08 08:10:48 +08:00
|
|
|
SearchPath, RelativePath,
|
2015-10-17 05:42:56 +08:00
|
|
|
RequestingModule,
|
2014-05-06 05:44:13 +08:00
|
|
|
SuggestedModule))) {
|
|
|
|
if (SuggestedModule && !LangOpts.AsmPreprocessor)
|
|
|
|
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
|
2016-03-15 01:52:37 +08:00
|
|
|
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
|
|
|
|
Filename, FE);
|
2008-03-09 12:17:44 +08:00
|
|
|
return FE;
|
2014-05-06 05:44:13 +08:00
|
|
|
}
|
|
|
|
}
|
2008-03-09 12:17:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 12:17:44 +08:00
|
|
|
for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
|
|
|
|
IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
|
2008-11-21 00:19:53 +08:00
|
|
|
if (IsFileLexer(ISEntry)) {
|
2015-12-18 18:30:12 +08:00
|
|
|
if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
|
2011-04-27 05:50:03 +08:00
|
|
|
if ((FE = HeaderInfo.LookupSubframeworkHeader(
|
2013-02-08 08:10:48 +08:00
|
|
|
Filename, CurFileEnt, SearchPath, RelativePath,
|
2015-10-17 05:42:56 +08:00
|
|
|
RequestingModule, SuggestedModule))) {
|
2014-05-06 05:44:13 +08:00
|
|
|
if (SuggestedModule && !LangOpts.AsmPreprocessor)
|
|
|
|
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
|
2016-03-15 01:52:37 +08:00
|
|
|
RequestingModule, RequestingModuleIsModuleInterface,
|
|
|
|
FilenameLoc, Filename, FE);
|
2008-03-09 12:17:44 +08:00
|
|
|
return FE;
|
2014-05-06 05:44:13 +08:00
|
|
|
}
|
|
|
|
}
|
2008-03-09 12:17:44 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 12:17:44 +08:00
|
|
|
// Otherwise, we really couldn't find the file.
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2008-03-09 12:17:44 +08:00
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-07 02:52:13 +08:00
|
|
|
class Preprocessor::ResetMacroExpansionHelper {
|
|
|
|
public:
|
|
|
|
ResetMacroExpansionHelper(Preprocessor *pp)
|
|
|
|
: PP(pp), save(pp->DisableMacroExpansion) {
|
|
|
|
if (pp->MacroExpansionInDirectivesOverride)
|
|
|
|
pp->DisableMacroExpansion = false;
|
|
|
|
}
|
2016-02-13 06:53:10 +08:00
|
|
|
|
2012-06-07 02:52:13 +08:00
|
|
|
~ResetMacroExpansionHelper() {
|
|
|
|
PP->DisableMacroExpansion = save;
|
|
|
|
}
|
2016-02-13 06:53:10 +08:00
|
|
|
|
2012-06-07 02:52:13 +08:00
|
|
|
private:
|
|
|
|
Preprocessor *PP;
|
|
|
|
bool save;
|
|
|
|
};
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
/// HandleDirective - This callback is invoked when the lexer sees a # token
|
2009-09-09 23:08:12 +08:00
|
|
|
/// at the start of a line. This consumes the directive, modifies the
|
2008-03-09 09:54:53 +08:00
|
|
|
/// lexer/preprocessor state, and advances the lexer(s) so that the next token
|
|
|
|
/// read is the correct one.
|
|
|
|
void Preprocessor::HandleDirective(Token &Result) {
|
|
|
|
// FIXME: Traditional: # with whitespace before it not recognized by K&R?
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// We just parsed a # character at the start of a line, so we're in directive
|
|
|
|
// mode. Tell the lexer this so any newlines we see will be converted into an
|
2011-02-28 10:37:51 +08:00
|
|
|
// EOD token (which terminates the directive).
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->ParsingPreprocessorDirective = true;
|
2013-02-22 02:53:19 +08:00
|
|
|
if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-13 05:20:57 +08:00
|
|
|
bool ImmediatelyAfterTopLevelIfndef =
|
|
|
|
CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
|
|
|
|
CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
++NumDirectives;
|
2010-03-13 18:17:05 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// We are about to read a token. For the multiple-include optimization FA to
|
2009-09-09 23:08:12 +08:00
|
|
|
// work, we have to remember if we had read any tokens *before* this
|
2008-03-09 09:54:53 +08:00
|
|
|
// pp-directive.
|
2009-12-14 12:54:40 +08:00
|
|
|
bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 05:00:25 +08:00
|
|
|
// Save the '#' token in case we need to return it later.
|
|
|
|
Token SavedHash = Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the next token, the directive flavor. This isn't expanded due to
|
|
|
|
// C99 6.10.3p8.
|
|
|
|
LexUnexpandedToken(Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
|
|
|
|
// #define A(x) #x
|
|
|
|
// A(abc
|
|
|
|
// #warning blah
|
|
|
|
// def)
|
2011-12-17 06:50:01 +08:00
|
|
|
// If so, the user is relying on undefined behavior, emit a diagnostic. Do
|
|
|
|
// not support this for #include-like directives, since that can result in
|
|
|
|
// terrible diagnostics, and does not work in GCC.
|
|
|
|
if (InMacroArgs) {
|
|
|
|
if (IdentifierInfo *II = Result.getIdentifierInfo()) {
|
|
|
|
switch (II->getPPKeywordID()) {
|
|
|
|
case tok::pp_include:
|
|
|
|
case tok::pp_import:
|
|
|
|
case tok::pp_include_next:
|
|
|
|
case tok::pp___include_macros:
|
2014-12-28 15:42:49 +08:00
|
|
|
case tok::pp_pragma:
|
|
|
|
Diag(Result, diag::err_embedded_directive) << II->getName();
|
2011-12-17 06:50:01 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(Result, diag::ext_embedded_directive);
|
2011-12-17 06:50:01 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-07 02:52:13 +08:00
|
|
|
// Temporarily enable macro expansion if set so
|
|
|
|
// and reset to previous state when returning from this function.
|
|
|
|
ResetMacroExpansionHelper helper(this);
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
switch (Result.getKind()) {
|
2011-02-28 10:37:51 +08:00
|
|
|
case tok::eod:
|
2008-03-09 09:54:53 +08:00
|
|
|
return; // null directive.
|
2010-08-25 03:08:16 +08:00
|
|
|
case tok::code_completion:
|
|
|
|
if (CodeComplete)
|
|
|
|
CodeComplete->CodeCompleteDirective(
|
|
|
|
CurPPLexer->getConditionalStackDepth() > 0);
|
2011-09-04 11:32:15 +08:00
|
|
|
setCodeCompletionReached();
|
2010-08-25 03:08:16 +08:00
|
|
|
return;
|
2009-01-26 14:19:46 +08:00
|
|
|
case tok::numeric_constant: // # 7 GNU line marker directive.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().AsmPreprocessor)
|
2009-03-19 04:41:10 +08:00
|
|
|
break; // # 4 is not a preprocessor directive in .S files.
|
2009-01-26 14:19:46 +08:00
|
|
|
return HandleDigitDirective(Result);
|
2008-03-09 09:54:53 +08:00
|
|
|
default:
|
|
|
|
IdentifierInfo *II = Result.getIdentifierInfo();
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!II) break; // Not an identifier.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Ask what the preprocessor keyword ID is.
|
|
|
|
switch (II->getPPKeywordID()) {
|
|
|
|
default: break;
|
|
|
|
// C99 6.10.1 - Conditional Inclusion.
|
|
|
|
case tok::pp_if:
|
|
|
|
return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
|
|
|
|
case tok::pp_ifdef:
|
|
|
|
return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
|
|
|
|
case tok::pp_ifndef:
|
|
|
|
return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
|
|
|
|
case tok::pp_elif:
|
|
|
|
return HandleElifDirective(Result);
|
|
|
|
case tok::pp_else:
|
|
|
|
return HandleElseDirective(Result);
|
|
|
|
case tok::pp_endif:
|
|
|
|
return HandleEndifDirective(Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// C99 6.10.2 - Source File Inclusion.
|
|
|
|
case tok::pp_include:
|
2010-10-21 06:00:55 +08:00
|
|
|
// Handle #include.
|
|
|
|
return HandleIncludeDirective(SavedHash.getLocation(), Result);
|
2009-04-09 02:24:34 +08:00
|
|
|
case tok::pp___include_macros:
|
2010-10-21 06:00:55 +08:00
|
|
|
// Handle -imacros.
|
2016-06-14 05:55:33 +08:00
|
|
|
return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// C99 6.10.3 - Macro Replacement.
|
|
|
|
case tok::pp_define:
|
2013-06-13 05:20:57 +08:00
|
|
|
return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
|
2008-03-09 09:54:53 +08:00
|
|
|
case tok::pp_undef:
|
|
|
|
return HandleUndefDirective(Result);
|
|
|
|
|
|
|
|
// C99 6.10.4 - Line Control.
|
|
|
|
case tok::pp_line:
|
2009-01-26 13:29:08 +08:00
|
|
|
return HandleLineDirective(Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// C99 6.10.5 - Error Directive.
|
|
|
|
case tok::pp_error:
|
|
|
|
return HandleUserDiagnosticDirective(Result, false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// C99 6.10.6 - Pragma Directive.
|
|
|
|
case tok::pp_pragma:
|
2013-07-21 04:09:11 +08:00
|
|
|
return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// GNU Extensions.
|
|
|
|
case tok::pp_import:
|
2010-10-21 06:00:55 +08:00
|
|
|
return HandleImportDirective(SavedHash.getLocation(), Result);
|
2008-03-09 09:54:53 +08:00
|
|
|
case tok::pp_include_next:
|
2010-10-21 06:00:55 +08:00
|
|
|
return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
case tok::pp_warning:
|
|
|
|
Diag(Result, diag::ext_pp_warning_directive);
|
|
|
|
return HandleUserDiagnosticDirective(Result, true);
|
|
|
|
case tok::pp_ident:
|
|
|
|
return HandleIdentSCCSDirective(Result);
|
|
|
|
case tok::pp_sccs:
|
|
|
|
return HandleIdentSCCSDirective(Result);
|
|
|
|
case tok::pp_assert:
|
|
|
|
//isExtension = true; // FIXME: implement #assert
|
|
|
|
break;
|
|
|
|
case tok::pp_unassert:
|
|
|
|
//isExtension = true; // FIXME: implement #unassert
|
|
|
|
break;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2012-01-04 03:48:16 +08:00
|
|
|
case tok::pp___public_macro:
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().Modules)
|
2012-01-04 02:24:14 +08:00
|
|
|
return HandleMacroPublicDirective(Result);
|
|
|
|
break;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2012-01-04 03:48:16 +08:00
|
|
|
case tok::pp___private_macro:
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().Modules)
|
2012-01-04 02:24:14 +08:00
|
|
|
return HandleMacroPrivateDirective(Result);
|
|
|
|
break;
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 05:00:25 +08:00
|
|
|
// If this is a .S file, treat unknown # directives as non-preprocessor
|
|
|
|
// directives. This is important because # may be a comment or introduce
|
|
|
|
// various pseudo-ops. Just return the # token and push back the following
|
|
|
|
// token to be lexed next time.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().AsmPreprocessor) {
|
2016-02-10 02:52:09 +08:00
|
|
|
auto Toks = llvm::make_unique<Token[]>(2);
|
2009-03-19 05:00:25 +08:00
|
|
|
// Return the # and the token after it.
|
2009-09-09 23:08:12 +08:00
|
|
|
Toks[0] = SavedHash;
|
2009-03-19 05:00:25 +08:00
|
|
|
Toks[1] = Result;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-01-06 13:01:51 +08:00
|
|
|
// If the second token is a hashhash token, then we need to translate it to
|
|
|
|
// unknown so the token lexer doesn't try to perform token pasting.
|
|
|
|
if (Result.is(tok::hashhash))
|
|
|
|
Toks[1].setKind(tok::unknown);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2009-03-19 05:00:25 +08:00
|
|
|
// Enter this token stream so that we re-lex the tokens. Make sure to
|
|
|
|
// enable macro expansion, in case the token after the # is an identifier
|
|
|
|
// that is expanded.
|
2016-02-10 02:52:09 +08:00
|
|
|
EnterTokenStream(std::move(Toks), 2, false);
|
2009-03-19 05:00:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If we reached here, the preprocessing token is not valid!
|
|
|
|
Diag(Result, diag::err_pp_invalid_directive);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the rest of the PP line.
|
|
|
|
DiscardUntilEndOfDirective();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Okay, we're done parsing the directive.
|
|
|
|
}
|
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
/// GetLineValue - Convert a numeric token into an unsigned value, emitting
|
|
|
|
/// Diagnostic DiagID if it is invalid, and returning the value in Val.
|
|
|
|
static bool GetLineValue(Token &DigitTok, unsigned &Val,
|
2013-04-10 09:04:18 +08:00
|
|
|
unsigned DiagID, Preprocessor &PP,
|
|
|
|
bool IsGNULineDirective=false) {
|
2009-01-26 13:29:08 +08:00
|
|
|
if (DigitTok.isNot(tok::numeric_constant)) {
|
2009-01-26 14:19:46 +08:00
|
|
|
PP.Diag(DigitTok, DiagID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
if (DigitTok.isNot(tok::eod))
|
2009-01-26 14:19:46 +08:00
|
|
|
PP.DiscardUntilEndOfDirective();
|
|
|
|
return true;
|
2009-01-26 13:29:08 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> IntegerBuffer;
|
2009-01-26 13:29:08 +08:00
|
|
|
IntegerBuffer.resize(DigitTok.getLength());
|
|
|
|
const char *DigitTokBegin = &IntegerBuffer[0];
|
2010-03-17 06:30:13 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return true;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2009-04-19 02:35:15 +08:00
|
|
|
// Verify that we have a simple digit-sequence, and compute the value. This
|
|
|
|
// is always a simple digit string computed in decimal, so we do this manually
|
|
|
|
// here.
|
|
|
|
Val = 0;
|
|
|
|
for (unsigned i = 0; i != ActualLength; ++i) {
|
2013-09-27 02:13:20 +08:00
|
|
|
// C++1y [lex.fcon]p1:
|
|
|
|
// Optional separating single quotes in a digit-sequence are ignored
|
|
|
|
if (DigitTokBegin[i] == '\'')
|
|
|
|
continue;
|
|
|
|
|
2013-02-09 06:30:41 +08:00
|
|
|
if (!isDigit(DigitTokBegin[i])) {
|
2009-04-19 02:35:15 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
|
2013-04-10 09:04:18 +08:00
|
|
|
diag::err_pp_line_digit_sequence) << IsGNULineDirective;
|
2009-04-19 02:35:15 +08:00
|
|
|
PP.DiscardUntilEndOfDirective();
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 02:35:15 +08:00
|
|
|
unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
|
|
|
|
if (NextVal < Val) { // overflow.
|
|
|
|
PP.Diag(DigitTok, DiagID);
|
|
|
|
PP.DiscardUntilEndOfDirective();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Val = NextVal;
|
2009-01-26 13:29:08 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-27 05:19:20 +08:00
|
|
|
if (DigitTokBegin[0] == '0' && Val)
|
2013-04-10 09:04:18 +08:00
|
|
|
PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
|
|
|
|
<< IsGNULineDirective;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// \brief Handle a \#line directive: C99 6.10.4.
|
|
|
|
///
|
|
|
|
/// The two acceptable forms are:
|
|
|
|
/// \verbatim
|
2009-01-26 14:19:46 +08:00
|
|
|
/// # line digit-sequence
|
|
|
|
/// # line digit-sequence "s-char-sequence"
|
2012-06-22 13:46:07 +08:00
|
|
|
/// \endverbatim
|
2009-01-26 14:19:46 +08:00
|
|
|
void Preprocessor::HandleLineDirective(Token &Tok) {
|
|
|
|
// Read the line # and string argument. Per C99 6.10.4p5, these tokens are
|
|
|
|
// expanded.
|
|
|
|
Token DigitTok;
|
|
|
|
Lex(DigitTok);
|
|
|
|
|
|
|
|
// Validate the number and convert it to an unsigned.
|
|
|
|
unsigned LineNo;
|
2009-04-19 02:35:15 +08:00
|
|
|
if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
|
2009-01-26 14:19:46 +08:00
|
|
|
return;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2012-06-27 05:19:20 +08:00
|
|
|
if (LineNo == 0)
|
|
|
|
Diag(DigitTok, diag::ext_pp_line_zero);
|
2009-01-26 14:19:46 +08:00
|
|
|
|
|
|
|
// Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
|
|
|
|
// number greater than 2147483647". C90 requires that the line # be <= 32767.
|
2011-10-11 07:35:28 +08:00
|
|
|
unsigned LineLimit = 32768U;
|
2013-01-02 19:42:31 +08:00
|
|
|
if (LangOpts.C99 || LangOpts.CPlusPlus11)
|
2011-10-11 07:35:28 +08:00
|
|
|
LineLimit = 2147483648U;
|
2009-01-26 13:29:08 +08:00
|
|
|
if (LineNo >= LineLimit)
|
|
|
|
Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
|
2013-01-02 19:42:31 +08:00
|
|
|
else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
|
2011-10-15 09:18:56 +08:00
|
|
|
Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 15:57:50 +08:00
|
|
|
int FilenameID = -1;
|
2009-01-26 13:29:08 +08:00
|
|
|
Token StrTok;
|
|
|
|
Lex(StrTok);
|
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
|
|
|
|
// string followed by eod.
|
|
|
|
if (StrTok.is(tok::eod))
|
2009-01-26 13:29:08 +08:00
|
|
|
; // ok
|
|
|
|
else if (StrTok.isNot(tok::string_literal)) {
|
|
|
|
Diag(StrTok, diag::err_pp_line_invalid_filename);
|
2012-03-06 11:21:47 +08:00
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
} else if (StrTok.hasUDSuffix()) {
|
|
|
|
Diag(StrTok, diag::err_invalid_string_udl);
|
|
|
|
return DiscardUntilEndOfDirective();
|
2009-01-26 13:29:08 +08:00
|
|
|
} else {
|
2009-01-26 15:57:50 +08:00
|
|
|
// Parse and validate the string, converting it into a unique ID.
|
2014-06-26 12:58:39 +08:00
|
|
|
StringLiteralParser Literal(StrTok, *this);
|
2011-07-27 13:40:30 +08:00
|
|
|
assert(Literal.isAscii() && "Didn't allow wide strings in");
|
2009-01-26 15:57:50 +08:00
|
|
|
if (Literal.hadError)
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
if (Literal.Pascal) {
|
|
|
|
Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
}
|
2011-06-21 23:13:30 +08:00
|
|
|
FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// Verify that there is nothing after the string, other than EOD. Because
|
2009-04-18 07:30:53 +08:00
|
|
|
// of C99 6.10.4p5, macros that expand to empty tokens are ok.
|
|
|
|
CheckEndOfDirective("line", true);
|
2009-01-26 13:29:08 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-04 05:52:55 +08:00
|
|
|
SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 01:13:49 +08:00
|
|
|
if (Callbacks)
|
2010-04-14 12:28:50 +08:00
|
|
|
Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
|
|
|
|
PPCallbacks::RenameFile,
|
2009-03-28 01:13:49 +08:00
|
|
|
SrcMgr::C_User);
|
2009-01-26 13:29:08 +08:00
|
|
|
}
|
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
|
|
|
|
/// marker directive.
|
|
|
|
static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
|
|
|
|
bool &IsSystemHeader, bool &IsExternCHeader,
|
|
|
|
Preprocessor &PP) {
|
|
|
|
unsigned FlagVal;
|
|
|
|
Token FlagTok;
|
|
|
|
PP.Lex(FlagTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (FlagTok.is(tok::eod)) return false;
|
2009-01-26 14:19:46 +08:00
|
|
|
if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (FlagVal == 1) {
|
|
|
|
IsFileEntry = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
PP.Lex(FlagTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (FlagTok.is(tok::eod)) return false;
|
2009-01-26 14:19:46 +08:00
|
|
|
if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
|
|
|
|
return true;
|
|
|
|
} else if (FlagVal == 2) {
|
|
|
|
IsFileExit = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-04 14:25:26 +08:00
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
// If we are leaving the current presumed file, check to make sure the
|
|
|
|
// presumed include stack isn't empty!
|
|
|
|
FileID CurFileID =
|
2011-07-26 04:52:32 +08:00
|
|
|
SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
|
2009-02-04 14:25:26 +08:00
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
|
2010-11-12 15:15:47 +08:00
|
|
|
if (PLoc.isInvalid())
|
|
|
|
return true;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2009-02-04 14:25:26 +08:00
|
|
|
// If there is no include loc (main file) or if the include loc is in a
|
|
|
|
// different physical file, then we aren't in a "1" line marker flag region.
|
|
|
|
SourceLocation IncLoc = PLoc.getIncludeLoc();
|
|
|
|
if (IncLoc.isInvalid() ||
|
2011-07-26 04:52:32 +08:00
|
|
|
SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
|
2009-02-04 14:25:26 +08:00
|
|
|
PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
|
|
|
|
PP.DiscardUntilEndOfDirective();
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
PP.Lex(FlagTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (FlagTok.is(tok::eod)) return false;
|
2009-01-26 14:19:46 +08:00
|
|
|
if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must have 3 if there are still flags.
|
|
|
|
if (FlagVal != 3) {
|
|
|
|
PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
|
2009-01-26 15:57:50 +08:00
|
|
|
PP.DiscardUntilEndOfDirective();
|
2009-01-26 14:19:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
IsSystemHeader = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
PP.Lex(FlagTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (FlagTok.is(tok::eod)) return false;
|
2009-02-04 13:21:58 +08:00
|
|
|
if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
|
2009-01-26 14:19:46 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// We must have 4 if there is yet another flag.
|
|
|
|
if (FlagVal != 4) {
|
|
|
|
PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
|
2009-01-26 15:57:50 +08:00
|
|
|
PP.DiscardUntilEndOfDirective();
|
2009-01-26 14:19:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
IsExternCHeader = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
PP.Lex(FlagTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (FlagTok.is(tok::eod)) return false;
|
2009-01-26 14:19:46 +08:00
|
|
|
|
|
|
|
// There are no more valid flags here.
|
|
|
|
PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
|
2009-01-26 15:57:50 +08:00
|
|
|
PP.DiscardUntilEndOfDirective();
|
2009-01-26 14:19:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
|
|
|
|
/// one of the following forms:
|
|
|
|
///
|
|
|
|
/// # 42
|
2009-09-09 23:08:12 +08:00
|
|
|
/// # 42 "file" ('1' | '2')?
|
2009-01-26 14:19:46 +08:00
|
|
|
/// # 42 "file" ('1' | '2')? '3' '4'?
|
|
|
|
///
|
|
|
|
void Preprocessor::HandleDigitDirective(Token &DigitTok) {
|
|
|
|
// Validate the number and convert it to an unsigned. GNU does not have a
|
|
|
|
// line # limit other than it fit in 32-bits.
|
|
|
|
unsigned LineNo;
|
|
|
|
if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
|
2013-04-10 09:04:18 +08:00
|
|
|
*this, true))
|
2009-01-26 14:19:46 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
Token StrTok;
|
|
|
|
Lex(StrTok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
bool IsFileEntry = false, IsFileExit = false;
|
|
|
|
bool IsSystemHeader = false, IsExternCHeader = false;
|
2009-01-26 15:57:50 +08:00
|
|
|
int FilenameID = -1;
|
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
|
|
|
|
// string followed by eod.
|
|
|
|
if (StrTok.is(tok::eod))
|
2009-01-26 14:19:46 +08:00
|
|
|
; // ok
|
|
|
|
else if (StrTok.isNot(tok::string_literal)) {
|
|
|
|
Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
|
2009-01-26 15:57:50 +08:00
|
|
|
return DiscardUntilEndOfDirective();
|
2012-03-06 11:21:47 +08:00
|
|
|
} else if (StrTok.hasUDSuffix()) {
|
|
|
|
Diag(StrTok, diag::err_invalid_string_udl);
|
|
|
|
return DiscardUntilEndOfDirective();
|
2009-01-26 14:19:46 +08:00
|
|
|
} else {
|
2009-01-26 15:57:50 +08:00
|
|
|
// Parse and validate the string, converting it into a unique ID.
|
2014-06-26 12:58:39 +08:00
|
|
|
StringLiteralParser Literal(StrTok, *this);
|
2011-07-27 13:40:30 +08:00
|
|
|
assert(Literal.isAscii() && "Didn't allow wide strings in");
|
2009-01-26 15:57:50 +08:00
|
|
|
if (Literal.hadError)
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
if (Literal.Pascal) {
|
|
|
|
Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
}
|
2011-06-21 23:13:30 +08:00
|
|
|
FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-26 14:19:46 +08:00
|
|
|
// If a filename was present, read any flags that are present.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
|
2009-01-26 15:57:50 +08:00
|
|
|
IsSystemHeader, IsExternCHeader, *this))
|
2009-01-26 14:19:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-04 13:21:58 +08:00
|
|
|
// Create a line note with this information.
|
|
|
|
SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
|
2009-09-09 23:08:12 +08:00
|
|
|
IsFileEntry, IsFileExit,
|
2009-02-04 13:21:58 +08:00
|
|
|
IsSystemHeader, IsExternCHeader);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 01:13:49 +08:00
|
|
|
// If the preprocessor has callbacks installed, notify them of the #line
|
|
|
|
// change. This is used so that the line marker comes out in -E mode for
|
|
|
|
// example.
|
|
|
|
if (Callbacks) {
|
|
|
|
PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
|
|
|
|
if (IsFileEntry)
|
|
|
|
Reason = PPCallbacks::EnterFile;
|
|
|
|
else if (IsFileExit)
|
|
|
|
Reason = PPCallbacks::ExitFile;
|
|
|
|
SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
|
|
|
|
if (IsExternCHeader)
|
|
|
|
FileKind = SrcMgr::C_ExternCSystem;
|
|
|
|
else if (IsSystemHeader)
|
|
|
|
FileKind = SrcMgr::C_System;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 12:28:50 +08:00
|
|
|
Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
|
2009-03-28 01:13:49 +08:00
|
|
|
}
|
2009-01-26 14:19:46 +08:00
|
|
|
}
|
|
|
|
|
2009-01-26 13:30:54 +08:00
|
|
|
/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
|
2008-03-09 09:54:53 +08:00
|
|
|
bool isWarning) {
|
2009-01-26 13:30:54 +08:00
|
|
|
// PTH doesn't emit #warning or #error directives.
|
|
|
|
if (CurPTHLexer)
|
2009-01-26 13:29:08 +08:00
|
|
|
return CurPTHLexer->DiscardToEndOfLine();
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the rest of the line raw. We do this because we don't want macros
|
|
|
|
// to be expanded and we don't require that the tokens be valid preprocessing
|
|
|
|
// tokens. For example, this is allowed: "#warning ` 'foo". GCC does
|
|
|
|
// collapse multiple consequtive white space between tokens, but this isn't
|
|
|
|
// specified by the standard.
|
2012-05-19 03:32:16 +08:00
|
|
|
SmallString<128> Message;
|
|
|
|
CurLexer->ReadToEndOfLine(&Message);
|
2012-02-02 08:16:13 +08:00
|
|
|
|
|
|
|
// Find the first non-whitespace character, so that we can make the
|
|
|
|
// diagnostic more succinct.
|
2016-02-25 06:07:26 +08:00
|
|
|
StringRef Msg = StringRef(Message).ltrim(' ');
|
2012-05-19 03:32:16 +08:00
|
|
|
|
2009-01-26 13:29:08 +08:00
|
|
|
if (isWarning)
|
2012-02-02 08:16:13 +08:00
|
|
|
Diag(Tok, diag::pp_hash_warning) << Msg;
|
2009-01-26 13:29:08 +08:00
|
|
|
else
|
2012-02-02 08:16:13 +08:00
|
|
|
Diag(Tok, diag::err_pp_hash_error) << Msg;
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
|
|
|
|
///
|
|
|
|
void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
|
|
|
|
// Yes, this directive is an extension.
|
|
|
|
Diag(Tok, diag::ext_pp_ident_directive);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the string argument.
|
|
|
|
Token StrTok;
|
|
|
|
Lex(StrTok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If the token kind isn't a string, it's a malformed directive.
|
|
|
|
if (StrTok.isNot(tok::string_literal) &&
|
2008-11-18 15:59:24 +08:00
|
|
|
StrTok.isNot(tok::wide_string_literal)) {
|
|
|
|
Diag(StrTok, diag::err_pp_malformed_ident);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (StrTok.isNot(tok::eod))
|
2009-01-26 13:30:54 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2008-11-18 15:59:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-03-06 11:21:47 +08:00
|
|
|
if (StrTok.hasUDSuffix()) {
|
|
|
|
Diag(StrTok, diag::err_invalid_string_udl);
|
|
|
|
return DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// Verify that there is nothing after the string, other than EOD.
|
2009-04-14 13:07:49 +08:00
|
|
|
CheckEndOfDirective("ident");
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2010-03-17 06:30:13 +08:00
|
|
|
if (Callbacks) {
|
|
|
|
bool Invalid = false;
|
|
|
|
std::string Str = getSpelling(StrTok, &Invalid);
|
|
|
|
if (!Invalid)
|
|
|
|
Callbacks->Ident(Tok.getLocation(), Str);
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2012-01-04 02:24:14 +08:00
|
|
|
/// \brief Handle a #public directive.
|
|
|
|
void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
|
2011-09-02 01:04:32 +08:00
|
|
|
Token MacroNameTok;
|
2014-10-25 01:31:32 +08:00
|
|
|
ReadMacroName(MacroNameTok, MU_Undef);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-09-02 01:04:32 +08:00
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
|
|
|
if (MacroNameTok.is(tok::eod))
|
|
|
|
return;
|
|
|
|
|
2012-01-04 03:48:16 +08:00
|
|
|
// Check to see if this is the last token on the #__public_macro line.
|
|
|
|
CheckEndOfDirective("__public_macro");
|
2011-09-02 01:04:32 +08:00
|
|
|
|
2013-03-23 05:12:57 +08:00
|
|
|
IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
|
2011-09-02 01:04:32 +08:00
|
|
|
// Okay, we finally have a valid identifier to undef.
|
2015-04-30 07:20:19 +08:00
|
|
|
MacroDirective *MD = getLocalMacroDirective(II);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-09-02 01:04:32 +08:00
|
|
|
// If the macro is not defined, this is an error.
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!MD) {
|
2013-03-23 05:12:57 +08:00
|
|
|
Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
|
2011-09-02 01:04:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-09-02 01:04:32 +08:00
|
|
|
// Note that this macro has now been exported.
|
2013-03-27 01:17:01 +08:00
|
|
|
appendMacroDirective(II, AllocateVisibilityMacroDirective(
|
|
|
|
MacroNameTok.getLocation(), /*IsPublic=*/true));
|
2011-10-17 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2012-01-04 02:24:14 +08:00
|
|
|
/// \brief Handle a #private directive.
|
2011-10-17 23:32:29 +08:00
|
|
|
void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
|
|
|
|
Token MacroNameTok;
|
2014-10-25 01:31:32 +08:00
|
|
|
ReadMacroName(MacroNameTok, MU_Undef);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-10-17 23:32:29 +08:00
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
|
|
|
if (MacroNameTok.is(tok::eod))
|
|
|
|
return;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2012-01-04 03:48:16 +08:00
|
|
|
// Check to see if this is the last token on the #__private_macro line.
|
|
|
|
CheckEndOfDirective("__private_macro");
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2013-03-23 05:12:57 +08:00
|
|
|
IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
|
2011-10-17 23:32:29 +08:00
|
|
|
// Okay, we finally have a valid identifier to undef.
|
2015-04-30 07:20:19 +08:00
|
|
|
MacroDirective *MD = getLocalMacroDirective(II);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-10-17 23:32:29 +08:00
|
|
|
// If the macro is not defined, this is an error.
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!MD) {
|
2013-03-23 05:12:57 +08:00
|
|
|
Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
|
2011-10-17 23:32:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-10-17 23:32:29 +08:00
|
|
|
// Note that this macro has now been marked private.
|
2013-03-27 01:17:01 +08:00
|
|
|
appendMacroDirective(II, AllocateVisibilityMacroDirective(
|
|
|
|
MacroNameTok.getLocation(), /*IsPublic=*/false));
|
2011-09-02 01:04:32 +08:00
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Include Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
|
2012-06-22 13:46:07 +08:00
|
|
|
/// checked and spelled filename, e.g. as an operand of \#include. This returns
|
2008-03-09 09:54:53 +08:00
|
|
|
/// true if the input filename was in <>'s or false if it were in ""'s. The
|
|
|
|
/// caller is expected to provide a buffer that is large enough to hold the
|
|
|
|
/// spelling of the filename, but is also expected to handle the case when
|
|
|
|
/// this method decides to use a different buffer.
|
|
|
|
bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef &Buffer) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// Get the text form of the filename.
|
2010-01-10 09:35:12 +08:00
|
|
|
assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Make sure the filename is <x> or "x".
|
|
|
|
bool isAngled;
|
2010-01-10 09:35:12 +08:00
|
|
|
if (Buffer[0] == '<') {
|
|
|
|
if (Buffer.back() != '>') {
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(Loc, diag::err_pp_expects_filename);
|
2011-07-23 18:55:15 +08:00
|
|
|
Buffer = StringRef();
|
2008-03-09 09:54:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
isAngled = true;
|
2010-01-10 09:35:12 +08:00
|
|
|
} else if (Buffer[0] == '"') {
|
|
|
|
if (Buffer.back() != '"') {
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(Loc, diag::err_pp_expects_filename);
|
2011-07-23 18:55:15 +08:00
|
|
|
Buffer = StringRef();
|
2008-03-09 09:54:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
isAngled = false;
|
|
|
|
} else {
|
|
|
|
Diag(Loc, diag::err_pp_expects_filename);
|
2011-07-23 18:55:15 +08:00
|
|
|
Buffer = StringRef();
|
2008-03-09 09:54:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Diagnose #include "" as invalid.
|
2010-01-10 09:35:12 +08:00
|
|
|
if (Buffer.size() <= 2) {
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(Loc, diag::err_pp_empty_filename);
|
2011-07-23 18:55:15 +08:00
|
|
|
Buffer = StringRef();
|
2010-01-10 09:35:12 +08:00
|
|
|
return true;
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Skip the brackets.
|
2010-01-10 09:35:12 +08:00
|
|
|
Buffer = Buffer.substr(1, Buffer.size()-2);
|
2008-03-09 09:54:53 +08:00
|
|
|
return isAngled;
|
|
|
|
}
|
|
|
|
|
2013-11-27 09:27:40 +08:00
|
|
|
// \brief Handle cases where the \#include name is expanded from a macro
|
|
|
|
// as multiple tokens, which need to be glued together.
|
|
|
|
//
|
|
|
|
// This occurs for code like:
|
|
|
|
// \code
|
|
|
|
// \#define FOO <a/b.h>
|
|
|
|
// \#include FOO
|
|
|
|
// \endcode
|
|
|
|
// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
|
|
|
|
//
|
|
|
|
// This code concatenates and consumes tokens up to the '>' token. It returns
|
|
|
|
// false if the > was found, otherwise it returns true if it finds and consumes
|
|
|
|
// the EOD marker.
|
|
|
|
bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
|
2010-10-21 06:00:55 +08:00
|
|
|
SourceLocation &End) {
|
2008-03-09 09:54:53 +08:00
|
|
|
Token CurTok;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
Lex(CurTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
while (CurTok.isNot(tok::eod)) {
|
2010-10-21 06:00:55 +08:00
|
|
|
End = CurTok.getLocation();
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2010-12-10 07:35:36 +08:00
|
|
|
// FIXME: Provide code completion for #includes.
|
|
|
|
if (CurTok.is(tok::code_completion)) {
|
2011-09-04 11:32:15 +08:00
|
|
|
setCodeCompletionReached();
|
2010-12-10 07:35:36 +08:00
|
|
|
Lex(CurTok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Append the spelling of this token to the buffer. If there was a space
|
|
|
|
// before it, add it now.
|
|
|
|
if (CurTok.hasLeadingSpace())
|
|
|
|
FilenameBuffer.push_back(' ');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Get the spelling of the token, directly into FilenameBuffer if possible.
|
|
|
|
unsigned PreAppendSize = FilenameBuffer.size();
|
|
|
|
FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
const char *BufPtr = &FilenameBuffer[PreAppendSize];
|
2009-10-30 21:49:06 +08:00
|
|
|
unsigned ActualLen = getSpelling(CurTok, BufPtr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If the token was spelled somewhere else, copy it into FilenameBuffer.
|
|
|
|
if (BufPtr != &FilenameBuffer[PreAppendSize])
|
|
|
|
memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Resize FilenameBuffer to the correct size.
|
|
|
|
if (CurTok.getLength() != ActualLen)
|
|
|
|
FilenameBuffer.resize(PreAppendSize+ActualLen);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If we found the '>' marker, return success.
|
|
|
|
if (CurTok.is(tok::greater))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
Lex(CurTok);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// If we hit the eod marker, emit an error and return true so that the caller
|
|
|
|
// knows the EOD has been read.
|
2009-10-30 21:49:06 +08:00
|
|
|
Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
|
2008-03-09 09:54:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-23 12:06:09 +08:00
|
|
|
/// \brief Push a token onto the token stream containing an annotation.
|
|
|
|
static void EnterAnnotationToken(Preprocessor &PP,
|
|
|
|
SourceLocation Begin, SourceLocation End,
|
|
|
|
tok::TokenKind Kind, void *AnnotationVal) {
|
2015-05-14 10:25:44 +08:00
|
|
|
// FIXME: Produce this as the current token directly, rather than
|
|
|
|
// allocating a new token for it.
|
2016-02-10 02:52:09 +08:00
|
|
|
auto Tok = llvm::make_unique<Token[]>(1);
|
2013-11-23 12:06:09 +08:00
|
|
|
Tok[0].startToken();
|
|
|
|
Tok[0].setKind(Kind);
|
|
|
|
Tok[0].setLocation(Begin);
|
|
|
|
Tok[0].setAnnotationEndLoc(End);
|
|
|
|
Tok[0].setAnnotationValue(AnnotationVal);
|
2016-02-10 02:52:09 +08:00
|
|
|
PP.EnterTokenStream(std::move(Tok), 1, true);
|
2013-11-23 12:06:09 +08:00
|
|
|
}
|
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
/// \brief Produce a diagnostic informing the user that a #include or similar
|
|
|
|
/// was implicitly treated as a module import.
|
|
|
|
static void diagnoseAutoModuleImport(
|
|
|
|
Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
|
|
|
|
ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
|
|
|
|
SourceLocation PathEnd) {
|
|
|
|
assert(PP.getLangOpts().ObjC2 && "no import syntax available");
|
|
|
|
|
|
|
|
SmallString<128> PathString;
|
|
|
|
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
|
|
|
|
if (I)
|
|
|
|
PathString += '.';
|
|
|
|
PathString += Path[I].first->getName();
|
|
|
|
}
|
|
|
|
int IncludeKind = 0;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
|
|
|
|
case tok::pp_include:
|
|
|
|
IncludeKind = 0;
|
|
|
|
break;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
case tok::pp_import:
|
|
|
|
IncludeKind = 1;
|
2016-06-14 05:55:33 +08:00
|
|
|
break;
|
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
case tok::pp_include_next:
|
|
|
|
IncludeKind = 2;
|
|
|
|
break;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
case tok::pp___include_macros:
|
|
|
|
IncludeKind = 3;
|
|
|
|
break;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown include directive kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
|
|
|
|
/*IsTokenRange=*/false);
|
|
|
|
PP.Diag(HashLoc, diag::warn_auto_module_import)
|
|
|
|
<< IncludeKind << PathString
|
|
|
|
<< FixItHint::CreateReplacement(ReplaceRange,
|
|
|
|
("@import " + PathString + ";").str());
|
|
|
|
}
|
|
|
|
|
2016-06-14 04:40:21 +08:00
|
|
|
// Given a vector of path components and a string containing the real
|
|
|
|
// path to the file, build a properly-cased replacement in the vector,
|
|
|
|
// and return true if the replacement should be suggested.
|
|
|
|
static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
|
|
|
|
StringRef RealPathName) {
|
|
|
|
auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
|
|
|
|
auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
|
|
|
|
int Cnt = 0;
|
|
|
|
bool SuggestReplacement = false;
|
|
|
|
// Below is a best-effort to handle ".." in paths. It is admittedly
|
|
|
|
// not 100% correct in the presence of symlinks.
|
|
|
|
for (auto &Component : llvm::reverse(Components)) {
|
|
|
|
if ("." == Component) {
|
|
|
|
} else if (".." == Component) {
|
|
|
|
++Cnt;
|
|
|
|
} else if (Cnt) {
|
|
|
|
--Cnt;
|
|
|
|
} else if (RealPathComponentIter != RealPathComponentEnd) {
|
|
|
|
if (Component != *RealPathComponentIter) {
|
|
|
|
// If these path components differ by more than just case, then we
|
|
|
|
// may be looking at symlinked paths. Bail on this diagnostic to avoid
|
|
|
|
// noisy false positives.
|
|
|
|
SuggestReplacement = RealPathComponentIter->equals_lower(Component);
|
|
|
|
if (!SuggestReplacement)
|
|
|
|
break;
|
|
|
|
Component = *RealPathComponentIter;
|
|
|
|
}
|
|
|
|
++RealPathComponentIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SuggestReplacement;
|
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleIncludeDirective - The "\#include" tokens have just been read, read
|
|
|
|
/// the file to be included from the lexer, then include it! This is a common
|
|
|
|
/// routine with functionality shared between \#include, \#include_next and
|
|
|
|
/// \#import. LookupFrom is set when this is a \#include_next directive, it
|
2009-09-09 23:08:12 +08:00
|
|
|
/// specifies the file to start searching from.
|
2016-06-14 05:55:33 +08:00
|
|
|
void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
|
2010-10-21 06:00:55 +08:00
|
|
|
Token &IncludeTok,
|
2008-03-09 09:54:53 +08:00
|
|
|
const DirectoryLookup *LookupFrom,
|
2014-10-20 08:15:49 +08:00
|
|
|
const FileEntry *LookupFromFile,
|
2008-03-09 09:54:53 +08:00
|
|
|
bool isImport) {
|
|
|
|
Token FilenameTok;
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->LexIncludeFilename(FilenameTok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Reserve a buffer to get the spelling.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilenameBuffer;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename;
|
2010-10-21 06:00:55 +08:00
|
|
|
SourceLocation End;
|
2011-12-01 02:02:36 +08:00
|
|
|
SourceLocation CharEnd; // the end of this directive, in characters
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
switch (FilenameTok.getKind()) {
|
2011-02-28 10:37:51 +08:00
|
|
|
case tok::eod:
|
|
|
|
// If the token kind is EOD, the error has already been diagnosed.
|
2008-03-09 09:54:53 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
case tok::angle_string_literal:
|
2010-02-27 21:44:12 +08:00
|
|
|
case tok::string_literal:
|
|
|
|
Filename = getSpelling(FilenameTok, FilenameBuffer);
|
2010-10-21 06:00:55 +08:00
|
|
|
End = FilenameTok.getLocation();
|
2012-11-02 01:52:58 +08:00
|
|
|
CharEnd = End.getLocWithOffset(FilenameTok.getLength());
|
2008-03-09 09:54:53 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
case tok::less:
|
|
|
|
// This could be a <foo/bar.h> file coming from a macro expansion. In this
|
|
|
|
// case, glue the tokens together into FilenameBuffer and interpret those.
|
|
|
|
FilenameBuffer.push_back('<');
|
2010-10-21 06:00:55 +08:00
|
|
|
if (ConcatenateIncludeName(FilenameBuffer, End))
|
2011-02-28 10:37:51 +08:00
|
|
|
return; // Found <eod> but no ">"? Diagnostic already emitted.
|
2015-03-18 18:17:07 +08:00
|
|
|
Filename = FilenameBuffer;
|
2012-11-02 01:52:58 +08:00
|
|
|
CharEnd = End.getLocWithOffset(1);
|
2008-03-09 09:54:53 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-09-29 09:06:10 +08:00
|
|
|
CharSourceRange FilenameRange
|
|
|
|
= CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
|
2012-03-03 06:51:54 +08:00
|
|
|
StringRef OriginalFilename = Filename;
|
2010-03-13 18:17:05 +08:00
|
|
|
bool isAngled =
|
2010-01-10 09:35:12 +08:00
|
|
|
GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
|
2008-03-09 09:54:53 +08:00
|
|
|
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
|
|
|
|
// error.
|
2010-01-10 09:35:12 +08:00
|
|
|
if (Filename.empty()) {
|
2008-03-09 09:54:53 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// Verify that there is nothing after the filename, other than EOD. Note that
|
2009-04-18 07:56:52 +08:00
|
|
|
// we allow macros that expand to nothing after the filename, because this
|
|
|
|
// falls into the category of "#include pp-tokens new-line" specified in
|
|
|
|
// C99 6.10.2p4.
|
2009-10-19 04:26:12 +08:00
|
|
|
CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
|
2008-03-09 09:54:53 +08:00
|
|
|
|
|
|
|
// Check that we don't have infinite #include recursion.
|
2008-11-18 15:59:24 +08:00
|
|
|
if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
|
|
|
|
Diag(FilenameTok, diag::err_pp_include_too_deep);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-30 13:12:12 +08:00
|
|
|
// Complain about attempts to #include files in an audit pragma.
|
|
|
|
if (PragmaARCCFCodeAuditedLoc.isValid()) {
|
|
|
|
Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
|
|
|
|
Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
|
|
|
|
|
|
|
|
// Immediately leave the pragma.
|
|
|
|
PragmaARCCFCodeAuditedLoc = SourceLocation();
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:25:57 +08:00
|
|
|
// Complain about attempts to #include files in an assume-nonnull pragma.
|
|
|
|
if (PragmaAssumeNonNullLoc.isValid()) {
|
|
|
|
Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
|
|
|
|
Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
|
|
|
|
|
|
|
|
// Immediately leave the pragma.
|
|
|
|
PragmaAssumeNonNullLoc = SourceLocation();
|
|
|
|
}
|
|
|
|
|
2012-03-03 06:51:54 +08:00
|
|
|
if (HeaderInfo.HasIncludeAliasMap()) {
|
2016-06-14 05:55:33 +08:00
|
|
|
// Map the filename with the brackets still attached. If the name doesn't
|
|
|
|
// map to anything, fall back on the filename we've already gotten the
|
2012-03-03 06:51:54 +08:00
|
|
|
// spelling for.
|
|
|
|
StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
|
|
|
|
if (!NewName.empty())
|
|
|
|
Filename = NewName;
|
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Search include directories.
|
|
|
|
const DirectoryLookup *CurDir;
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<1024> SearchPath;
|
|
|
|
SmallString<1024> RelativePath;
|
2011-03-17 02:34:36 +08:00
|
|
|
// We get the raw path only if we have 'Callbacks' to which we later pass
|
|
|
|
// the path.
|
2013-06-21 05:14:14 +08:00
|
|
|
ModuleMap::KnownHeader SuggestedModule;
|
|
|
|
SourceLocation FilenameLoc = FilenameTok.getLocation();
|
2014-03-12 10:26:08 +08:00
|
|
|
SmallString<128> NormalizedPath;
|
2014-03-12 06:41:45 +08:00
|
|
|
if (LangOpts.MSVCCompat) {
|
|
|
|
NormalizedPath = Filename.str();
|
2014-08-10 02:13:01 +08:00
|
|
|
#ifndef LLVM_ON_WIN32
|
2014-08-09 05:31:04 +08:00
|
|
|
llvm::sys::path::native(NormalizedPath);
|
2014-08-10 02:13:01 +08:00
|
|
|
#endif
|
2014-03-12 06:41:45 +08:00
|
|
|
}
|
2011-03-17 02:34:36 +08:00
|
|
|
const FileEntry *File = LookupFile(
|
2014-03-12 06:41:45 +08:00
|
|
|
FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
|
2014-10-20 08:15:49 +08:00
|
|
|
isAngled, LookupFrom, LookupFromFile, CurDir,
|
|
|
|
Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
|
2015-06-16 08:08:24 +08:00
|
|
|
&SuggestedModule);
|
2011-09-16 06:00:41 +08:00
|
|
|
|
2015-05-14 10:25:44 +08:00
|
|
|
if (!File) {
|
|
|
|
if (Callbacks) {
|
2011-12-01 02:12:06 +08:00
|
|
|
// Give the clients a chance to recover.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> RecoveryPath;
|
2011-12-01 02:12:06 +08:00
|
|
|
if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
|
|
|
|
if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
|
|
|
|
// Add the recovery path to the list of search paths.
|
2013-01-25 09:50:28 +08:00
|
|
|
DirectoryLookup DL(DE, SrcMgr::C_User, false);
|
2011-12-01 02:12:06 +08:00
|
|
|
HeaderInfo.AddSearchPath(DL, isAngled);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2011-12-01 02:12:06 +08:00
|
|
|
// Try the lookup again, skipping the cache.
|
2014-10-20 08:15:49 +08:00
|
|
|
File = LookupFile(
|
|
|
|
FilenameLoc,
|
|
|
|
LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
|
|
|
|
LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
|
2015-06-16 08:08:24 +08:00
|
|
|
&SuggestedModule, /*SkipCache*/ true);
|
2011-12-01 02:12:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-18 07:10:59 +08:00
|
|
|
|
2012-07-18 07:19:16 +08:00
|
|
|
if (!SuppressIncludeNotFoundError) {
|
2016-06-14 05:55:33 +08:00
|
|
|
// If the file could not be located and it was included via angle
|
2012-07-18 07:19:16 +08:00
|
|
|
// brackets, we can attempt a lookup as though it were a quoted path to
|
|
|
|
// provide the user with a possible fixit.
|
|
|
|
if (isAngled) {
|
2013-08-06 04:26:17 +08:00
|
|
|
File = LookupFile(
|
2014-10-20 08:15:49 +08:00
|
|
|
FilenameLoc,
|
|
|
|
LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
|
|
|
|
LookupFrom, LookupFromFile, CurDir,
|
|
|
|
Callbacks ? &SearchPath : nullptr,
|
2014-05-18 07:10:59 +08:00
|
|
|
Callbacks ? &RelativePath : nullptr,
|
2015-06-16 08:08:24 +08:00
|
|
|
&SuggestedModule);
|
2012-07-18 07:19:16 +08:00
|
|
|
if (File) {
|
|
|
|
SourceRange Range(FilenameTok.getLocation(), CharEnd);
|
2016-06-14 05:55:33 +08:00
|
|
|
Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
|
|
|
|
Filename <<
|
2012-07-18 07:19:16 +08:00
|
|
|
FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 10:25:44 +08:00
|
|
|
|
2012-07-18 07:19:16 +08:00
|
|
|
// If the file is still not found, just go with the vanilla diagnostic
|
|
|
|
if (!File)
|
|
|
|
Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
|
|
|
|
}
|
2011-12-01 02:12:06 +08:00
|
|
|
}
|
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
// Should we enter the source file? Set to false if either the source file is
|
|
|
|
// known to have no effect beyond its effect on module visibility -- that is,
|
|
|
|
// if it's got an include guard that is already defined or is a modular header
|
|
|
|
// we've imported or already built.
|
|
|
|
bool ShouldEnter = true;
|
|
|
|
|
|
|
|
// Determine whether we should try to import the module for this #include, if
|
|
|
|
// there is one. Don't do so if precompiled module support is disabled or we
|
|
|
|
// are processing this module textually (because we're building the module).
|
|
|
|
if (File && SuggestedModule && getLangOpts().Modules &&
|
|
|
|
SuggestedModule.getModule()->getTopLevelModuleName() !=
|
2016-02-20 06:25:36 +08:00
|
|
|
getLangOpts().CurrentModule) {
|
[modules] PR20507: Avoid silent textual inclusion.
Summary:
If a module was unavailable (either a missing requirement on the module
being imported, or a missing file anywhere in the top-level module (and
not dominated by an unsatisfied `requires`)), we would silently treat
inclusions as textual. This would cause all manner of crazy and
confusing errors (and would also silently "work" sometimes, making the
problem difficult to track down).
I'm really not a fan of the `M->isAvailable(getLangOpts(), getTargetInfo(),
Requirement, MissingHeader)` function; it seems to do too many things at
once, but for now I've done things in a sort of awkward way.
The changes to test/Modules/Inputs/declare-use/module.map
were necessitated because the thing that was meant to be tested there
(introduced in r197805) was predicated on silently falling back to textual
inclusion, which we no longer do.
The changes to test/Modules/Inputs/macro-reexport/module.modulemap
are just an overlooked missing header that seems to have been missing since
this code was committed (r213922), which is now caught.
Reviewers: rsmith, benlangmuir, djasper
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D10423
llvm-svn: 245228
2015-08-18 00:39:30 +08:00
|
|
|
// If this include corresponds to a module but that module is
|
|
|
|
// unavailable, diagnose the situation and bail out.
|
2016-04-13 03:58:30 +08:00
|
|
|
// FIXME: Remove this; loadModule does the same check (but produces
|
|
|
|
// slightly worse diagnostics).
|
|
|
|
if (!SuggestedModule.getModule()->isAvailable() &&
|
2016-04-13 04:20:33 +08:00
|
|
|
!SuggestedModule.getModule()
|
|
|
|
->getTopLevelModule()
|
|
|
|
->HasIncompatibleModuleFile) {
|
2016-09-08 05:53:17 +08:00
|
|
|
Module::Requirement Requirement;
|
|
|
|
Module::UnresolvedHeaderDirective MissingHeader;
|
[modules] PR20507: Avoid silent textual inclusion.
Summary:
If a module was unavailable (either a missing requirement on the module
being imported, or a missing file anywhere in the top-level module (and
not dominated by an unsatisfied `requires`)), we would silently treat
inclusions as textual. This would cause all manner of crazy and
confusing errors (and would also silently "work" sometimes, making the
problem difficult to track down).
I'm really not a fan of the `M->isAvailable(getLangOpts(), getTargetInfo(),
Requirement, MissingHeader)` function; it seems to do too many things at
once, but for now I've done things in a sort of awkward way.
The changes to test/Modules/Inputs/declare-use/module.map
were necessitated because the thing that was meant to be tested there
(introduced in r197805) was predicated on silently falling back to textual
inclusion, which we no longer do.
The changes to test/Modules/Inputs/macro-reexport/module.modulemap
are just an overlooked missing header that seems to have been missing since
this code was committed (r213922), which is now caught.
Reviewers: rsmith, benlangmuir, djasper
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D10423
llvm-svn: 245228
2015-08-18 00:39:30 +08:00
|
|
|
Module *M = SuggestedModule.getModule();
|
|
|
|
// Identify the cause.
|
|
|
|
(void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
|
|
|
|
MissingHeader);
|
|
|
|
if (MissingHeader.FileNameLoc.isValid()) {
|
|
|
|
Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
|
|
|
|
<< MissingHeader.IsUmbrella << MissingHeader.FileName;
|
|
|
|
} else {
|
|
|
|
Diag(M->DefinitionLoc, diag::err_module_unavailable)
|
|
|
|
<< M->getFullModuleName() << Requirement.second << Requirement.first;
|
|
|
|
}
|
|
|
|
Diag(FilenameTok.getLocation(),
|
|
|
|
diag::note_implicit_top_level_module_import_here)
|
|
|
|
<< M->getTopLevelModuleName();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-30 08:36:36 +08:00
|
|
|
// Compute the module access path corresponding to this module.
|
|
|
|
// FIXME: Should we have a second loadModule() overload to avoid this
|
|
|
|
// extra lookup step?
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
|
2013-06-21 05:14:14 +08:00
|
|
|
for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
|
2011-11-30 08:36:36 +08:00
|
|
|
Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
|
|
|
|
FilenameTok.getLocation()));
|
|
|
|
std::reverse(Path.begin(), Path.end());
|
|
|
|
|
2011-12-01 02:02:36 +08:00
|
|
|
// Warn that we're replacing the include/import with a module import.
|
2015-05-18 12:45:41 +08:00
|
|
|
// We only do this in Objective-C, where we have a module-import syntax.
|
|
|
|
if (getLangOpts().ObjC2)
|
|
|
|
diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2015-05-02 10:08:26 +08:00
|
|
|
// Load the module to import its macros. We'll make the declarations
|
2013-11-15 12:24:58 +08:00
|
|
|
// visible when the parser gets here.
|
2015-05-14 10:25:44 +08:00
|
|
|
// FIXME: Pass SuggestedModule in here rather than converting it to a path
|
|
|
|
// and making the module loader convert it back again.
|
2015-05-02 10:08:26 +08:00
|
|
|
ModuleLoadResult Imported = TheModuleLoader.loadModule(
|
|
|
|
IncludeTok.getLocation(), Path, Module::Hidden,
|
|
|
|
/*IsIncludeDirective=*/true);
|
2014-05-18 07:10:59 +08:00
|
|
|
assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
|
2012-09-29 09:06:01 +08:00
|
|
|
"the imported module is different than the suggested one");
|
2013-05-24 13:44:08 +08:00
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
if (Imported)
|
|
|
|
ShouldEnter = false;
|
|
|
|
else if (Imported.isMissingExpected()) {
|
|
|
|
// We failed to find a submodule that we assumed would exist (because it
|
|
|
|
// was in the directory of an umbrella header, for instance), but no
|
|
|
|
// actual module exists for it (because the umbrella header is
|
|
|
|
// incomplete). Treat this as a textual inclusion.
|
|
|
|
SuggestedModule = ModuleMap::KnownHeader();
|
|
|
|
} else {
|
|
|
|
// We hit an error processing the import. Bail out.
|
|
|
|
if (hadModuleLoaderFatalFailure()) {
|
|
|
|
// With a fatal failure in the module loader, we abort parsing.
|
|
|
|
Token &Result = IncludeTok;
|
|
|
|
if (CurLexer) {
|
|
|
|
Result.startToken();
|
|
|
|
CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
|
|
|
|
CurLexer->cutOffLexing();
|
|
|
|
} else {
|
|
|
|
assert(CurPTHLexer && "#include but no current lexer set!");
|
|
|
|
CurPTHLexer->getEOF(Result);
|
|
|
|
}
|
2013-11-15 12:24:58 +08:00
|
|
|
}
|
2011-12-09 01:01:29 +08:00
|
|
|
return;
|
2012-09-29 09:06:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
if (Callbacks) {
|
|
|
|
// Notify the callback object that we've seen an inclusion directive.
|
|
|
|
Callbacks->InclusionDirective(
|
|
|
|
HashLoc, IncludeTok,
|
|
|
|
LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
|
|
|
|
FilenameRange, File, SearchPath, RelativePath,
|
|
|
|
ShouldEnter ? nullptr : SuggestedModule.getModule());
|
2011-09-16 06:00:41 +08:00
|
|
|
}
|
2015-05-18 12:45:41 +08:00
|
|
|
|
|
|
|
if (!File)
|
|
|
|
return;
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2008-09-27 04:12:23 +08:00
|
|
|
// The #included file will be considered to be a system header if either it is
|
|
|
|
// in a system include directory, or if the #includer is a system include
|
|
|
|
// header.
|
2009-09-09 23:08:12 +08:00
|
|
|
SrcMgr::CharacteristicKind FileCharacter =
|
2008-09-27 05:18:42 +08:00
|
|
|
std::max(HeaderInfo.getFileDirFlavor(File),
|
2009-01-19 15:59:15 +08:00
|
|
|
SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-05-20 03:58:11 +08:00
|
|
|
// FIXME: If we have a suggested module, and we've already visited this file,
|
|
|
|
// don't bother entering it again. We know it has no further effect.
|
|
|
|
|
2016-06-14 04:40:21 +08:00
|
|
|
// Issue a diagnostic if the name of the file on disk has a different case
|
|
|
|
// than the one we're about to open.
|
|
|
|
const bool CheckIncludePathPortability =
|
|
|
|
File && !File->tryGetRealPathName().empty();
|
|
|
|
|
|
|
|
if (CheckIncludePathPortability) {
|
|
|
|
StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
|
|
|
|
StringRef RealPathName = File->tryGetRealPathName();
|
|
|
|
SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
|
|
|
|
llvm::sys::path::end(Name));
|
|
|
|
|
|
|
|
if (trySimplifyPath(Components, RealPathName)) {
|
|
|
|
SmallString<128> Path;
|
|
|
|
Path.reserve(Name.size()+2);
|
|
|
|
Path.push_back(isAngled ? '<' : '"');
|
|
|
|
for (auto Component : Components) {
|
|
|
|
Path.append(Component);
|
|
|
|
// Append the separator the user used, or the close quote
|
|
|
|
Path.push_back(
|
|
|
|
Path.size() <= Filename.size() ? Filename[Path.size()-1] :
|
|
|
|
(isAngled ? '>' : '"'));
|
|
|
|
}
|
|
|
|
auto Replacement = Path.str().str();
|
|
|
|
// For user files and known standard headers, by default we issue a diagnostic.
|
|
|
|
// For other system headers, we don't. They can be controlled separately.
|
|
|
|
auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
|
|
|
|
diag::pp_nonportable_path : diag::pp_nonportable_system_path;
|
|
|
|
SourceRange Range(FilenameTok.getLocation(), CharEnd);
|
|
|
|
Diag(FilenameTok, DiagId) << Replacement <<
|
|
|
|
FixItHint::CreateReplacement(Range, Replacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-20 04:44:31 +08:00
|
|
|
// Ask HeaderInfo if we should enter this #include file. If not, #including
|
2015-05-20 03:58:11 +08:00
|
|
|
// this file will have no effect.
|
2015-05-18 12:45:41 +08:00
|
|
|
if (ShouldEnter &&
|
2015-07-01 09:51:38 +08:00
|
|
|
!HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
|
|
|
|
SuggestedModule.getModule())) {
|
2015-05-18 12:45:41 +08:00
|
|
|
ShouldEnter = false;
|
2010-04-21 04:35:58 +08:00
|
|
|
if (Callbacks)
|
2010-04-20 04:44:31 +08:00
|
|
|
Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
|
2015-05-18 12:45:41 +08:00
|
|
|
}
|
2015-05-14 10:25:44 +08:00
|
|
|
|
2015-05-18 12:45:41 +08:00
|
|
|
// If we don't need to enter the file, stop now.
|
|
|
|
if (!ShouldEnter) {
|
2015-05-14 10:25:44 +08:00
|
|
|
// If this is a module import, make it visible if needed.
|
2015-05-18 11:52:30 +08:00
|
|
|
if (auto *M = SuggestedModule.getModule()) {
|
|
|
|
makeModuleVisible(M, HashLoc);
|
2015-05-14 10:25:44 +08:00
|
|
|
|
|
|
|
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
|
|
|
|
tok::pp___include_macros)
|
2015-05-18 11:52:30 +08:00
|
|
|
EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
|
2015-05-14 10:25:44 +08:00
|
|
|
}
|
2010-04-20 04:44:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Look up the file, create a File ID for it.
|
2012-03-28 02:47:48 +08:00
|
|
|
SourceLocation IncludePos = End;
|
|
|
|
// If the filename string was the result of macro expansions, set the include
|
|
|
|
// position on the file where it will be included and after the expansions.
|
|
|
|
if (IncludePos.isMacroID())
|
|
|
|
IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
|
|
|
|
FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
|
2015-10-03 18:46:20 +08:00
|
|
|
assert(FID.isValid() && "Expected valid file ID");
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2013-11-23 12:06:09 +08:00
|
|
|
// If all is good, enter the new file!
|
2014-02-01 04:47:44 +08:00
|
|
|
if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
|
|
|
|
return;
|
2013-11-23 12:06:09 +08:00
|
|
|
|
2015-05-18 11:52:30 +08:00
|
|
|
// Determine if we're switching to building a new submodule, and which one.
|
|
|
|
if (auto *M = SuggestedModule.getModule()) {
|
2014-02-01 04:47:44 +08:00
|
|
|
assert(!CurSubmodule && "should not have marked this as a module yet");
|
2015-05-18 11:52:30 +08:00
|
|
|
CurSubmodule = M;
|
2014-02-01 04:47:44 +08:00
|
|
|
|
2015-05-18 11:52:30 +08:00
|
|
|
// Let the macro handling code know that any future macros are within
|
|
|
|
// the new submodule.
|
|
|
|
EnterSubmodule(M, HashLoc);
|
2015-04-24 02:18:26 +08:00
|
|
|
|
2015-05-18 11:52:30 +08:00
|
|
|
// Let the parser know that any future declarations are within the new
|
|
|
|
// submodule.
|
|
|
|
// FIXME: There's no point doing this if we're handling a #__include_macros
|
|
|
|
// directive.
|
|
|
|
EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
|
2014-02-01 04:47:44 +08:00
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleIncludeNextDirective - Implements \#include_next.
|
2008-03-09 09:54:53 +08:00
|
|
|
///
|
2010-10-21 06:00:55 +08:00
|
|
|
void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
|
|
|
|
Token &IncludeNextTok) {
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// #include_next is like #include, except that we start searching after
|
|
|
|
// the current found directory. If we can't do this, issue a
|
|
|
|
// diagnostic.
|
|
|
|
const DirectoryLookup *Lookup = CurDirLookup;
|
2014-10-20 08:15:49 +08:00
|
|
|
const FileEntry *LookupFromFile = nullptr;
|
2008-03-09 09:54:53 +08:00
|
|
|
if (isInPrimaryFile()) {
|
2014-05-18 07:10:59 +08:00
|
|
|
Lookup = nullptr;
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(IncludeNextTok, diag::pp_include_next_in_primary);
|
2014-10-20 08:15:49 +08:00
|
|
|
} else if (CurSubmodule) {
|
|
|
|
// Start looking up in the directory *after* the one in which the current
|
|
|
|
// file would be found, if any.
|
|
|
|
assert(CurPPLexer && "#include_next directive in macro?");
|
|
|
|
LookupFromFile = CurPPLexer->getFileEntry();
|
|
|
|
Lookup = nullptr;
|
2014-05-18 07:10:59 +08:00
|
|
|
} else if (!Lookup) {
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
|
|
|
|
} else {
|
|
|
|
// Start looking up in the next directory.
|
|
|
|
++Lookup;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-10-20 08:15:49 +08:00
|
|
|
return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
|
|
|
|
LookupFromFile);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
|
2012-03-18 11:10:37 +08:00
|
|
|
void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
|
|
|
|
// The Microsoft #import directive takes a type library and generates header
|
|
|
|
// files from it, and includes those. This is beyond the scope of what clang
|
|
|
|
// does, so we ignore it and error out. However, #import can optionally have
|
|
|
|
// trailing attributes that span multiple lines. We're going to eat those
|
|
|
|
// so we can continue processing from there.
|
|
|
|
Diag(Tok, diag::err_pp_import_directive_ms );
|
|
|
|
|
2016-06-14 05:55:33 +08:00
|
|
|
// Read tokens until we get to the end of the directive. Note that the
|
2012-03-18 11:10:37 +08:00
|
|
|
// directive can be split over multiple lines using the backslash character.
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleImportDirective - Implements \#import.
|
2008-03-09 09:54:53 +08:00
|
|
|
///
|
2010-10-21 06:00:55 +08:00
|
|
|
void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
|
|
|
|
Token &ImportTok) {
|
2012-03-18 11:10:37 +08:00
|
|
|
if (!LangOpts.ObjC1) { // #import is standard for ObjC.
|
2014-01-14 20:51:41 +08:00
|
|
|
if (LangOpts.MSVCCompat)
|
2012-03-18 11:10:37 +08:00
|
|
|
return HandleMicrosoftImportDirective(ImportTok);
|
2009-03-06 12:28:03 +08:00
|
|
|
Diag(ImportTok, diag::ext_pp_import_directive);
|
2012-03-18 11:10:37 +08:00
|
|
|
}
|
2014-10-20 08:15:49 +08:00
|
|
|
return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2009-04-09 02:46:40 +08:00
|
|
|
/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
|
|
|
|
/// pseudo directive in the predefines buffer. This handles it by sucking all
|
|
|
|
/// tokens through the preprocessor and discarding them (only keeping the side
|
|
|
|
/// effects on the preprocessor).
|
2010-10-21 06:00:55 +08:00
|
|
|
void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
|
|
|
|
Token &IncludeMacrosTok) {
|
2009-04-09 02:46:40 +08:00
|
|
|
// This directive should only occur in the predefines buffer. If not, emit an
|
|
|
|
// error and reject it.
|
|
|
|
SourceLocation Loc = IncludeMacrosTok.getLocation();
|
|
|
|
if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
|
|
|
|
Diag(IncludeMacrosTok.getLocation(),
|
|
|
|
diag::pp_include_macros_out_of_predefines);
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-09 04:53:24 +08:00
|
|
|
// Treat this as a normal #include for checking purposes. If this is
|
|
|
|
// successful, it will push a new lexer onto the include stack.
|
2014-10-20 08:15:49 +08:00
|
|
|
HandleIncludeDirective(HashLoc, IncludeMacrosTok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-09 04:53:24 +08:00
|
|
|
Token TmpTok;
|
|
|
|
do {
|
|
|
|
Lex(TmpTok);
|
|
|
|
assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
|
|
|
|
} while (TmpTok.isNot(tok::hashhash));
|
2009-04-09 02:46:40 +08:00
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Macro Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
|
|
|
|
/// definition has just been read. Lex the rest of the arguments and the
|
|
|
|
/// closing ), updating MI with what we learn. Return true if an error occurs
|
|
|
|
/// parsing the arg list.
|
2012-04-01 04:17:27 +08:00
|
|
|
bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<IdentifierInfo*, 32> Arguments;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-09-08 05:53:17 +08:00
|
|
|
while (true) {
|
2008-03-09 09:54:53 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::r_paren:
|
|
|
|
// Found the end of the argument list.
|
2009-02-21 06:31:31 +08:00
|
|
|
if (Arguments.empty()) // #define FOO()
|
2008-03-09 09:54:53 +08:00
|
|
|
return false;
|
|
|
|
// Otherwise we have #define FOO(A,)
|
|
|
|
Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
|
|
|
|
return true;
|
|
|
|
case tok::ellipsis: // #define X(... -> C99 varargs
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!LangOpts.C99)
|
2016-06-14 05:55:33 +08:00
|
|
|
Diag(Tok, LangOpts.CPlusPlus11 ?
|
2011-10-15 09:18:56 +08:00
|
|
|
diag::warn_cxx98_compat_variadic_macro :
|
|
|
|
diag::ext_variadic_macro);
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2013-01-18 01:35:00 +08:00
|
|
|
// OpenCL v1.2 s6.9.e: variadic macros are not supported.
|
|
|
|
if (LangOpts.OpenCL) {
|
|
|
|
Diag(Tok, diag::err_pp_opencl_variadic_macros);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Lex the token after the identifier.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Add the __VA_ARGS__ identifier as an argument.
|
|
|
|
Arguments.push_back(Ident__VA_ARGS__);
|
|
|
|
MI->setIsC99Varargs();
|
2015-10-22 12:59:52 +08:00
|
|
|
MI->setArgumentList(Arguments, BP);
|
2008-03-09 09:54:53 +08:00
|
|
|
return false;
|
2011-02-28 10:37:51 +08:00
|
|
|
case tok::eod: // #define X(
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
// Handle keywords and identifiers here to accept things like
|
|
|
|
// #define Foo(for) for.
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!II) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// #define X(1
|
|
|
|
Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is already used as an argument, it is used multiple times (e.g.
|
|
|
|
// #define X(A,A.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (std::find(Arguments.begin(), Arguments.end(), II) !=
|
2008-03-09 09:54:53 +08:00
|
|
|
Arguments.end()) { // C99 6.10.3p6
|
2008-11-19 15:33:58 +08:00
|
|
|
Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
|
2008-03-09 09:54:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Add the argument to the macro info.
|
|
|
|
Arguments.push_back(II);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Lex the token after the identifier.
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
default: // #define X(A B
|
|
|
|
Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
|
|
|
|
return true;
|
|
|
|
case tok::r_paren: // #define X(A)
|
2015-10-22 12:59:52 +08:00
|
|
|
MI->setArgumentList(Arguments, BP);
|
2008-03-09 09:54:53 +08:00
|
|
|
return false;
|
|
|
|
case tok::comma: // #define X(A,
|
|
|
|
break;
|
|
|
|
case tok::ellipsis: // #define X(A... -> GCC extension
|
|
|
|
// Diagnose extension.
|
|
|
|
Diag(Tok, diag::ext_named_variadic_macro);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Lex the token after the identifier.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
MI->setIsGNUVarargs();
|
2015-10-22 12:59:52 +08:00
|
|
|
MI->setArgumentList(Arguments, BP);
|
2008-03-09 09:54:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
|
|
|
|
const LangOptions &LOptions) {
|
|
|
|
if (MI->getNumTokens() == 1) {
|
|
|
|
const Token &Value = MI->getReplacementToken(0);
|
|
|
|
|
|
|
|
// Macro that is identity, like '#define inline inline' is a valid pattern.
|
|
|
|
if (MacroName.getKind() == Value.getKind())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Macro that maps a keyword to the same keyword decorated with leading/
|
|
|
|
// trailing underscores is a valid pattern:
|
|
|
|
// #define inline __inline
|
|
|
|
// #define inline __inline__
|
|
|
|
// #define inline _inline (in MS compatibility mode)
|
|
|
|
StringRef MacroText = MacroName.getIdentifierInfo()->getName();
|
|
|
|
if (IdentifierInfo *II = Value.getIdentifierInfo()) {
|
|
|
|
if (!II->isKeyword(LOptions))
|
|
|
|
return false;
|
|
|
|
StringRef ValueText = II->getName();
|
|
|
|
StringRef TrimmedValue = ValueText;
|
|
|
|
if (!ValueText.startswith("__")) {
|
|
|
|
if (ValueText.startswith("_"))
|
|
|
|
TrimmedValue = TrimmedValue.drop_front(1);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
TrimmedValue = TrimmedValue.drop_front(2);
|
|
|
|
if (TrimmedValue.endswith("__"))
|
|
|
|
TrimmedValue = TrimmedValue.drop_back(2);
|
|
|
|
}
|
|
|
|
return TrimmedValue.equals(MacroText);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// #define inline
|
2015-12-28 23:30:42 +08:00
|
|
|
return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
|
|
|
|
tok::kw_const) &&
|
|
|
|
MI->getNumTokens() == 0;
|
2014-12-18 19:14:21 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleDefineDirective - Implements \#define. This consumes the entire macro
|
2008-03-09 09:54:53 +08:00
|
|
|
/// line then lets the caller lex the next real token.
|
2013-06-13 05:20:57 +08:00
|
|
|
void Preprocessor::HandleDefineDirective(Token &DefineTok,
|
|
|
|
bool ImmediatelyAfterHeaderGuard) {
|
2008-03-09 09:54:53 +08:00
|
|
|
++NumDefined;
|
|
|
|
|
|
|
|
Token MacroNameTok;
|
2014-12-18 19:14:21 +08:00
|
|
|
bool MacroShadowsKeyword;
|
|
|
|
ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (MacroNameTok.is(tok::eod))
|
2008-03-09 09:54:53 +08:00
|
|
|
return;
|
|
|
|
|
2009-04-21 12:46:33 +08:00
|
|
|
Token LastTok = MacroNameTok;
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If we are supposed to keep comments in #defines, reenable comment saving
|
|
|
|
// mode.
|
2008-11-18 08:43:07 +08:00
|
|
|
if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Create the new macro.
|
2008-12-16 03:56:42 +08:00
|
|
|
MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
Token Tok;
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is a function-like macro definition, parse the argument list,
|
|
|
|
// marking each of the identifiers as being used as macro arguments. Also,
|
|
|
|
// check other constraints on the first token of the macro body.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.is(tok::eod)) {
|
2013-06-13 05:20:57 +08:00
|
|
|
if (ImmediatelyAfterHeaderGuard) {
|
|
|
|
// Save this macro information since it may part of a header guard.
|
|
|
|
CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
|
|
|
|
MacroNameTok.getLocation());
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
// If there is no body to this macro, we have no special handling here.
|
2009-04-18 10:23:25 +08:00
|
|
|
} else if (Tok.hasLeadingSpace()) {
|
|
|
|
// This is a normal token with leading space. Clear the leading space
|
|
|
|
// marker on the first token to get proper expansion.
|
|
|
|
Tok.clearFlag(Token::LeadingSpace);
|
|
|
|
} else if (Tok.is(tok::l_paren)) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// This is a function-like macro definition. Read the argument list.
|
|
|
|
MI->setIsFunctionLike();
|
2012-04-01 04:17:27 +08:00
|
|
|
if (ReadMacroDefinitionArgList(MI, LastTok)) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// Throw away the rest of the line.
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->ParsingPreprocessorDirective)
|
2008-03-09 09:54:53 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-20 02:26:34 +08:00
|
|
|
// If this is a definition of a variadic C99 function-like macro, not using
|
|
|
|
// the GNU named varargs extension, enabled __VA_ARGS__.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-20 02:26:34 +08:00
|
|
|
// "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
|
|
|
|
// This gets unpoisoned where it is allowed.
|
|
|
|
assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
|
|
|
|
if (MI->isC99Varargs())
|
|
|
|
Ident__VA_ARGS__->setIsPoisoned(false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the first token after the arg list for down below.
|
|
|
|
LexUnexpandedToken(Tok);
|
2013-01-02 19:42:31 +08:00
|
|
|
} else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// C99 requires whitespace between the macro definition and the body. Emit
|
|
|
|
// a diagnostic for something like "#define X+".
|
2009-04-18 10:23:25 +08:00
|
|
|
Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
|
2008-03-09 09:54:53 +08:00
|
|
|
} else {
|
2009-04-18 10:23:25 +08:00
|
|
|
// C90 6.8 TC1 says: "In the definition of an object-like macro, if the
|
|
|
|
// first character of a replacement list is not a character required by
|
|
|
|
// subclause 5.2.1, then there shall be white-space separation between the
|
|
|
|
// identifier and the replacement list.". 5.2.1 lists this set:
|
|
|
|
// "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
|
|
|
|
// is irrelevant here.
|
|
|
|
bool isInvalid = false;
|
|
|
|
if (Tok.is(tok::at)) // @ is not in the list above.
|
|
|
|
isInvalid = true;
|
|
|
|
else if (Tok.is(tok::unknown)) {
|
|
|
|
// If we have an unknown token, it is something strange like "`". Since
|
|
|
|
// all of valid characters would have lexed into a single character
|
|
|
|
// token of some sort, we know this is not a valid case.
|
|
|
|
isInvalid = true;
|
|
|
|
}
|
|
|
|
if (isInvalid)
|
|
|
|
Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
|
|
|
|
else
|
|
|
|
Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-04-21 12:46:33 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
if (!Tok.is(tok::eod))
|
2009-04-21 12:46:33 +08:00
|
|
|
LastTok = Tok;
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Read the rest of the macro body.
|
|
|
|
if (MI->isObjectLike()) {
|
|
|
|
// Object-like macros are very simple, just read their body.
|
2011-02-28 10:37:51 +08:00
|
|
|
while (Tok.isNot(tok::eod)) {
|
2009-04-21 12:46:33 +08:00
|
|
|
LastTok = Tok;
|
2008-03-09 09:54:53 +08:00
|
|
|
MI->AddTokenToBody(Tok);
|
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-26 01:16:10 +08:00
|
|
|
// Otherwise, read the body of a function-like macro. While we are at it,
|
|
|
|
// check C99 6.10.3.2p1: ensure that # operators are followed by macro
|
|
|
|
// parameters in function-like macro expansions.
|
2011-02-28 10:37:51 +08:00
|
|
|
while (Tok.isNot(tok::eod)) {
|
2009-04-21 12:46:33 +08:00
|
|
|
LastTok = Tok;
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2016-04-02 03:02:20 +08:00
|
|
|
if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
|
2009-05-26 01:16:10 +08:00
|
|
|
MI->AddTokenToBody(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-07-09 09:00:29 +08:00
|
|
|
// If we're in -traditional mode, then we should ignore stringification
|
|
|
|
// and token pasting. Mark the tokens as unknown so as not to confuse
|
|
|
|
// things.
|
|
|
|
if (getLangOpts().TraditionalCPP) {
|
|
|
|
Tok.setKind(tok::unknown);
|
|
|
|
MI->AddTokenToBody(Tok);
|
|
|
|
|
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-14 10:18:46 +08:00
|
|
|
if (Tok.is(tok::hashhash)) {
|
|
|
|
// If we see token pasting, check if it looks like the gcc comma
|
|
|
|
// pasting extension. We'll use this information to suppress
|
|
|
|
// diagnostics later on.
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2012-11-14 10:18:46 +08:00
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
|
|
|
|
if (Tok.is(tok::eod)) {
|
|
|
|
MI->AddTokenToBody(LastTok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumTokens = MI->getNumTokens();
|
|
|
|
if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
|
|
|
|
MI->getReplacementToken(NumTokens-1).is(tok::comma))
|
|
|
|
MI->setHasCommaPasting();
|
|
|
|
|
2013-11-05 17:30:17 +08:00
|
|
|
// Things look ok, add the '##' token to the macro.
|
2012-11-14 10:18:46 +08:00
|
|
|
MI->AddTokenToBody(LastTok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-26 01:16:10 +08:00
|
|
|
// Check for a valid macro arg identifier.
|
2014-05-18 07:10:59 +08:00
|
|
|
if (Tok.getIdentifierInfo() == nullptr ||
|
2009-05-26 01:16:10 +08:00
|
|
|
MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
|
|
|
|
|
|
|
|
// If this is assembler-with-cpp mode, we accept random gibberish after
|
|
|
|
// the '#' because '#' is often a comment character. However, change
|
|
|
|
// the kind of the token to tok::unknown so that the preprocessor isn't
|
|
|
|
// confused.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
|
2009-05-26 01:16:10 +08:00
|
|
|
LastTok.setKind(tok::unknown);
|
2013-06-19 05:33:38 +08:00
|
|
|
MI->AddTokenToBody(LastTok);
|
|
|
|
continue;
|
2009-05-26 01:16:10 +08:00
|
|
|
} else {
|
2016-04-02 03:02:20 +08:00
|
|
|
Diag(Tok, diag::err_pp_stringize_not_parameter)
|
|
|
|
<< LastTok.is(tok::hashat);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-26 01:16:10 +08:00
|
|
|
// Disable __VA_ARGS__ again.
|
|
|
|
Ident__VA_ARGS__->setIsPoisoned(true);
|
|
|
|
return;
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-26 01:16:10 +08:00
|
|
|
// Things look ok, add the '#' and param name tokens to the macro.
|
|
|
|
MI->AddTokenToBody(LastTok);
|
2008-03-09 09:54:53 +08:00
|
|
|
MI->AddTokenToBody(Tok);
|
2009-05-26 01:16:10 +08:00
|
|
|
LastTok = Tok;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Get the next token of the macro.
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-12-18 19:14:21 +08:00
|
|
|
if (MacroShadowsKeyword &&
|
|
|
|
!isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
|
|
|
|
Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Disable __VA_ARGS__ again.
|
|
|
|
Ident__VA_ARGS__->setIsPoisoned(true);
|
|
|
|
|
2011-04-15 13:22:18 +08:00
|
|
|
// Check that there is no paste (##) operator at the beginning or end of the
|
2008-03-09 09:54:53 +08:00
|
|
|
// replacement list.
|
|
|
|
unsigned NumTokens = MI->getNumTokens();
|
|
|
|
if (NumTokens != 0) {
|
|
|
|
if (MI->getReplacementToken(0).is(tok::hashhash)) {
|
|
|
|
Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
|
|
|
|
Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 12:46:33 +08:00
|
|
|
MI->setDefinitionEndLoc(LastTok.getLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Finally, if this identifier already had a macro defined for it, verify that
|
Keep history of macro definitions and #undefs
Summary:
Summary: Keep history of macro definitions and #undefs with corresponding source locations, so that we can later find out all macros active in a specified source location. We don't save the history in PCH (no need currently). Memory overhead is about sizeof(void*)*3*<number of macro definitions and #undefs>+<in-memory size of all #undef'd macros>
I've run a test on a file composed of 109 .h files from boost 1.49 on x86-64 linux.
Stats before this patch:
*** Preprocessor Stats:
73222 directives found:
19171 #define.
4345 #undef.
#include/#include_next/#import:
5233 source files entered.
27 max include stack depth
19210 #if/#ifndef/#ifdef.
2384 #else/#elif.
6891 #endif.
408 #pragma.
14466 #if/#ifndef#ifdef regions skipped
80023/451669/1270 obj/fn/builtin macros expanded, 85724 on the fast path.
127145 token paste (##) operations performed, 11008 on the fast path.
Preprocessor Memory: 5874615B total
BumpPtr: 4399104
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
Stats with this patch:
...
Preprocessor Memory: 7541687B total
BumpPtr: 6066176
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
In my test increase in memory usage is about 1.7Mb, which is ~28% of initial preprocessor's memory usage and about 0.8% of clang's total VMM allocation.
As for CPU overhead, it should only be noticeable when iterating over all macros, and should mostly consist of couple extra dereferences and one comparison per macro + skipping of #undef'd macros. It's less trivial to measure, though, as the preprocessor consumes a very small fraction of compilation time.
Reviewers: doug.gregor, klimek, rsmith, djasper
Reviewed By: doug.gregor
CC: cfe-commits, chandlerc
Differential Revision: http://llvm-reviews.chandlerc.com/D28
llvm-svn: 162810
2012-08-29 08:20:03 +08:00
|
|
|
// the macro bodies are identical, and issue diagnostics if they are not.
|
2013-02-20 08:54:57 +08:00
|
|
|
if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
|
2015-12-11 07:31:01 +08:00
|
|
|
// In Objective-C, ignore attempts to directly redefine the builtin
|
|
|
|
// definitions of the ownership qualifiers. It's still possible to
|
|
|
|
// #undef them.
|
|
|
|
auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
|
|
|
|
return II->isStr("__strong") ||
|
|
|
|
II->isStr("__weak") ||
|
|
|
|
II->isStr("__unsafe_unretained") ||
|
|
|
|
II->isStr("__autoreleasing");
|
|
|
|
};
|
|
|
|
if (getLangOpts().ObjC1 &&
|
|
|
|
SourceMgr.getFileID(OtherMI->getDefinitionLoc())
|
|
|
|
== getPredefinesFileID() &&
|
|
|
|
isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
|
|
|
|
// Warn if it changes the tokens.
|
|
|
|
if ((!getDiagnostics().getSuppressSystemWarnings() ||
|
|
|
|
!SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
|
|
|
|
!MI->isIdenticalTo(*OtherMI, *this,
|
|
|
|
/*Syntactic=*/LangOpts.MicrosoftExt)) {
|
|
|
|
Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
|
|
|
|
}
|
|
|
|
assert(!OtherMI->isWarnIfUnused());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-17 03:50:11 +08:00
|
|
|
// It is very common for system headers to have tons of macro redefinitions
|
|
|
|
// and for warnings to be disabled in system headers. If this is the case,
|
|
|
|
// then don't bother calling MacroInfo::isIdenticalTo.
|
2009-03-14 05:17:23 +08:00
|
|
|
if (!getDiagnostics().getSuppressSystemWarnings() ||
|
2009-01-17 03:50:11 +08:00
|
|
|
!SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
|
2011-01-19 03:50:15 +08:00
|
|
|
if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
|
2009-01-17 03:50:11 +08:00
|
|
|
Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2016-06-14 05:55:33 +08:00
|
|
|
// Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
|
2013-03-06 08:46:00 +08:00
|
|
|
// C++ [cpp.predefined]p4, but allow it as an extension.
|
|
|
|
if (OtherMI->isBuiltinMacro())
|
|
|
|
Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
|
2010-08-17 23:55:45 +08:00
|
|
|
// Macros must be identical. This means all tokens and whitespace
|
2013-04-04 01:39:30 +08:00
|
|
|
// separation must be the same. C99 6.10.3p2.
|
2013-03-06 08:46:00 +08:00
|
|
|
else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
|
2013-04-04 01:39:30 +08:00
|
|
|
!MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
|
2009-01-17 03:50:11 +08:00
|
|
|
Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
|
|
|
|
<< MacroNameTok.getIdentifierInfo();
|
|
|
|
Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
|
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2011-01-19 03:50:15 +08:00
|
|
|
if (OtherMI->isWarnIfUnused())
|
|
|
|
WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-27 01:17:01 +08:00
|
|
|
DefMacroDirective *MD =
|
|
|
|
appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-16 02:44:22 +08:00
|
|
|
assert(!MI->isUsed());
|
|
|
|
// If we need warning for not using the macro, add its location in the
|
|
|
|
// warn-because-unused-macro set. If it gets used it will be removed from set.
|
2013-08-22 08:27:10 +08:00
|
|
|
if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
|
2014-06-16 07:30:39 +08:00
|
|
|
!Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
|
2010-12-16 02:44:22 +08:00
|
|
|
MI->setIsWarnIfUnused(true);
|
|
|
|
WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
|
|
|
|
}
|
|
|
|
|
2009-04-12 09:39:54 +08:00
|
|
|
// If the callbacks want to know, tell them about the macro definition.
|
|
|
|
if (Callbacks)
|
2013-02-24 08:05:14 +08:00
|
|
|
Callbacks->MacroDefined(MacroNameTok, MD);
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleUndefDirective - Implements \#undef.
|
2008-03-09 09:54:53 +08:00
|
|
|
///
|
|
|
|
void Preprocessor::HandleUndefDirective(Token &UndefTok) {
|
|
|
|
++NumUndefined;
|
|
|
|
|
|
|
|
Token MacroNameTok;
|
2014-10-25 01:31:32 +08:00
|
|
|
ReadMacroName(MacroNameTok, MU_Undef);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (MacroNameTok.is(tok::eod))
|
2008-03-09 09:54:53 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Check to see if this is the last token on the #undef line.
|
2009-04-14 13:07:49 +08:00
|
|
|
CheckEndOfDirective("undef");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-04-30 07:20:19 +08:00
|
|
|
// Okay, we have a valid identifier to undef.
|
|
|
|
auto *II = MacroNameTok.getIdentifierInfo();
|
2015-05-04 11:15:40 +08:00
|
|
|
auto MD = getMacroDefinition(II);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-17 00:52:44 +08:00
|
|
|
// If the callbacks want to know, tell them about the macro #undef.
|
|
|
|
// Note: no matter if the macro was defined or not.
|
2015-05-04 11:15:40 +08:00
|
|
|
if (Callbacks)
|
2013-02-24 08:05:14 +08:00
|
|
|
Callbacks->MacroUndefined(MacroNameTok, MD);
|
2013-01-17 00:52:44 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If the macro is not defined, this is a noop undef, just return.
|
2015-05-04 11:15:40 +08:00
|
|
|
const MacroInfo *MI = MD.getMacroInfo();
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!MI)
|
|
|
|
return;
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2011-07-12 04:39:47 +08:00
|
|
|
if (!MI->isUsed() && MI->isWarnIfUnused())
|
2008-03-09 09:54:53 +08:00
|
|
|
Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
|
2009-04-21 11:42:09 +08:00
|
|
|
|
2010-12-16 02:44:22 +08:00
|
|
|
if (MI->isWarnIfUnused())
|
|
|
|
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
|
|
|
|
|
2013-03-27 01:17:01 +08:00
|
|
|
appendMacroDirective(MacroNameTok.getIdentifierInfo(),
|
|
|
|
AllocateUndefMacroDirective(MacroNameTok.getLocation()));
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Conditional Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
|
|
|
|
/// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
|
|
|
|
/// true if any tokens have been returned or pp-directives activated before this
|
|
|
|
/// \#ifndef has been lexed.
|
2008-03-09 09:54:53 +08:00
|
|
|
///
|
|
|
|
void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
|
|
|
|
bool ReadAnyTokensBeforeDirective) {
|
|
|
|
++NumIf;
|
|
|
|
Token DirectiveTok = Result;
|
|
|
|
|
|
|
|
Token MacroNameTok;
|
|
|
|
ReadMacroName(MacroNameTok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Error reading macro name? If so, diagnostic already issued.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (MacroNameTok.is(tok::eod)) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// Skip code until we get to #endif. This helps with recovery by not
|
|
|
|
// emitting an error when the #endif is reached.
|
|
|
|
SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
|
|
|
|
/*Foundnonskip*/false, /*FoundElse*/false);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Check to see if this is the last token on the #if[n]def line.
|
2009-04-14 13:07:49 +08:00
|
|
|
CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2010-02-12 16:03:27 +08:00
|
|
|
IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
|
2015-05-04 11:15:40 +08:00
|
|
|
auto MD = getMacroDefinition(MII);
|
|
|
|
MacroInfo *MI = MD.getMacroInfo();
|
2010-03-13 18:17:05 +08:00
|
|
|
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->getConditionalStackDepth() == 0) {
|
2010-02-12 16:03:27 +08:00
|
|
|
// If the start of a top-level #ifdef and if the macro is not defined,
|
|
|
|
// inform MIOpt that this might be the start of a proper include guard.
|
|
|
|
// Otherwise it is some other form of unknown conditional which we can't
|
|
|
|
// handle.
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!ReadAnyTokensBeforeDirective && !MI) {
|
2008-03-09 09:54:53 +08:00
|
|
|
assert(isIfndef && "#ifdef shouldn't reach here");
|
2013-06-13 05:20:57 +08:00
|
|
|
CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
|
2008-03-09 09:54:53 +08:00
|
|
|
} else
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->MIOpt.EnterTopLevelConditional();
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a macro, process it.
|
|
|
|
if (MI) // Mark it used.
|
2010-12-16 02:44:22 +08:00
|
|
|
markMacroAsUsed(MI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-03-05 13:48:09 +08:00
|
|
|
if (Callbacks) {
|
|
|
|
if (isIfndef)
|
2013-02-24 08:05:14 +08:00
|
|
|
Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
|
2012-03-05 13:48:09 +08:00
|
|
|
else
|
2013-02-24 08:05:14 +08:00
|
|
|
Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
|
2012-03-05 13:48:09 +08:00
|
|
|
}
|
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Should we include the stuff contained by this directive?
|
|
|
|
if (!MI == isIfndef) {
|
|
|
|
// Yes, remember that we are inside a conditional, then lex the next token.
|
2009-12-14 12:54:40 +08:00
|
|
|
CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
|
|
|
|
/*wasskip*/false, /*foundnonskip*/true,
|
|
|
|
/*foundelse*/false);
|
2008-03-09 09:54:53 +08:00
|
|
|
} else {
|
2010-11-06 09:19:03 +08:00
|
|
|
// No, skip the contents of this block.
|
2008-03-09 09:54:53 +08:00
|
|
|
SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
|
2009-09-09 23:08:12 +08:00
|
|
|
/*Foundnonskip*/false,
|
2008-03-09 09:54:53 +08:00
|
|
|
/*FoundElse*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleIfDirective - Implements the \#if directive.
|
2008-03-09 09:54:53 +08:00
|
|
|
///
|
|
|
|
void Preprocessor::HandleIfDirective(Token &IfToken,
|
|
|
|
bool ReadAnyTokensBeforeDirective) {
|
|
|
|
++NumIf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-06 09:19:03 +08:00
|
|
|
// Parse and evaluate the conditional expression.
|
2014-05-18 07:10:59 +08:00
|
|
|
IdentifierInfo *IfNDefMacro = nullptr;
|
2010-11-06 09:19:03 +08:00
|
|
|
const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
|
|
|
|
const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
|
|
|
|
const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
|
2008-06-02 02:31:24 +08:00
|
|
|
|
|
|
|
// If this condition is equivalent to #ifndef X, and if this is the first
|
|
|
|
// directive seen, handle it for the multiple-include optimization.
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->getConditionalStackDepth() == 0) {
|
2010-02-12 16:03:27 +08:00
|
|
|
if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
|
2013-06-16 13:05:39 +08:00
|
|
|
// FIXME: Pass in the location of the macro name, not the 'if' token.
|
|
|
|
CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
|
2008-06-02 02:31:24 +08:00
|
|
|
else
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->MIOpt.EnterTopLevelConditional();
|
2008-06-02 02:31:24 +08:00
|
|
|
}
|
|
|
|
|
2012-03-05 13:48:09 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->If(IfToken.getLocation(),
|
2013-07-18 08:00:36 +08:00
|
|
|
SourceRange(ConditionalBegin, ConditionalEnd),
|
2013-12-07 16:41:15 +08:00
|
|
|
(ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
|
2012-03-05 13:48:09 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Should we include the stuff contained by this directive?
|
|
|
|
if (ConditionalTrue) {
|
|
|
|
// Yes, remember that we are inside a conditional, then lex the next token.
|
2008-11-18 08:34:22 +08:00
|
|
|
CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
|
2008-03-09 09:54:53 +08:00
|
|
|
/*foundnonskip*/true, /*foundelse*/false);
|
|
|
|
} else {
|
2010-11-06 09:19:03 +08:00
|
|
|
// No, skip the contents of this block.
|
2009-09-09 23:08:12 +08:00
|
|
|
SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
|
2008-03-09 09:54:53 +08:00
|
|
|
/*FoundElse*/false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleEndifDirective - Implements the \#endif directive.
|
2008-03-09 09:54:53 +08:00
|
|
|
///
|
|
|
|
void Preprocessor::HandleEndifDirective(Token &EndifToken) {
|
|
|
|
++NumEndif;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// Check that this is the whole directive.
|
2009-04-14 13:07:49 +08:00
|
|
|
CheckEndOfDirective("endif");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
PPConditionalInfo CondInfo;
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->popConditionalLevel(CondInfo)) {
|
2008-03-09 09:54:53 +08:00
|
|
|
// No conditionals on the stack: this is an #endif without an #if.
|
2008-11-18 15:59:24 +08:00
|
|
|
Diag(EndifToken, diag::err_pp_endif_without_if);
|
|
|
|
return;
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this the end of a top-level #endif, inform MIOpt.
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->getConditionalStackDepth() == 0)
|
|
|
|
CurPPLexer->MIOpt.ExitTopLevelConditional();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-18 08:34:22 +08:00
|
|
|
assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
|
2008-03-09 09:54:53 +08:00
|
|
|
"This code should only be reachable in the non-skipping case!");
|
|
|
|
|
2010-11-06 09:19:03 +08:00
|
|
|
if (Callbacks)
|
2012-03-05 13:48:09 +08:00
|
|
|
Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
|
2010-11-06 09:19:03 +08:00
|
|
|
}
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleElseDirective - Implements the \#else directive.
|
2010-11-06 09:19:03 +08:00
|
|
|
///
|
2008-03-09 09:54:53 +08:00
|
|
|
void Preprocessor::HandleElseDirective(Token &Result) {
|
|
|
|
++NumElse;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// #else directive in a non-skipping conditional... start skipping.
|
2009-04-14 13:07:49 +08:00
|
|
|
CheckEndOfDirective("else");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
PPConditionalInfo CI;
|
2008-11-18 15:59:24 +08:00
|
|
|
if (CurPPLexer->popConditionalLevel(CI)) {
|
|
|
|
Diag(Result, diag::pp_err_else_without_if);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is a top-level #else, inform the MIOpt.
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->getConditionalStackDepth() == 0)
|
|
|
|
CurPPLexer->MIOpt.EnterTopLevelConditional();
|
2008-03-09 09:54:53 +08:00
|
|
|
|
|
|
|
// If this is a #else with a #else before it, report the error.
|
|
|
|
if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-03-05 13:48:09 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->Else(Result.getLocation(), CI.IfLoc);
|
|
|
|
|
2010-11-06 09:19:03 +08:00
|
|
|
// Finally, skip the rest of the contents of this block.
|
|
|
|
SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
|
2011-09-28 01:32:05 +08:00
|
|
|
/*FoundElse*/true, Result.getLocation());
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-22 13:46:07 +08:00
|
|
|
/// HandleElifDirective - Implements the \#elif directive.
|
2010-11-06 09:19:03 +08:00
|
|
|
///
|
2008-03-09 09:54:53 +08:00
|
|
|
void Preprocessor::HandleElifDirective(Token &ElifToken) {
|
|
|
|
++NumElse;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// #elif directive in a non-skipping conditional... start skipping.
|
|
|
|
// We don't care what the condition is, because we will always skip it (since
|
|
|
|
// the block immediately before it was included).
|
2010-11-06 09:19:03 +08:00
|
|
|
const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
|
2008-03-09 09:54:53 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2010-11-06 09:19:03 +08:00
|
|
|
const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
|
2008-03-09 09:54:53 +08:00
|
|
|
|
|
|
|
PPConditionalInfo CI;
|
2008-11-18 15:59:24 +08:00
|
|
|
if (CurPPLexer->popConditionalLevel(CI)) {
|
|
|
|
Diag(ElifToken, diag::pp_err_elif_without_if);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is a top-level #elif, inform the MIOpt.
|
2008-11-18 08:34:22 +08:00
|
|
|
if (CurPPLexer->getConditionalStackDepth() == 0)
|
|
|
|
CurPPLexer->MIOpt.EnterTopLevelConditional();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 09:54:53 +08:00
|
|
|
// If this is a #elif with a #else before it, report the error.
|
|
|
|
if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
|
2016-06-14 05:55:33 +08:00
|
|
|
|
2012-03-05 13:48:09 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->Elif(ElifToken.getLocation(),
|
2013-07-18 08:00:36 +08:00
|
|
|
SourceRange(ConditionalBegin, ConditionalEnd),
|
2013-12-07 16:41:15 +08:00
|
|
|
PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
|
2008-03-09 09:54:53 +08:00
|
|
|
|
2010-11-06 09:19:03 +08:00
|
|
|
// Finally, skip the rest of the contents of this block.
|
|
|
|
SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
|
2011-09-28 01:32:05 +08:00
|
|
|
/*FoundElse*/CI.FoundElse,
|
|
|
|
ElifToken.getLocation());
|
2008-03-09 09:54:53 +08:00
|
|
|
}
|