#include "clang/Lex/MacroInfo.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <utility>
using namespace clang;
namespace {
template <int> class MacroInfoSizeChecker {
public:
constexpr static bool AsExpected = true;
};
template <> class MacroInfoSizeChecker<8> {
public:
constexpr static bool AsExpected =
sizeof(MacroInfo) == (32 + sizeof(SourceLocation) * 2);
};
static_assert(MacroInfoSizeChecker<sizeof(void *)>::AsExpected,
"Unexpected size of MacroInfo");
}
MacroInfo::MacroInfo(SourceLocation DefLoc)
: Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
HasCommaPasting(false), IsDisabled(false), IsUsed(false),
IsAllowRedefinitionsWithoutWarning(false), IsWarnIfUnused(false),
UsedForHeaderGuard(false) {}
unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const {
assert(!IsDefinitionLengthCached);
IsDefinitionLengthCached = true;
ArrayRef<Token> ReplacementTokens = tokens();
if (ReplacementTokens.empty())
return (DefinitionLength = 0);
const Token &firstToken = ReplacementTokens.front();
const Token &lastToken = ReplacementTokens.back();
SourceLocation macroStart = firstToken.getLocation();
SourceLocation macroEnd = lastToken.getLocation();
assert(macroStart.isValid() && macroEnd.isValid());
assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
"Macro defined in macro?");
assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
"Macro defined in macro?");
std::pair<FileID, unsigned>
startInfo = SM.getDecomposedExpansionLoc(macroStart);
std::pair<FileID, unsigned>
endInfo = SM.getDecomposedExpansionLoc(macroEnd);
assert(startInfo.first == endInfo.first &&
"Macro definition spanning multiple FileIDs ?");
assert(startInfo.second <= endInfo.second);
DefinitionLength = endInfo.second - startInfo.second;
DefinitionLength += lastToken.getLength();
return DefinitionLength;
}
bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
bool Syntactically) const {
bool Lexically = !Syntactically;
if (getNumTokens() != Other.getNumTokens() ||
getNumParams() != Other.getNumParams() ||
isFunctionLike() != Other.isFunctionLike() ||
isC99Varargs() != Other.isC99Varargs() ||
isGNUVarargs() != Other.isGNUVarargs())
return false;
if (Lexically) {
for (param_iterator I = param_begin(), OI = Other.param_begin(),
E = param_end();
I != E; ++I, ++OI)
if (*I != *OI) return false;
}
for (unsigned i = 0; i != NumReplacementTokens; ++i) {
const Token &A = ReplacementTokens[i];
const Token &B = Other.ReplacementTokens[i];
if (A.getKind() != B.getKind())
return false;
if (i != 0 &&
(A.isAtStartOfLine() != B.isAtStartOfLine() ||
A.hasLeadingSpace() != B.hasLeadingSpace()))
return false;
if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
if (A.getIdentifierInfo() == B.getIdentifierInfo())
continue;
if (Lexically)
return false;
int AArgNum = getParameterNum(A.getIdentifierInfo());
if (AArgNum == -1)
return false;
if (AArgNum != Other.getParameterNum(B.getIdentifierInfo()))
return false;
continue;
}
if (PP.getSpelling(A) != PP.getSpelling(B))
return false;
}
return true;
}
LLVM_DUMP_METHOD void MacroInfo::dump() const {
llvm::raw_ostream &Out = llvm::errs();
Out << "MacroInfo " << this;
if (IsBuiltinMacro) Out << " builtin";
if (IsDisabled) Out << " disabled";
if (IsUsed) Out << " used";
if (IsAllowRedefinitionsWithoutWarning)
Out << " allow_redefinitions_without_warning";
if (IsWarnIfUnused) Out << " warn_if_unused";
if (UsedForHeaderGuard) Out << " header_guard";
Out << "\n #define <macro>";
if (IsFunctionLike) {
Out << "(";
for (unsigned I = 0; I != NumParameters; ++I) {
if (I) Out << ", ";
Out << ParameterList[I]->getName();
}
if (IsC99Varargs || IsGNUVarargs) {
if (NumParameters && IsC99Varargs) Out << ", ";
Out << "...";
}
Out << ")";
}
bool First = true;
for (const Token &Tok : tokens()) {
if (First || Tok.hasLeadingSpace())
Out << " ";
First = false;
if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
Out << Punc;
else if (Tok.isLiteral() && Tok.getLiteralData())
Out << StringRef(Tok.getLiteralData(), Tok.getLength());
else if (auto *II = Tok.getIdentifierInfo())
Out << II->getName();
else
Out << Tok.getName();
}
}
MacroDirective::DefInfo MacroDirective::getDefinition() {
MacroDirective *MD = this;
SourceLocation UndefLoc;
Optional<bool> isPublic;
for (; MD; MD = MD->getPrevious()) {
if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
return DefInfo(DefMD, UndefLoc, !isPublic || *isPublic);
if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
UndefLoc = UndefMD->getLocation();
continue;
}
VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
if (!isPublic)
isPublic = VisMD->isPublic();
}
return DefInfo(nullptr, UndefLoc, !isPublic || isPublic.value());
}
const MacroDirective::DefInfo
MacroDirective::findDirectiveAtLoc(SourceLocation L,
const SourceManager &SM) const {
assert(L.isValid() && "SourceLocation is invalid.");
for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
if (Def.getLocation().isInvalid() || SM.isBeforeInTranslationUnit(Def.getLocation(), L))
return (!Def.isUndefined() ||
SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
? Def : DefInfo();
}
return DefInfo();
}
LLVM_DUMP_METHOD void MacroDirective::dump() const {
llvm::raw_ostream &Out = llvm::errs();
switch (getKind()) {
case MD_Define: Out << "DefMacroDirective"; break;
case MD_Undefine: Out << "UndefMacroDirective"; break;
case MD_Visibility: Out << "VisibilityMacroDirective"; break;
}
Out << " " << this;
if (auto *Prev = getPrevious())
Out << " prev " << Prev;
if (IsFromPCH) Out << " from_pch";
if (isa<VisibilityMacroDirective>(this))
Out << (IsPublic ? " public" : " private");
if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
if (auto *Info = DMD->getInfo()) {
Out << "\n ";
Info->dump();
}
}
Out << "\n";
}
ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
IdentifierInfo *II, MacroInfo *Macro,
ArrayRef<ModuleMacro *> Overrides) {
void *Mem = PP.getPreprocessorAllocator().Allocate(
sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
alignof(ModuleMacro));
return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);
}