[modules] Stop trying to fake up a linear MacroDirective history.

Modules builds fundamentally have a non-linear macro history. In the interest
of better source fidelity, represent the macro definition information
faithfully: we have a linear macro directive history within each module, and at
any point we have a unique "latest" local macro directive and a collection of
visible imported directives. This also removes the attendent complexity of
attempting to create a correct MacroDirective history (which we got wrong
in the general case).

No functionality change intended.

llvm-svn: 236176
This commit is contained in:
Richard Smith 2015-04-29 23:20:19 +00:00
parent bf0a42ac09
commit 20e883e59b
31 changed files with 309 additions and 493 deletions

View File

@ -216,6 +216,9 @@ public:
/// of that name in objective-c. /// of that name in objective-c.
StringRef GetNSIntegralKind(QualType T) const; StringRef GetNSIntegralKind(QualType T) const;
/// \brief Returns \c true if \p Id is currently defined as a macro.
bool isMacroDefined(StringRef Id) const;
private: private:
bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const; bool isObjCTypedef(QualType T, StringRef name, IdentifierInfo *&II) const;
bool isObjCEnumerator(const Expr *E, bool isObjCEnumerator(const Expr *E,

View File

@ -124,6 +124,7 @@ public:
} }
/// \brief Return true if this identifier is \#defined to some other value. /// \brief Return true if this identifier is \#defined to some other value.
/// \note The current definition may be in a module and not currently visible.
bool hasMacroDefinition() const { bool hasMacroDefinition() const {
return HasMacro; return HasMacro;
} }

View File

@ -32,6 +32,7 @@ class FileEntry;
class FileManager; class FileManager;
class HeaderSearchOptions; class HeaderSearchOptions;
class IdentifierInfo; class IdentifierInfo;
class Preprocessor;
/// \brief The preprocessor keeps track of this information for each /// \brief The preprocessor keeps track of this information for each
/// file that is \#included. /// file that is \#included.
@ -419,8 +420,8 @@ public:
/// ///
/// \return false if \#including the file will have no effect or true /// \return false if \#including the file will have no effect or true
/// if we should include it. /// if we should include it.
bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); bool ShouldEnterIncludeFile(Preprocessor &PP, const FileEntry *File,
bool isImport);
/// \brief Return whether the specified file is a normal header, /// \brief Return whether the specified file is a normal header,
/// a system header, or a C++ friendly system header. /// a system header, or a C++ friendly system header.

View File

@ -386,7 +386,8 @@ class Preprocessor : public RefCountedBase<Preprocessor> {
class MacroState { class MacroState {
mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State; mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State;
ModuleMacroInfo *getModuleInfo(Preprocessor &PP, IdentifierInfo *II) const { ModuleMacroInfo *getModuleInfo(Preprocessor &PP,
const IdentifierInfo *II) const {
// FIXME: Find a spare bit on IdentifierInfo and store a // FIXME: Find a spare bit on IdentifierInfo and store a
// HasModuleMacros flag. // HasModuleMacros flag.
if (!II->hasMacroDefinition() || !PP.getLangOpts().Modules || if (!II->hasMacroDefinition() || !PP.getLangOpts().Modules ||
@ -434,12 +435,12 @@ class Preprocessor : public RefCountedBase<Preprocessor> {
State = MD; State = MD;
} }
bool isAmbiguous(Preprocessor &PP, IdentifierInfo *II) const { bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const {
auto *Info = getModuleInfo(PP, II); auto *Info = getModuleInfo(PP, II);
return Info ? Info->IsAmbiguous : false; return Info ? Info->IsAmbiguous : false;
} }
ArrayRef<ModuleMacro *> getActiveModuleMacros(Preprocessor &PP, ArrayRef<ModuleMacro *>
IdentifierInfo *II) const { getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const {
if (auto *Info = getModuleInfo(PP, II)) if (auto *Info = getModuleInfo(PP, II))
return Info->ActiveModuleMacros; return Info->ActiveModuleMacros;
return None; return None;
@ -755,33 +756,119 @@ public:
} }
/// \} /// \}
/// \brief Given an identifier, return its latest MacroDirective if it is /// \brief A description of the current definition of a macro.
/// \#defined or null if it isn't \#define'd. class MacroDefinition {
MacroDirective *getMacroDirective(IdentifierInfo *II) const { llvm::PointerIntPair<DefMacroDirective*, 1, bool> LatestLocalAndAmbiguous;
ArrayRef<ModuleMacro*> ModuleMacros;
public:
MacroDefinition() : LatestLocalAndAmbiguous(), ModuleMacros() {}
MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs,
bool IsAmbiguous)
: LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {}
/// \brief Determine whether there is a definition of this macro.
explicit operator bool() const {
return getLocalDirective() || !ModuleMacros.empty();
}
/// \brief Get the MacroInfo that should be used for this definition.
MacroInfo *getMacroInfo() const {
if (!ModuleMacros.empty())
return ModuleMacros.back()->getMacroInfo();
if (auto *MD = getLocalDirective())
return MD->getMacroInfo();
return nullptr;
}
/// \brief \c true if the definition is ambiguous, \c false otherwise.
bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); }
/// \brief Get the latest non-imported, non-\#undef'd macro definition
/// for this macro.
DefMacroDirective *getLocalDirective() const {
return LatestLocalAndAmbiguous.getPointer();
}
/// \brief Get the active module macros for this macro.
ArrayRef<ModuleMacro *> getModuleMacros() const {
return ModuleMacros;
}
template<typename Fn> void forAllDefinitions(Fn F) const {
if (auto *MD = getLocalDirective())
F(MD->getMacroInfo());
for (auto *MM : getModuleMacros())
F(MM->getMacroInfo());
}
};
bool isMacroDefined(StringRef Id) {
return isMacroDefined(&Identifiers.get(Id));
}
bool isMacroDefined(const IdentifierInfo *II) {
return II->hasMacroDefinition() &&
(!getLangOpts().Modules || (bool)getMacroDefinition(II));
}
MacroDefinition getMacroDefinition(const IdentifierInfo *II) {
if (!II->hasMacroDefinition())
return MacroDefinition();
MacroState &S = Macros[II];
auto *MD = S.getLatest();
while (MD && isa<VisibilityMacroDirective>(MD))
MD = MD->getPrevious();
return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD),
S.getActiveModuleMacros(*this, II),
S.isAmbiguous(*this, II));
}
MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II,
SourceLocation Loc) {
if (!II->hadMacroDefinition())
return MacroDefinition();
MacroState &S = Macros[II];
MacroDirective::DefInfo DI;
if (auto *MD = S.getLatest())
DI = MD->findDirectiveAtLoc(Loc, getSourceManager());
// FIXME: Compute the set of active module macros at the specified location.
return MacroDefinition(DI.getDirective(),
S.getActiveModuleMacros(*this, II),
S.isAmbiguous(*this, II));
}
/// \brief Given an identifier, return its latest non-imported MacroDirective
/// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd.
MacroDirective *getLocalMacroDirective(const IdentifierInfo *II) const {
if (!II->hasMacroDefinition()) if (!II->hasMacroDefinition())
return nullptr; return nullptr;
MacroDirective *MD = getMacroDirectiveHistory(II); auto *MD = getLocalMacroDirectiveHistory(II);
assert(MD->isDefined() && "Macro is undefined!"); if (!MD || MD->getDefinition().isUndefined())
return nullptr;
return MD; return MD;
} }
const MacroInfo *getMacroInfo(IdentifierInfo *II) const { const MacroInfo *getMacroInfo(const IdentifierInfo *II) const {
return const_cast<Preprocessor*>(this)->getMacroInfo(II); return const_cast<Preprocessor*>(this)->getMacroInfo(II);
} }
MacroInfo *getMacroInfo(IdentifierInfo *II) { MacroInfo *getMacroInfo(const IdentifierInfo *II) {
if (MacroDirective *MD = getMacroDirective(II)) if (!II->hasMacroDefinition())
return MD->getMacroInfo(); return nullptr;
if (auto MD = getMacroDefinition(II))
return MD.getMacroInfo();
return nullptr; return nullptr;
} }
/// \brief Given an identifier, return the (probably #undef'd) MacroInfo /// \brief Given an identifier, return the latest non-imported macro
/// representing the most recent macro definition. /// directive for that identifier.
/// ///
/// One can iterate over all previous macro definitions from the most recent /// One can iterate over all previous macro directives from the most recent
/// one. This should only be called for identifiers that hadMacroDefinition(). /// one.
MacroDirective *getMacroDirectiveHistory(const IdentifierInfo *II) const; MacroDirective *getLocalMacroDirectiveHistory(const IdentifierInfo *II) const;
/// \brief Add a directive to the macro directive history for this identifier. /// \brief Add a directive to the macro directive history for this identifier.
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD); void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
@ -1580,7 +1667,7 @@ private:
/// Update the set of active module macros and ambiguity flag for a module /// Update the set of active module macros and ambiguity flag for a module
/// macro name. /// macro name.
void updateModuleMacroInfo(IdentifierInfo *II, ModuleMacroInfo &Info); void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
/// \brief Allocate a new MacroInfo object. /// \brief Allocate a new MacroInfo object.
MacroInfo *AllocateMacroInfo(); MacroInfo *AllocateMacroInfo();
@ -1644,7 +1731,7 @@ private:
/// If an identifier token is read that is to be expanded as a macro, handle /// If an identifier token is read that is to be expanded as a macro, handle
/// it and return the next token as 'Tok'. If we lexed a token, return true; /// it and return the next token as 'Tok'. If we lexed a token, return true;
/// otherwise the caller should lex again. /// otherwise the caller should lex again.
bool HandleMacroExpandedIdentifier(Token &Tok, MacroDirective *MD); bool HandleMacroExpandedIdentifier(Token &Tok, const MacroDefinition &MD);
/// \brief Cache macro expanded tokens for TokenLexers. /// \brief Cache macro expanded tokens for TokenLexers.
// //

View File

@ -576,54 +576,8 @@ private:
/// global submodule ID to produce a local ID. /// global submodule ID to produce a local ID.
GlobalSubmoduleMapType GlobalSubmoduleMap; GlobalSubmoduleMapType GlobalSubmoduleMap;
/// \brief Information on a macro definition or undefinition that is visible
/// at the end of a submodule.
struct ModuleMacroInfo;
/// \brief An entity that has been hidden.
class HiddenName {
public:
enum NameKind {
Declaration,
Macro
} Kind;
private:
union {
Decl *D;
ModuleMacroInfo *MMI;
};
IdentifierInfo *Id;
public:
HiddenName(Decl *D) : Kind(Declaration), D(D), Id() { }
HiddenName(IdentifierInfo *II, ModuleMacroInfo *MMI)
: Kind(Macro), MMI(MMI), Id(II) { }
NameKind getKind() const { return Kind; }
Decl *getDecl() const {
assert(getKind() == Declaration && "Hidden name is not a declaration");
return D;
}
std::pair<IdentifierInfo *, ModuleMacroInfo *> getMacro() const {
assert(getKind() == Macro && "Hidden name is not a macro!");
return std::make_pair(Id, MMI);
}
};
typedef llvm::SmallDenseMap<IdentifierInfo*,
ModuleMacroInfo*> HiddenMacrosMap;
/// \brief A set of hidden declarations. /// \brief A set of hidden declarations.
struct HiddenNames { typedef SmallVector<Decl*, 2> HiddenNames;
SmallVector<Decl*, 2> HiddenDecls;
HiddenMacrosMap HiddenMacros;
};
typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType; typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
/// \brief A mapping from each of the hidden submodules to the deserialized /// \brief A mapping from each of the hidden submodules to the deserialized
@ -1850,20 +1804,6 @@ public:
void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo); void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
void installImportedMacro(IdentifierInfo *II, ModuleMacroInfo &MMI,
Module *Owner);
typedef llvm::TinyPtrVector<DefMacroDirective *> AmbiguousMacros;
llvm::DenseMap<IdentifierInfo*, AmbiguousMacros> AmbiguousMacroDefs;
void removeOverriddenMacros(IdentifierInfo *II, SourceLocation Loc,
AmbiguousMacros &Ambig,
ArrayRef<ModuleMacro *> Overrides);
AmbiguousMacros *removeOverriddenMacros(IdentifierInfo *II,
SourceLocation Loc,
ArrayRef<ModuleMacro *> Overrides);
/// \brief Retrieve the macro with the given ID. /// \brief Retrieve the macro with the given ID.
MacroInfo *getMacro(serialization::MacroID ID); MacroInfo *getMacro(serialization::MacroID ID);

View File

@ -468,7 +468,7 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter,
ASTContext &Context = NS.getASTContext(); ASTContext &Context = NS.getASTContext();
bool LParenAdded = false; bool LParenAdded = false;
std::string PropertyString = "@property "; std::string PropertyString = "@property ";
if (UseNsIosOnlyMacro && Context.Idents.get("NS_NONATOMIC_IOSONLY").hasMacroDefinition()) { if (UseNsIosOnlyMacro && NS.isMacroDefined("NS_NONATOMIC_IOSONLY")) {
PropertyString += "(NS_NONATOMIC_IOSONLY"; PropertyString += "(NS_NONATOMIC_IOSONLY";
LParenAdded = true; LParenAdded = true;
} else if (!Atomic) { } else if (!Atomic) {
@ -1277,7 +1277,7 @@ void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx,
QualType RT = OM->getReturnType(); QualType RT = OM->getReturnType();
if (!TypeIsInnerPointer(RT) || if (!TypeIsInnerPointer(RT) ||
!Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) !NSAPIObj->isMacroDefined("NS_RETURNS_INNER_POINTER"))
return; return;
edit::Commit commit(*Editor); edit::Commit commit(*Editor);
@ -1288,9 +1288,9 @@ void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx,
void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx,
ObjCPropertyDecl *P) { ObjCPropertyDecl *P) {
QualType T = P->getType(); QualType T = P->getType();
if (!TypeIsInnerPointer(T) || if (!TypeIsInnerPointer(T) ||
!Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) !NSAPIObj->isMacroDefined("NS_RETURNS_INNER_POINTER"))
return; return;
edit::Commit commit(*Editor); edit::Commit commit(*Editor);
commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER "); commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER ");
@ -1408,7 +1408,7 @@ static bool AuditedType (QualType AT) {
void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) { void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) {
if (CFFunctionIBCandidates.empty()) if (CFFunctionIBCandidates.empty())
return; return;
if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) { if (!NSAPIObj->isMacroDefined("CF_IMPLICIT_BRIDGING_ENABLED")) {
CFFunctionIBCandidates.clear(); CFFunctionIBCandidates.clear();
FileId = FileID(); FileId = FileID();
return; return;
@ -1483,16 +1483,14 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
RetEffect Ret = CE.getReturnValue(); RetEffect Ret = CE.getReturnValue();
const char *AnnotationString = nullptr; const char *AnnotationString = nullptr;
if (Ret.getObjKind() == RetEffect::CF) { if (Ret.getObjKind() == RetEffect::CF) {
if (Ret.isOwned() && if (Ret.isOwned() && NSAPIObj->isMacroDefined("CF_RETURNS_RETAINED"))
Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition())
AnnotationString = " CF_RETURNS_RETAINED"; AnnotationString = " CF_RETURNS_RETAINED";
else if (Ret.notOwned() && else if (Ret.notOwned() &&
Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) NSAPIObj->isMacroDefined("CF_RETURNS_NOT_RETAINED"))
AnnotationString = " CF_RETURNS_NOT_RETAINED"; AnnotationString = " CF_RETURNS_NOT_RETAINED";
} }
else if (Ret.getObjKind() == RetEffect::ObjC) { else if (Ret.getObjKind() == RetEffect::ObjC) {
if (Ret.isOwned() && if (Ret.isOwned() && NSAPIObj->isMacroDefined("NS_RETURNS_RETAINED"))
Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition())
AnnotationString = " NS_RETURNS_RETAINED"; AnnotationString = " NS_RETURNS_RETAINED";
} }
@ -1509,13 +1507,13 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
const ParmVarDecl *pd = *pi; const ParmVarDecl *pd = *pi;
ArgEffect AE = AEArgs[i]; ArgEffect AE = AEArgs[i];
if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() &&
Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { NSAPIObj->isMacroDefined("CF_CONSUMED")) {
edit::Commit commit(*Editor); edit::Commit commit(*Editor);
commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
Editor->commit(commit); Editor->commit(commit);
} }
else if (AE == DecRefMsg && !pd->hasAttr<NSConsumedAttr>() && else if (AE == DecRefMsg && !pd->hasAttr<NSConsumedAttr>() &&
Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) { NSAPIObj->isMacroDefined("NS_CONSUMED")) {
edit::Commit commit(*Editor); edit::Commit commit(*Editor);
commit.insertBefore(pd->getLocation(), "NS_CONSUMED "); commit.insertBefore(pd->getLocation(), "NS_CONSUMED ");
Editor->commit(commit); Editor->commit(commit);
@ -1600,11 +1598,10 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
RetEffect Ret = CE.getReturnValue(); RetEffect Ret = CE.getReturnValue();
const char *AnnotationString = nullptr; const char *AnnotationString = nullptr;
if (Ret.getObjKind() == RetEffect::CF) { if (Ret.getObjKind() == RetEffect::CF) {
if (Ret.isOwned() && if (Ret.isOwned() && NSAPIObj->isMacroDefined("CF_RETURNS_RETAINED"))
Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition())
AnnotationString = " CF_RETURNS_RETAINED"; AnnotationString = " CF_RETURNS_RETAINED";
else if (Ret.notOwned() && else if (Ret.notOwned() &&
Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) NSAPIObj->isMacroDefined("CF_RETURNS_NOT_RETAINED"))
AnnotationString = " CF_RETURNS_NOT_RETAINED"; AnnotationString = " CF_RETURNS_NOT_RETAINED";
} }
else if (Ret.getObjKind() == RetEffect::ObjC) { else if (Ret.getObjKind() == RetEffect::ObjC) {
@ -1618,8 +1615,7 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
break; break;
default: default:
if (Ret.isOwned() && if (Ret.isOwned() && NSAPIObj->isMacroDefined("NS_RETURNS_RETAINED"))
Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition())
AnnotationString = " NS_RETURNS_RETAINED"; AnnotationString = " NS_RETURNS_RETAINED";
break; break;
} }
@ -1638,7 +1634,7 @@ void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx,
const ParmVarDecl *pd = *pi; const ParmVarDecl *pd = *pi;
ArgEffect AE = AEArgs[i]; ArgEffect AE = AEArgs[i];
if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() &&
Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { NSAPIObj->isMacroDefined("CF_CONSUMED")) {
edit::Commit commit(*Editor); edit::Commit commit(*Editor);
commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
Editor->commit(commit); Editor->commit(commit);
@ -1658,12 +1654,12 @@ void ObjCMigrateASTConsumer::migrateAddMethodAnnotation(
MethodDecl->hasAttr<NSReturnsRetainedAttr>() || MethodDecl->hasAttr<NSReturnsRetainedAttr>() ||
MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() || MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() ||
MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>()); MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>());
if (CE.getReceiver() == DecRefMsg && if (CE.getReceiver() == DecRefMsg &&
!MethodDecl->hasAttr<NSConsumesSelfAttr>() && !MethodDecl->hasAttr<NSConsumesSelfAttr>() &&
MethodDecl->getMethodFamily() != OMF_init && MethodDecl->getMethodFamily() != OMF_init &&
MethodDecl->getMethodFamily() != OMF_release && MethodDecl->getMethodFamily() != OMF_release &&
Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { NSAPIObj->isMacroDefined("NS_CONSUMES_SELF")) {
edit::Commit commit(*Editor); edit::Commit commit(*Editor);
commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF");
Editor->commit(commit); Editor->commit(commit);
@ -1729,7 +1725,7 @@ void ObjCMigrateASTConsumer::inferDesignatedInitializers(
const ObjCInterfaceDecl *IFace = ImplD->getClassInterface(); const ObjCInterfaceDecl *IFace = ImplD->getClassInterface();
if (!IFace || IFace->hasDesignatedInitializers()) if (!IFace || IFace->hasDesignatedInitializers())
return; return;
if (!Ctx.Idents.get("NS_DESIGNATED_INITIALIZER").hasMacroDefinition()) if (!NSAPIObj->isMacroDefined("NS_DESIGNATED_INITIALIZER"))
return; return;
for (const auto *MD : ImplD->instance_methods()) { for (const auto *MD : ImplD->instance_methods()) {

View File

@ -95,7 +95,7 @@ public:
Pass.TA.clearDiagnostic(diag::err_unavailable, Pass.TA.clearDiagnostic(diag::err_unavailable,
diag::err_unavailable_message, diag::err_unavailable_message,
E->getSelectorLoc(0)); E->getSelectorLoc(0));
Pass.TA.replace(E->getSourceRange(), getNilString(Pass.Ctx)); Pass.TA.replace(E->getSourceRange(), getNilString(Pass));
} }
return true; return true;
} }

View File

@ -145,7 +145,7 @@ public:
// when an exception is thrown. // when an exception is thrown.
Pass.TA.replace(RecContainer->getSourceRange(), RecRange); Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
std::string str = " = "; std::string str = " = ";
str += getNilString(Pass.Ctx); str += getNilString(Pass);
Pass.TA.insertAfterToken(RecRange.getEnd(), str); Pass.TA.insertAfterToken(RecRange.getEnd(), str);
return true; return true;
} }

View File

@ -58,7 +58,7 @@ public:
SourceRange ExprRange = ME->getSourceRange(); SourceRange ExprRange = ME->getSourceRange();
Pass.TA.insert(ExprRange.getBegin(), "if (!(self = "); Pass.TA.insert(ExprRange.getBegin(), "if (!(self = ");
std::string retStr = ")) return "; std::string retStr = ")) return ";
retStr += getNilString(Pass.Ctx); retStr += getNilString(Pass);
Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr); Pass.TA.insertAfterToken(ExprRange.getEnd(), retStr);
} }
return true; return true;

View File

@ -16,6 +16,7 @@
#include "clang/Basic/SourceManager.h" #include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h" #include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Lexer.h" #include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Sema.h" #include "clang/Sema/Sema.h"
#include "clang/Sema/SemaDiagnostic.h" #include "clang/Sema/SemaDiagnostic.h"
#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/DenseSet.h"
@ -212,11 +213,8 @@ bool trans::isGlobalVar(Expr *E) {
return false; return false;
} }
StringRef trans::getNilString(ASTContext &Ctx) { StringRef trans::getNilString(MigrationPass &Pass) {
if (Ctx.Idents.get("nil").hasMacroDefinition()) return Pass.SemaRef.PP.isMacroDefined("nil") ? "nil" : "0";
return "nil";
else
return "0";
} }
namespace { namespace {

View File

@ -180,7 +180,7 @@ SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx,
bool hasSideEffects(Expr *E, ASTContext &Ctx); bool hasSideEffects(Expr *E, ASTContext &Ctx);
bool isGlobalVar(Expr *E); bool isGlobalVar(Expr *E);
/// \brief Returns "nil" or "0" if 'nil' macro is not actually defined. /// \brief Returns "nil" or "0" if 'nil' macro is not actually defined.
StringRef getNilString(ASTContext &Ctx); StringRef getNilString(MigrationPass &Pass);
template <typename BODY_TRANS> template <typename BODY_TRANS>
class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > { class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > {

View File

@ -505,6 +505,11 @@ StringRef NSAPI::GetNSIntegralKind(QualType T) const {
return StringRef(); return StringRef();
} }
bool NSAPI::isMacroDefined(StringRef Id) const {
// FIXME: Check whether the relevant module macros are visible.
return Ctx.Idents.get(Id).hasMacroDefinition();
}
bool NSAPI::isObjCTypedef(QualType T, bool NSAPI::isObjCTypedef(QualType T,
StringRef name, IdentifierInfo *&II) const { StringRef name, IdentifierInfo *&II) const {
if (!Ctx.getLangOpts().ObjC1) if (!Ctx.getLangOpts().ObjC1)

View File

@ -1084,79 +1084,51 @@ static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
// not have changed. // not have changed.
if (!Id->hadMacroDefinition()) if (!Id->hadMacroDefinition())
return; return;
auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
// If this identifier does not currently have a macro definition, // Find the macro definition from the command line.
// check whether it had one on the command line. MacroInfo *CmdLineDefinition = nullptr;
if (!Id->hasMacroDefinition()) { for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
MacroDirective::DefInfo LatestDef =
PP.getMacroDirectiveHistory(Id)->getDefinition();
for (MacroDirective::DefInfo Def = LatestDef; Def;
Def = Def.getPreviousDefinition()) {
FileID FID = SourceMgr.getFileID(Def.getLocation());
if (FID.isInvalid())
continue;
// We only care about the predefines buffer.
if (FID != PP.getPredefinesFileID())
continue;
// This macro was defined on the command line, then #undef'd later.
// Complain.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< true << ConfigMacro << Mod->getFullModuleName();
if (LatestDef.isUndefined())
PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
<< true;
return;
}
// Okay: no definition in the predefines buffer.
return;
}
// This identifier has a macro definition. Check whether we had a definition
// on the command line.
MacroDirective::DefInfo LatestDef =
PP.getMacroDirectiveHistory(Id)->getDefinition();
MacroDirective::DefInfo PredefinedDef;
for (MacroDirective::DefInfo Def = LatestDef; Def;
Def = Def.getPreviousDefinition()) {
FileID FID = SourceMgr.getFileID(Def.getLocation());
if (FID.isInvalid())
continue;
// We only care about the predefines buffer. // We only care about the predefines buffer.
if (FID != PP.getPredefinesFileID()) FileID FID = SourceMgr.getFileID(MD->getLocation());
if (FID.isInvalid() || FID != PP.getPredefinesFileID())
continue; continue;
if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
PredefinedDef = Def; CmdLineDefinition = DMD->getMacroInfo();
break; break;
} }
// If there was no definition for this macro in the predefines buffer, auto *CurrentDefinition = PP.getMacroInfo(Id);
// complain. if (CurrentDefinition == CmdLineDefinition) {
if (!PredefinedDef || // Macro matches. Nothing to do.
(!PredefinedDef.getLocation().isValid() && } else if (!CurrentDefinition) {
PredefinedDef.getUndefLocation().isValid())) { // This macro was defined on the command line, then #undef'd later.
// Complain.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< true << ConfigMacro << Mod->getFullModuleName();
auto LatestDef = LatestLocalMD->getDefinition();
assert(LatestDef.isUndefined() &&
"predefined macro went away with no #undef?");
PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
<< true;
return;
} else if (!CmdLineDefinition) {
// There was no definition for this macro in the predefines buffer,
// but there was a local definition. Complain.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< false << ConfigMacro << Mod->getFullModuleName(); << false << ConfigMacro << Mod->getFullModuleName();
PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here) PP.Diag(CurrentDefinition->getDefinitionLoc(),
diag::note_module_def_undef_here)
<< false;
} else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
/*Syntactically=*/true)) {
// The macro definitions differ.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< false << ConfigMacro << Mod->getFullModuleName();
PP.Diag(CurrentDefinition->getDefinitionLoc(),
diag::note_module_def_undef_here)
<< false; << false;
return;
} }
// If the current macro definition is the same as the predefined macro
// definition, it's okay.
if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() ||
LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP,
/*Syntactically=*/true))
return;
// The macro definitions differ.
PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
<< false << ConfigMacro << Mod->getFullModuleName();
PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
<< false;
} }
/// \brief Write a new timestamp file with the given path. /// \brief Write a new timestamp file with the given path.

View File

@ -686,9 +686,9 @@ static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
SmallVector<id_macro_pair, 128> MacrosByID; SmallVector<id_macro_pair, 128> MacrosByID;
for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
I != E; ++I) { I != E; ++I) {
if (I->first->hasMacroDefinition()) auto *MD = I->second.getLatest();
MacrosByID.push_back( if (MD && MD->isDefined())
id_macro_pair(I->first, I->second.getLatest()->getMacroInfo())); MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
} }
llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);

View File

@ -18,6 +18,7 @@
#include "clang/Lex/HeaderSearchOptions.h" #include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/LexDiagnostic.h" #include "clang/Lex/LexDiagnostic.h"
#include "clang/Lex/Lexer.h" #include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/APInt.h" #include "llvm/ADT/APInt.h"
#include "llvm/ADT/Hashing.h" #include "llvm/ADT/Hashing.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
@ -1016,7 +1017,9 @@ void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
HFI.setHeaderRole(Role); HFI.setHeaderRole(Role);
} }
bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
const FileEntry *File,
bool isImport) {
++NumIncluded; // Count # of attempted #includes. ++NumIncluded; // Count # of attempted #includes.
// Get information about this file. // Get information about this file.
@ -1041,7 +1044,7 @@ bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
// if the macro that guards it is defined, we know the #include has no effect. // if the macro that guards it is defined, we know the #include has no effect.
if (const IdentifierInfo *ControllingMacro if (const IdentifierInfo *ControllingMacro
= FileInfo.getControllingMacro(ExternalLookup)) = FileInfo.getControllingMacro(ExternalLookup))
if (ControllingMacro->hasMacroDefinition()) { if (PP.isMacroDefined(ControllingMacro)) {
++NumMultiIncludeFileOptzn; ++NumMultiIncludeFileOptzn;
return false; return false;
} }

View File

@ -133,12 +133,11 @@ bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
// If there are no identifiers in the argument list, or if the identifiers are // If there are no identifiers in the argument list, or if the identifiers are
// known to not be macros, pre-expansion won't modify it. // known to not be macros, pre-expansion won't modify it.
for (; ArgTok->isNot(tok::eof); ++ArgTok) for (; ArgTok->isNot(tok::eof); ++ArgTok)
if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) { if (IdentifierInfo *II = ArgTok->getIdentifierInfo())
if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled()) if (II->hasMacroDefinition())
// Return true even though the macro could be a function-like macro // Return true even though the macro could be a function-like macro
// without a following '(' token. // without a following '(' token, or could be disabled, or not visible.
return true; return true;
}
return false; return false;
} }

View File

@ -179,11 +179,13 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
return Diag(MacroNameTok, diag::err_defined_macro_name); return Diag(MacroNameTok, diag::err_defined_macro_name);
} }
if (isDefineUndef == MU_Undef && II->hasMacroDefinition() && if (isDefineUndef == MU_Undef) {
getMacroInfo(II)->isBuiltinMacro()) { auto *MI = getMacroInfo(II);
// Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 if (MI && MI->isBuiltinMacro()) {
// and C++ [cpp.predefined]p4], but allow it as an extension. // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); // and C++ [cpp.predefined]p4], but allow it as an extension.
Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
}
} }
// If defining/undefining reserved identifier or a keyword, we need to issue // If defining/undefining reserved identifier or a keyword, we need to issue
@ -1292,7 +1294,7 @@ void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
// Okay, we finally have a valid identifier to undef. // Okay, we finally have a valid identifier to undef.
MacroDirective *MD = getMacroDirective(II); MacroDirective *MD = getLocalMacroDirective(II);
// If the macro is not defined, this is an error. // If the macro is not defined, this is an error.
if (!MD) { if (!MD) {
@ -1319,7 +1321,7 @@ void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
// Okay, we finally have a valid identifier to undef. // Okay, we finally have a valid identifier to undef.
MacroDirective *MD = getMacroDirective(II); MacroDirective *MD = getLocalMacroDirective(II);
// If the macro is not defined, this is an error. // If the macro is not defined, this is an error.
if (!MD) { if (!MD) {
@ -1757,7 +1759,7 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
// Ask HeaderInfo if we should enter this #include file. If not, #including // Ask HeaderInfo if we should enter this #include file. If not, #including
// this file will have no effect. // this file will have no effect.
if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { if (!HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport)) {
if (Callbacks) if (Callbacks)
Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
return; return;
@ -2295,16 +2297,19 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) {
// Check to see if this is the last token on the #undef line. // Check to see if this is the last token on the #undef line.
CheckEndOfDirective("undef"); CheckEndOfDirective("undef");
// Okay, we finally have a valid identifier to undef. // Okay, we have a valid identifier to undef.
MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); auto *II = MacroNameTok.getIdentifierInfo();
const MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
// If the callbacks want to know, tell them about the macro #undef. // If the callbacks want to know, tell them about the macro #undef.
// Note: no matter if the macro was defined or not. // Note: no matter if the macro was defined or not.
if (Callbacks) if (Callbacks) {
// FIXME: Tell callbacks about module macros.
MacroDirective *MD = getLocalMacroDirective(II);
Callbacks->MacroUndefined(MacroNameTok, MD); Callbacks->MacroUndefined(MacroNameTok, MD);
}
// If the macro is not defined, this is a noop undef, just return. // If the macro is not defined, this is a noop undef, just return.
const MacroInfo *MI = getMacroInfo(II);
if (!MI) if (!MI)
return; return;
@ -2349,8 +2354,7 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
MacroDirective *MD = getMacroDirective(MII); MacroInfo *MI = getMacroInfo(MII);
MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr;
if (CurPPLexer->getConditionalStackDepth() == 0) { if (CurPPLexer->getConditionalStackDepth() == 0) {
// If the start of a top-level #ifdef and if the macro is not defined, // If the start of a top-level #ifdef and if the macro is not defined,
@ -2369,6 +2373,8 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
markMacroAsUsed(MI); markMacroAsUsed(MI);
if (Callbacks) { if (Callbacks) {
// FIXME: Tell callbacks about module macros.
MacroDirective *MD = getLocalMacroDirective(MII);
if (isIfndef) if (isIfndef)
Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
else else

View File

@ -108,15 +108,13 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
// Otherwise, we got an identifier, is it defined to something? // Otherwise, we got an identifier, is it defined to something?
IdentifierInfo *II = PeekTok.getIdentifierInfo(); IdentifierInfo *II = PeekTok.getIdentifierInfo();
Result.Val = II->hasMacroDefinition(); Preprocessor::MacroDefinition Macro = PP.getMacroDefinition(II);
Result.Val = !!Macro;
Result.Val.setIsUnsigned(false); // Result is signed intmax_t. Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
MacroDirective *Macro = nullptr;
// If there is a macro, mark it used. // If there is a macro, mark it used.
if (Result.Val != 0 && ValueLive) { if (Result.Val != 0 && ValueLive)
Macro = PP.getMacroDirective(II); PP.markMacroAsUsed(Macro.getMacroInfo());
PP.markMacroAsUsed(Macro->getMacroInfo());
}
// Save macro token for callback. // Save macro token for callback.
Token macroToken(PeekTok); Token macroToken(PeekTok);
@ -144,10 +142,8 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
// Invoke the 'defined' callback. // Invoke the 'defined' callback.
if (PPCallbacks *Callbacks = PP.getPPCallbacks()) { if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
MacroDirective *MD = Macro; // FIXME: Tell callbacks about module macros.
// Pass the MacroInfo for the macro name even if the value is dead. MacroDirective *MD = Macro.getLocalDirective();
if (!MD && Result.Val != 0)
MD = PP.getMacroDirective(II);
Callbacks->Defined(macroToken, MD, Callbacks->Defined(macroToken, MD,
SourceRange(beginLoc, PeekTok.getLocation())); SourceRange(beginLoc, PeekTok.getLocation()));
} }

View File

@ -309,7 +309,7 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
} }
if (const IdentifierInfo *DefinedMacro = if (const IdentifierInfo *DefinedMacro =
CurPPLexer->MIOpt.GetDefinedMacro()) { CurPPLexer->MIOpt.GetDefinedMacro()) {
if (!ControllingMacro->hasMacroDefinition() && if (!isMacroDefined(ControllingMacro) &&
DefinedMacro != ControllingMacro && DefinedMacro != ControllingMacro &&
HeaderInfo.FirstTimeLexingFile(FE)) { HeaderInfo.FirstTimeLexingFile(FE)) {

View File

@ -34,12 +34,11 @@
using namespace clang; using namespace clang;
MacroDirective * MacroDirective *
Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); if (!II->hadMacroDefinition())
return nullptr;
auto Pos = Macros.find(II); auto Pos = Macros.find(II);
assert(Pos != Macros.end() && "Identifier macro info is missing!"); return Pos == Macros.end() ? nullptr : Pos->second.getLatest();
return Pos->second.getLatest();
} }
void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
@ -54,7 +53,7 @@ void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
// Set up the identifier as having associated macro history. // Set up the identifier as having associated macro history.
II->setHasMacroDefinition(true); II->setHasMacroDefinition(true);
if (!MD->isDefined()) if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
II->setHasMacroDefinition(false); II->setHasMacroDefinition(false);
if (II->isFromAST() && !MD->isImported()) if (II->isFromAST() && !MD->isImported())
II->setChangedSinceDeserialization(); II->setChangedSinceDeserialization();
@ -69,7 +68,7 @@ void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
StoredMD = MD; StoredMD = MD;
// Setup the identifier as having associated macro history. // Setup the identifier as having associated macro history.
II->setHasMacroDefinition(true); II->setHasMacroDefinition(true);
if (!MD->isDefined()) if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
II->setHasMacroDefinition(false); II->setHasMacroDefinition(false);
} }
@ -108,6 +107,8 @@ ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,
// The new macro is always a leaf macro. // The new macro is always a leaf macro.
LeafMacros.push_back(MM); LeafMacros.push_back(MM);
// The identifier now has defined macros (that may or may not be visible).
II->setHasMacroDefinition(true);
New = true; New = true;
return MM; return MM;
@ -121,7 +122,7 @@ ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, IdentifierInfo *II) {
return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos); return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
} }
void Preprocessor::updateModuleMacroInfo(IdentifierInfo *II, void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
ModuleMacroInfo &Info) { ModuleMacroInfo &Info) {
assert(Info.ActiveModuleMacrosGeneration != MacroVisibilityGeneration && assert(Info.ActiveModuleMacrosGeneration != MacroVisibilityGeneration &&
"don't need to update this macro name info"); "don't need to update this macro name info");
@ -156,14 +157,20 @@ void Preprocessor::updateModuleMacroInfo(IdentifierInfo *II,
Worklist.push_back(O); Worklist.push_back(O);
} }
} }
// Our reverse postorder walk found the macros in reverse order.
std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
// Determine whether the macro name is ambiguous. // Determine whether the macro name is ambiguous.
Info.IsAmbiguous = false;
MacroInfo *MI = nullptr; MacroInfo *MI = nullptr;
bool IsSystemMacro = false; bool IsSystemMacro = true;
if (auto *DMD = dyn_cast<DefMacroDirective>(Info.MD)) { bool IsAmbiguous = false;
MI = DMD->getInfo(); if (auto *MD = Info.MD) {
IsSystemMacro = SourceMgr.isInSystemHeader(DMD->getLocation()); while (MD && isa<VisibilityMacroDirective>(MD))
MD = MD->getPrevious();
if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
MI = DMD->getInfo();
IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
}
} }
for (auto *Active : Info.ActiveModuleMacros) { for (auto *Active : Info.ActiveModuleMacros) {
auto *NewMI = Active->getMacroInfo(); auto *NewMI = Active->getMacroInfo();
@ -177,13 +184,14 @@ void Preprocessor::updateModuleMacroInfo(IdentifierInfo *II,
// //
// FIXME: Remove the defined-in-system-headers check. clang's limits.h // FIXME: Remove the defined-in-system-headers check. clang's limits.h
// overrides the system limits.h's macros, so there's no conflict here. // overrides the system limits.h's macros, so there's no conflict here.
IsSystemMacro &= Active->getOwningModule()->IsSystem; if (MI && NewMI != MI &&
if (MI && NewMI != MI && !IsSystemMacro && !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true))
!MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)) { IsAmbiguous = true;
Info.IsAmbiguous = true; IsSystemMacro &= Active->getOwningModule()->IsSystem ||
break; SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc());
} MI = NewMI;
} }
Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro;
} }
/// RegisterBuiltinMacro - Register the specified identifier in the identifier /// RegisterBuiltinMacro - Register the specified identifier in the identifier
@ -270,10 +278,11 @@ static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
// If the identifier is a macro, and if that macro is enabled, it may be // If the identifier is a macro, and if that macro is enabled, it may be
// expanded so it's not a trivial expansion. // expanded so it's not a trivial expansion.
if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && if (auto *ExpansionMI = PP.getMacroInfo(II))
// Fast expanding "#define X X" is ok, because X would be disabled. if (PP.getMacroInfo(II)->isEnabled() &&
II != MacroIdent) // Fast expanding "#define X X" is ok, because X would be disabled.
return false; II != MacroIdent)
return false;
// If this is an object-like macro invocation, it is safe to trivially expand // If this is an object-like macro invocation, it is safe to trivially expand
// it. // it.
@ -336,10 +345,8 @@ bool Preprocessor::isNextPPTokenLParen() {
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
/// expanded as a macro, handle it and return the next token as 'Identifier'. /// expanded as a macro, handle it and return the next token as 'Identifier'.
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
MacroDirective *MD) { const MacroDefinition &M) {
MacroDirective::DefInfo Def = MD->getDefinition(); MacroInfo *MI = M.getMacroInfo();
assert(Def.isValid());
MacroInfo *MI = Def.getMacroInfo();
// If this is a macro expansion in the "#if !defined(x)" line for the file, // If this is a macro expansion in the "#if !defined(x)" line for the file,
// then the macro could expand to different things in other contexts, we need // then the macro could expand to different things in other contexts, we need
@ -348,7 +355,8 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
// If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
if (MI->isBuiltinMacro()) { if (MI->isBuiltinMacro()) {
if (Callbacks) Callbacks->MacroExpands(Identifier, MD, // FIXME: Tell callbacks about module macros.
if (Callbacks) Callbacks->MacroExpands(Identifier, M.getLocalDirective(),
Identifier.getLocation(), Identifier.getLocation(),
/*Args=*/nullptr); /*Args=*/nullptr);
ExpandBuiltinMacro(Identifier); ExpandBuiltinMacro(Identifier);
@ -396,10 +404,13 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
// reading the function macro arguments. To ensure, in that case, that // reading the function macro arguments. To ensure, in that case, that
// MacroExpands callbacks still happen in source order, queue this // MacroExpands callbacks still happen in source order, queue this
// callback to have it happen after the function macro callback. // callback to have it happen after the function macro callback.
// FIXME: Tell callbacks about module macros.
DelayedMacroExpandsCallbacks.push_back( DelayedMacroExpandsCallbacks.push_back(
MacroExpandsInfo(Identifier, MD, ExpansionRange)); MacroExpandsInfo(Identifier, M.getLocalDirective(), ExpansionRange));
} else { } else {
Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args); // FIXME: Tell callbacks about module macros.
Callbacks->MacroExpands(Identifier, M.getLocalDirective(), ExpansionRange,
Args);
if (!DelayedMacroExpandsCallbacks.empty()) { if (!DelayedMacroExpandsCallbacks.empty()) {
for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
@ -413,20 +424,16 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
} }
// If the macro definition is ambiguous, complain. // If the macro definition is ambiguous, complain.
if (Def.getDirective()->isAmbiguous()) { if (M.isAmbiguous()) {
Diag(Identifier, diag::warn_pp_ambiguous_macro) Diag(Identifier, diag::warn_pp_ambiguous_macro)
<< Identifier.getIdentifierInfo(); << Identifier.getIdentifierInfo();
Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
<< Identifier.getIdentifierInfo(); << Identifier.getIdentifierInfo();
for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition(); M.forAllDefinitions([&](const MacroInfo *OtherMI) {
PrevDef && !PrevDef.isUndefined(); if (OtherMI != MI)
PrevDef = PrevDef.getPreviousDefinition()) { Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
Diag(PrevDef.getMacroInfo()->getDefinitionLoc(), << Identifier.getIdentifierInfo();
diag::note_pp_ambiguous_macro_other) });
<< Identifier.getIdentifierInfo();
if (!PrevDef.getDirective()->isAmbiguous())
break;
}
} }
// If we started lexing a macro, enter the macro expansion body. // If we started lexing a macro, enter the macro expansion body.

View File

@ -400,7 +400,7 @@ void Preprocessor::HandlePragmaPoison(Token &PoisonTok) {
if (II->isPoisoned()) continue; if (II->isPoisoned()) continue;
// If this is a macro identifier, emit a warning. // If this is a macro identifier, emit a warning.
if (II->hasMacroDefinition()) if (isMacroDefined(II))
Diag(Tok, diag::pp_poisoning_existing_macro); Diag(Tok, diag::pp_poisoning_existing_macro);
// Finally, poison it! // Finally, poison it!
@ -590,8 +590,7 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
PragmaPushMacroInfo.find(IdentInfo); PragmaPushMacroInfo.find(IdentInfo);
if (iter != PragmaPushMacroInfo.end()) { if (iter != PragmaPushMacroInfo.end()) {
// Forget the MacroInfo currently associated with IdentInfo. // Forget the MacroInfo currently associated with IdentInfo.
if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) { if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
MacroInfo *MI = CurrentMD->getMacroInfo();
if (MI->isWarnIfUnused()) if (MI->isWarnIfUnused())
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));

View File

@ -73,8 +73,7 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
ModuleImportExpectsIdentifier(false), CodeCompletionReached(0), ModuleImportExpectsIdentifier(false), CodeCompletionReached(0),
MainFileDir(nullptr), SkipMainFilePreamble(0, true), CurPPLexer(nullptr), MainFileDir(nullptr), SkipMainFilePreamble(0, true), CurPPLexer(nullptr),
CurDirLookup(nullptr), CurLexerKind(CLK_Lexer), CurSubmodule(nullptr), CurDirLookup(nullptr), CurLexerKind(CLK_Lexer), CurSubmodule(nullptr),
Callbacks(nullptr), MacroVisibilityGeneration(0), Callbacks(nullptr), MacroArgCache(nullptr), Record(nullptr),
MacroArgCache(nullptr), Record(nullptr),
MIChainHead(nullptr), DeserialMIChainHead(nullptr) { MIChainHead(nullptr), DeserialMIChainHead(nullptr) {
OwnsHeaderSearch = OwnsHeaders; OwnsHeaderSearch = OwnsHeaders;
@ -108,6 +107,9 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
// We haven't read anything from the external source. // We haven't read anything from the external source.
ReadMacrosFromExternalSource = false; ReadMacrosFromExternalSource = false;
// We might already have some macros from an imported module (via a PCH or
// preamble) if modules is enabled.
MacroVisibilityGeneration = LangOpts.Modules ? 1 : 0;
// "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
// This gets unpoisoned where it is allowed. // This gets unpoisoned where it is allowed.
@ -623,8 +625,8 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
} }
// If this is a macro to be expanded, do it. // If this is a macro to be expanded, do it.
if (MacroDirective *MD = getMacroDirective(&II)) { if (MacroDefinition MD = getMacroDefinition(&II)) {
MacroInfo *MI = MD->getMacroInfo(); auto *MI = MD.getMacroInfo();
if (!DisableMacroExpansion) { if (!DisableMacroExpansion) {
if (!Identifier.isExpandDisabled() && MI->isEnabled()) { if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
// C99 6.10.3p10: If the preprocessing token immediately after the // C99 6.10.3p10: If the preprocessing token immediately after the

View File

@ -2017,7 +2017,7 @@ static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
if (SemaRef.getLangOpts().C11) { if (SemaRef.getLangOpts().C11) {
// _Alignof // _Alignof
Builder.AddResultTypeChunk("size_t"); Builder.AddResultTypeChunk("size_t");
if (SemaRef.getASTContext().Idents.get("alignof").hasMacroDefinition()) if (SemaRef.PP.isMacroDefined("alignof"))
Builder.AddTypedTextChunk("alignof"); Builder.AddTypedTextChunk("alignof");
else else
Builder.AddTypedTextChunk("_Alignof"); Builder.AddTypedTextChunk("_Alignof");
@ -2085,15 +2085,14 @@ static void AddResultTypeChunk(ASTContext &Context,
Result.getAllocator())); Result.getAllocator()));
} }
static void MaybeAddSentinel(ASTContext &Context, static void MaybeAddSentinel(Preprocessor &PP,
const NamedDecl *FunctionOrMethod, const NamedDecl *FunctionOrMethod,
CodeCompletionBuilder &Result) { CodeCompletionBuilder &Result) {
if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>())
if (Sentinel->getSentinel() == 0) { if (Sentinel->getSentinel() == 0) {
if (Context.getLangOpts().ObjC1 && if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil"))
Context.Idents.get("nil").hasMacroDefinition())
Result.AddTextChunk(", nil"); Result.AddTextChunk(", nil");
else if (Context.Idents.get("NULL").hasMacroDefinition()) else if (PP.isMacroDefined("NULL"))
Result.AddTextChunk(", NULL"); Result.AddTextChunk(", NULL");
else else
Result.AddTextChunk(", (void*)0"); Result.AddTextChunk(", (void*)0");
@ -2117,8 +2116,7 @@ static std::string formatObjCParamQualifiers(unsigned ObjCQuals) {
return Result; return Result;
} }
static std::string FormatFunctionParameter(ASTContext &Context, static std::string FormatFunctionParameter(const PrintingPolicy &Policy,
const PrintingPolicy &Policy,
const ParmVarDecl *Param, const ParmVarDecl *Param,
bool SuppressName = false, bool SuppressName = false,
bool SuppressBlock = false) { bool SuppressBlock = false) {
@ -2217,7 +2215,7 @@ static std::string FormatFunctionParameter(ASTContext &Context,
for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) {
if (I) if (I)
Params += ", "; Params += ", ";
Params += FormatFunctionParameter(Context, Policy, Block.getParam(I), Params += FormatFunctionParameter(Policy, Block.getParam(I),
/*SuppressName=*/false, /*SuppressName=*/false,
/*SuppressBlock=*/true); /*SuppressBlock=*/true);
@ -2247,7 +2245,7 @@ static std::string FormatFunctionParameter(ASTContext &Context,
} }
/// \brief Add function parameter chunks to the given code completion string. /// \brief Add function parameter chunks to the given code completion string.
static void AddFunctionParameterChunks(ASTContext &Context, static void AddFunctionParameterChunks(Preprocessor &PP,
const PrintingPolicy &Policy, const PrintingPolicy &Policy,
const FunctionDecl *Function, const FunctionDecl *Function,
CodeCompletionBuilder &Result, CodeCompletionBuilder &Result,
@ -2265,7 +2263,7 @@ static void AddFunctionParameterChunks(ASTContext &Context,
Result.getCodeCompletionTUInfo()); Result.getCodeCompletionTUInfo());
if (!FirstParameter) if (!FirstParameter)
Opt.AddChunk(CodeCompletionString::CK_Comma); Opt.AddChunk(CodeCompletionString::CK_Comma);
AddFunctionParameterChunks(Context, Policy, Function, Opt, P, true); AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true);
Result.AddOptionalChunk(Opt.TakeString()); Result.AddOptionalChunk(Opt.TakeString());
break; break;
} }
@ -2278,9 +2276,8 @@ static void AddFunctionParameterChunks(ASTContext &Context,
InOptional = false; InOptional = false;
// Format the placeholder string. // Format the placeholder string.
std::string PlaceholderStr = FormatFunctionParameter(Context, Policy, std::string PlaceholderStr = FormatFunctionParameter(Policy, Param);
Param);
if (Function->isVariadic() && P == N - 1) if (Function->isVariadic() && P == N - 1)
PlaceholderStr += ", ..."; PlaceholderStr += ", ...";
@ -2295,7 +2292,7 @@ static void AddFunctionParameterChunks(ASTContext &Context,
if (Proto->getNumParams() == 0) if (Proto->getNumParams() == 0)
Result.AddPlaceholderChunk("..."); Result.AddPlaceholderChunk("...");
MaybeAddSentinel(Context, Function, Result); MaybeAddSentinel(PP, Function, Result);
} }
} }
@ -2575,11 +2572,7 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
} }
if (Kind == RK_Macro) { if (Kind == RK_Macro) {
const MacroDirective *MD = PP.getMacroDirectiveHistory(Macro); const MacroInfo *MI = PP.getMacroInfo(Macro);
assert(MD && "Not a macro?");
const MacroInfo *MI = MD->getMacroInfo();
assert((!MD->isDefined() || MI) && "missing MacroInfo for define");
Result.AddTypedTextChunk( Result.AddTypedTextChunk(
Result.getAllocator().CopyString(Macro->getName())); Result.getAllocator().CopyString(Macro->getName()));
@ -2654,7 +2647,7 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
Ctx, Policy); Ctx, Policy);
AddTypedNameChunk(Ctx, Policy, ND, Result); AddTypedNameChunk(Ctx, Policy, ND, Result);
Result.AddChunk(CodeCompletionString::CK_LeftParen); Result.AddChunk(CodeCompletionString::CK_LeftParen);
AddFunctionParameterChunks(Ctx, Policy, Function, Result); AddFunctionParameterChunks(PP, Policy, Function, Result);
Result.AddChunk(CodeCompletionString::CK_RightParen); Result.AddChunk(CodeCompletionString::CK_RightParen);
AddFunctionTypeQualsToCompletionString(Result, Function); AddFunctionTypeQualsToCompletionString(Result, Function);
return Result.TakeString(); return Result.TakeString();
@ -2708,7 +2701,7 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
// Add the function parameters // Add the function parameters
Result.AddChunk(CodeCompletionString::CK_LeftParen); Result.AddChunk(CodeCompletionString::CK_LeftParen);
AddFunctionParameterChunks(Ctx, Policy, Function, Result); AddFunctionParameterChunks(PP, Policy, Function, Result);
Result.AddChunk(CodeCompletionString::CK_RightParen); Result.AddChunk(CodeCompletionString::CK_RightParen);
AddFunctionTypeQualsToCompletionString(Result, Function); AddFunctionTypeQualsToCompletionString(Result, Function);
return Result.TakeString(); return Result.TakeString();
@ -2769,7 +2762,7 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
std::string Arg; std::string Arg;
if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity) if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity)
Arg = FormatFunctionParameter(Ctx, Policy, *P, true); Arg = FormatFunctionParameter(Policy, *P, true);
else { else {
(*P)->getType().getAsStringInternal(Arg, Policy); (*P)->getType().getAsStringInternal(Arg, Policy);
Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier()) Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier())
@ -2800,7 +2793,7 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
Result.AddPlaceholderChunk(", ..."); Result.AddPlaceholderChunk(", ...");
} }
MaybeAddSentinel(Ctx, Method, Result); MaybeAddSentinel(PP, Method, Result);
} }
return Result.TakeString(); return Result.TakeString();
@ -2854,8 +2847,7 @@ static void AddOverloadParameterChunks(ASTContext &Context,
// Format the placeholder string. // Format the placeholder string.
std::string Placeholder; std::string Placeholder;
if (Function) if (Function)
Placeholder = FormatFunctionParameter(Context, Policy, Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P));
Function->getParamDecl(P));
else else
Placeholder = Prototype->getParamType(P).getAsString(Policy); Placeholder = Prototype->getParamType(P).getAsString(Policy);
@ -3036,8 +3028,9 @@ static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
for (Preprocessor::macro_iterator M = PP.macro_begin(), for (Preprocessor::macro_iterator M = PP.macro_begin(),
MEnd = PP.macro_end(); MEnd = PP.macro_end();
M != MEnd; ++M) { M != MEnd; ++M) {
if (IncludeUndefined || M->first->hasMacroDefinition()) { auto MD = PP.getMacroDefinition(M->first);
if (MacroInfo *MI = M->second.getLatest()->getMacroInfo()) if (IncludeUndefined || MD) {
if (MacroInfo *MI = MD.getMacroInfo())
if (MI->isUsedForHeaderGuard()) if (MI->isUsedForHeaderGuard())
continue; continue;
@ -5122,7 +5115,7 @@ void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
// an action, e.g., // an action, e.g.,
// IBAction)<#selector#>:(id)sender // IBAction)<#selector#>:(id)sender
if (DS.getObjCDeclQualifier() == 0 && !IsParameter && if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
Context.Idents.get("IBAction").hasMacroDefinition()) { PP.isMacroDefined("IBAction")) {
CodeCompletionBuilder Builder(Results.getAllocator(), CodeCompletionBuilder Builder(Results.getAllocator(),
Results.getCodeCompletionTUInfo(), Results.getCodeCompletionTUInfo(),
CCP_CodePattern, CXAvailability_Available); CCP_CodePattern, CXAvailability_Available);

View File

@ -450,12 +450,11 @@ void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
SourceLocation MissingNilLoc SourceLocation MissingNilLoc
= PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); = PP.getLocForEndOfToken(sentinelExpr->getLocEnd());
std::string NullValue; std::string NullValue;
if (calleeType == CT_Method && if (calleeType == CT_Method && PP.isMacroDefined("nil"))
PP.getIdentifierInfo("nil")->hasMacroDefinition())
NullValue = "nil"; NullValue = "nil";
else if (getLangOpts().CPlusPlus11) else if (getLangOpts().CPlusPlus11)
NullValue = "nullptr"; NullValue = "nullptr";
else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) else if (PP.isMacroDefined("NULL"))
NullValue = "NULL"; NullValue = "NULL";
else else
NullValue = "(void*) 0"; NullValue = "(void*) 0";

View File

@ -161,11 +161,8 @@ bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
} }
static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) { static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
const IdentifierInfo *II = &S.getASTContext().Idents.get(Name); return (bool)S.PP.getMacroDefinitionAtLoc(&S.getASTContext().Idents.get(Name),
if (!II->hadMacroDefinition()) return false; Loc);
MacroDirective *Macro = S.PP.getMacroDirectiveHistory(II);
return Macro && Macro->findDirectiveAtLoc(Loc, S.getSourceManager());
} }
static std::string getScalarZeroExpressionForType( static std::string getScalarZeroExpressionForType(

View File

@ -1723,22 +1723,6 @@ void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
IdentifierGeneration[II] = getGeneration(); IdentifierGeneration[II] = getGeneration();
} }
struct ASTReader::ModuleMacroInfo {
ModuleMacro *MM;
// FIXME: Remove this.
ModuleFile *F;
bool isDefine() const { return MM->getMacroInfo(); }
ArrayRef<ModuleMacro *> getOverriddenMacros() const {
return MM->overrides();
}
MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
return PP.AllocateImportedMacroDirective(MM, ImportLoc);
}
};
void ASTReader::resolvePendingMacro(IdentifierInfo *II, void ASTReader::resolvePendingMacro(IdentifierInfo *II,
const PendingMacroInfo &PMInfo) { const PendingMacroInfo &PMInfo) {
ModuleFile &M = *PMInfo.M; ModuleFile &M = *PMInfo.M;
@ -1806,19 +1790,7 @@ void ASTReader::resolvePendingMacro(IdentifierInfo *II,
bool Inserted = false; bool Inserted = false;
Module *Owner = getSubmodule(MMR.SubModID); Module *Owner = getSubmodule(MMR.SubModID);
auto *MM = PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
if (!Inserted)
continue;
ModuleMacroInfo MMI = { MM, &M };
if (Owner->NameVisibility == Module::Hidden) {
// Macros in the owning module are hidden. Just remember this macro to
// install if we make this module visible.
HiddenNamesMap[Owner].HiddenMacros.insert(
std::make_pair(II, new (Context) ModuleMacroInfo(MMI)));
} else {
installImportedMacro(II, MMI, Owner);
}
} }
} }
@ -1870,164 +1842,6 @@ void ASTReader::resolvePendingMacro(IdentifierInfo *II,
PP.setLoadedMacroDirective(II, Latest); PP.setLoadedMacroDirective(II, Latest);
} }
/// \brief For the given macro definitions, check if they are both in system
/// modules.
static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Module *NewOwner, ASTReader &Reader) {
assert(PrevMI && NewMI);
Module *PrevOwner = nullptr;
if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
PrevOwner = Reader.getSubmodule(PrevModID);
if (PrevOwner && PrevOwner == NewOwner)
return false;
SourceManager &SrcMgr = Reader.getSourceManager();
bool PrevInSystem = (PrevOwner && PrevOwner->IsSystem) ||
SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
bool NewInSystem = (NewOwner && NewOwner->IsSystem) ||
SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
return PrevInSystem && NewInSystem;
}
void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
SourceLocation ImportLoc,
AmbiguousMacros &Ambig,
ArrayRef<ModuleMacro *> Overrides) {
for (ModuleMacro *Overridden : Overrides) {
Module *Owner = Overridden->getOwningModule();
// If this macro is not yet visible, remove it from the hidden names list.
// It won't be there if we're in the middle of making the owner visible.
auto HiddenIt = HiddenNamesMap.find(Owner);
if (HiddenIt != HiddenNamesMap.end()) {
HiddenNames &Hidden = HiddenIt->second;
HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
if (HI != Hidden.HiddenMacros.end()) {
// Register the macro now so we don't lose it when we re-export.
PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
auto SubOverrides = HI->second->getOverriddenMacros();
Hidden.HiddenMacros.erase(HI);
removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
}
}
// If this macro is already in our list of conflicts, remove it from there.
Ambig.erase(
std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
return getSubmodule(MD->getInfo()->getOwningModuleID()) == Owner;
}),
Ambig.end());
}
}
ASTReader::AmbiguousMacros *
ASTReader::removeOverriddenMacros(IdentifierInfo *II,
SourceLocation ImportLoc,
ArrayRef<ModuleMacro *> Overrides) {
MacroDirective *Prev = PP.getMacroDirective(II);
if (!Prev && Overrides.empty())
return nullptr;
DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
: nullptr;
if (PrevDef && PrevDef->isAmbiguous()) {
// We had a prior ambiguity. Check whether we resolve it (or make it worse).
AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
Ambig.push_back(PrevDef);
removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
if (!Ambig.empty())
return &Ambig;
AmbiguousMacroDefs.erase(II);
} else {
// There's no ambiguity yet. Maybe we're introducing one.
AmbiguousMacros Ambig;
if (PrevDef)
Ambig.push_back(PrevDef);
removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
if (!Ambig.empty()) {
AmbiguousMacros &Result = AmbiguousMacroDefs[II];
std::swap(Result, Ambig);
return &Result;
}
}
// We ended up with no ambiguity.
return nullptr;
}
void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo &MMI,
Module *Owner) {
assert(II && Owner);
SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
AmbiguousMacros *Prev =
removeOverriddenMacros(II, ImportLoc, MMI.getOverriddenMacros());
// Create a synthetic macro definition corresponding to the import (or null
// if this was an undefinition of the macro).
MacroDirective *Imported = MMI.import(PP, ImportLoc);
DefMacroDirective *MD = dyn_cast<DefMacroDirective>(Imported);
// If there's no ambiguity, just install the macro.
if (!Prev) {
PP.appendMacroDirective(II, Imported);
return;
}
assert(!Prev->empty());
if (!MD) {
// We imported a #undef that didn't remove all prior definitions. The most
// recent prior definition remains, and we install it in the place of the
// imported directive, as if by a local #pragma pop_macro.
MacroInfo *NewMI = Prev->back()->getInfo();
Prev->pop_back();
MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc);
// Install our #undef first so that we don't lose track of it. We'll replace
// this with whichever macro definition ends up winning.
PP.appendMacroDirective(II, Imported);
}
// We're introducing a macro definition that creates or adds to an ambiguity.
// We can resolve that ambiguity if this macro is token-for-token identical to
// all of the existing definitions.
MacroInfo *NewMI = MD->getInfo();
assert(NewMI && "macro definition with no MacroInfo?");
while (!Prev->empty()) {
MacroInfo *PrevMI = Prev->back()->getInfo();
assert(PrevMI && "macro definition with no MacroInfo?");
// Before marking the macros as ambiguous, check if this is a case where
// both macros are in system headers. If so, we trust that the system
// did not get it wrong. This also handles cases where Clang's own
// headers have a different spelling of certain system macros:
// #define LONG_MAX __LONG_MAX__ (clang's limits.h)
// #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
//
// FIXME: Remove the defined-in-system-headers check. clang's limits.h
// overrides the system limits.h's macros, so there's no conflict here.
if (NewMI != PrevMI &&
!PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
break;
// The previous definition is the same as this one (or both are defined in
// system modules so we can assume they're equivalent); we don't need to
// track it any more.
Prev->pop_back();
}
if (!Prev->empty())
MD->setAmbiguous(true);
PP.appendMacroDirective(II, MD);
}
ASTReader::InputFileInfo ASTReader::InputFileInfo
ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
// Go find this input file. // Go find this input file.
@ -3422,7 +3236,7 @@ void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
"nothing to make visible?"); "nothing to make visible?");
// FIXME: Only do this if Owner->NameVisibility == AllVisible. // FIXME: Only do this if Owner->NameVisibility == AllVisible.
for (Decl *D : Names.HiddenDecls) { for (Decl *D : Names) {
bool wasHidden = D->Hidden; bool wasHidden = D->Hidden;
D->Hidden = false; D->Hidden = false;
@ -3432,9 +3246,6 @@ void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
} }
} }
} }
for (const auto &Macro : Names.HiddenMacros)
installImportedMacro(Macro.first, *Macro.second, Owner);
} }
void ASTReader::makeModuleVisible(Module *Mod, void ASTReader::makeModuleVisible(Module *Mod,

View File

@ -474,7 +474,7 @@ void ASTDeclReader::VisitDecl(Decl *D) {
// Note that this declaration was hidden because its owning module is // Note that this declaration was hidden because its owning module is
// not yet visible. // not yet visible.
Reader.HiddenNamesMap[Owner].HiddenDecls.push_back(D); Reader.HiddenNamesMap[Owner].push_back(D);
} }
} }
} }
@ -1402,8 +1402,8 @@ void ASTDeclReader::MergeDefinitionData(
else { else {
auto SubmoduleID = MergeDD.Definition->getOwningModuleID(); auto SubmoduleID = MergeDD.Definition->getOwningModuleID();
assert(SubmoduleID && "hidden definition in no module"); assert(SubmoduleID && "hidden definition in no module");
Reader.HiddenNamesMap[Reader.getSubmodule(SubmoduleID)] Reader.HiddenNamesMap[Reader.getSubmodule(SubmoduleID)].push_back(
.HiddenDecls.push_back(DD.Definition); DD.Definition);
} }
} }
} }
@ -3816,7 +3816,7 @@ void ASTDeclReader::UpdateDecl(Decl *D, ModuleFile &ModuleFile,
if (Owner && Owner->NameVisibility != Module::AllVisible) { if (Owner && Owner->NameVisibility != Module::AllVisible) {
// If Owner is made visible at some later point, make this declaration // If Owner is made visible at some later point, make this declaration
// visible too. // visible too.
Reader.HiddenNamesMap[Owner].HiddenDecls.push_back(D); Reader.HiddenNamesMap[Owner].push_back(D);
} else { } else {
// The declaration is now visible. // The declaration is now visible.
D->Hidden = false; D->Hidden = false;

View File

@ -2050,7 +2050,7 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
// If the macro or identifier need no updates, don't write the macro history // If the macro or identifier need no updates, don't write the macro history
// for this one. // for this one.
// FIXME: Chain the macro history instead of re-writing it. // FIXME: Chain the macro history instead of re-writing it.
if (MD->isFromPCH() && if (MD && MD->isFromPCH() &&
Name->isFromAST() && !Name->hasChangedSinceDeserialization()) Name->isFromAST() && !Name->hasChangedSinceDeserialization())
continue; continue;

View File

@ -28,7 +28,7 @@ void test_variadic() {
// RUN: c-index-test -code-completion-at=%s:7:1 %s -I%S | FileCheck -check-prefix=CHECK-CC0 %s // RUN: c-index-test -code-completion-at=%s:7:1 %s -I%S | FileCheck -check-prefix=CHECK-CC0 %s
// CHECK-CC0-NOT: FOO // CHECK-CC0-NOT: FOO
// RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test -code-completion-at=%s:7:1 %s -I%S | FileCheck -check-prefix=CHECK-CC1 %s // RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test -code-completion-at=%s:7:1 %s -I%S | FileCheck -check-prefix=CHECK-CC1 %s
// CHECK-CC1: macro definition:{TypedText FOO}{LeftParen (}{Placeholder Arg1}{Comma , }{Placeholder Arg2}{RightParen )} // CHECK-CC1: macro definition:{TypedText FOO} (70)
// RUN: c-index-test -code-completion-at=%s:13:13 %s -I%S | FileCheck -check-prefix=CHECK-CC2 %s // RUN: c-index-test -code-completion-at=%s:13:13 %s -I%S | FileCheck -check-prefix=CHECK-CC2 %s
// RUN: c-index-test -code-completion-at=%s:14:8 %s -I%S | FileCheck -check-prefix=CHECK-CC2 %s // RUN: c-index-test -code-completion-at=%s:14:8 %s -I%S | FileCheck -check-prefix=CHECK-CC2 %s
// RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test -code-completion-at=%s:14:8 %s -I%S | FileCheck -check-prefix=CHECK-CC2 %s // RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test -code-completion-at=%s:14:8 %s -I%S | FileCheck -check-prefix=CHECK-CC2 %s

View File

@ -13,6 +13,7 @@
// expected-note@Inputs/macros_right.h:12{{expanding this definition of 'LEFT_RIGHT_DIFFERENT'}} // expected-note@Inputs/macros_right.h:12{{expanding this definition of 'LEFT_RIGHT_DIFFERENT'}}
// expected-note@Inputs/macros_right.h:13{{expanding this definition of 'LEFT_RIGHT_DIFFERENT2'}} // expected-note@Inputs/macros_right.h:13{{expanding this definition of 'LEFT_RIGHT_DIFFERENT2'}}
// expected-note@Inputs/macros_left.h:14{{other definition of 'LEFT_RIGHT_DIFFERENT'}} // expected-note@Inputs/macros_left.h:14{{other definition of 'LEFT_RIGHT_DIFFERENT'}}
// expected-note@Inputs/macros_left.h:11{{other definition of 'LEFT_RIGHT_DIFFERENT2'}}
@import macros; @import macros;

View File

@ -7144,7 +7144,7 @@ MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II,
ASTUnit *Unit = cxtu::getASTUnit(TU); ASTUnit *Unit = cxtu::getASTUnit(TU);
Preprocessor &PP = Unit->getPreprocessor(); Preprocessor &PP = Unit->getPreprocessor();
MacroDirective *MD = PP.getMacroDirectiveHistory(&II); MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II);
if (MD) { if (MD) {
for (MacroDirective::DefInfo for (MacroDirective::DefInfo
Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) {
@ -7201,7 +7201,7 @@ MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI,
if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end()) if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end())
return nullptr; return nullptr;
MacroDirective *InnerMD = PP.getMacroDirectiveHistory(&II); MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II);
if (!InnerMD) if (!InnerMD)
return nullptr; return nullptr;