diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ARCMigrate/TransGCAttrs.cpp | 3 | ||||
-rw-r--r-- | lib/AST/ASTContext.cpp | 19 | ||||
-rw-r--r-- | lib/AST/ASTDiagnostic.cpp | 5 | ||||
-rw-r--r-- | lib/AST/ASTStructuralEquivalence.cpp | 7 | ||||
-rw-r--r-- | lib/AST/ItaniumMangle.cpp | 1 | ||||
-rw-r--r-- | lib/AST/Type.cpp | 19 | ||||
-rw-r--r-- | lib/AST/TypePrinter.cpp | 16 | ||||
-rw-r--r-- | lib/CodeGen/CGDebugInfo.cpp | 4 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenFunction.cpp | 1 | ||||
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 40 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 5 | ||||
-rw-r--r-- | lib/Sema/SemaStmt.cpp | 8 | ||||
-rw-r--r-- | lib/Sema/SemaType.cpp | 27 | ||||
-rw-r--r-- | lib/Sema/TreeTransform.h | 27 | ||||
-rw-r--r-- | lib/Serialization/ASTReader.cpp | 14 | ||||
-rw-r--r-- | lib/Serialization/ASTWriter.cpp | 11 |
16 files changed, 195 insertions, 12 deletions
diff --git a/lib/ARCMigrate/TransGCAttrs.cpp b/lib/ARCMigrate/TransGCAttrs.cpp index a73b526ca8..fdbe1d119a 100644 --- a/lib/ARCMigrate/TransGCAttrs.cpp +++ b/lib/ARCMigrate/TransGCAttrs.cpp @@ -68,6 +68,9 @@ public: if (handleAttr(Attr, D)) break; TL = Attr.getModifiedLoc(); + } else if (MacroQualifiedTypeLoc MDTL = + TL.getAs<MacroQualifiedTypeLoc>()) { + TL = MDTL.getInnerLoc(); } else if (ArrayTypeLoc Arr = TL.getAs<ArrayTypeLoc>()) { TL = Arr.getElementLoc(); } else if (PointerTypeLoc PT = TL.getAs<PointerTypeLoc>()) { diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index d0a790cad4..6348885375 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -2047,6 +2047,10 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { case Type::Paren: return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); + case Type::MacroQualified: + return getTypeInfo( + cast<MacroQualifiedType>(T)->getUnderlyingType().getTypePtr()); + case Type::ObjCTypeParam: return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); @@ -3929,7 +3933,7 @@ QualType ASTContext::getAttributedType(attr::Kind attrKind, QualType canon = getCanonicalType(equivalentType); type = new (*this, TypeAlignment) - AttributedType(canon, attrKind, modifiedType, equivalentType); + AttributedType(canon, attrKind, modifiedType, equivalentType); Types.push_back(type); AttributedTypes.InsertNode(type, insertPos); @@ -4210,6 +4214,19 @@ ASTContext::getParenType(QualType InnerType) const { return QualType(T, 0); } +QualType +ASTContext::getMacroQualifiedType(QualType UnderlyingTy, + const IdentifierInfo *MacroII) const { + QualType Canon = UnderlyingTy; + if (!Canon.isCanonical()) + Canon = getCanonicalType(UnderlyingTy); + + auto *newType = new (*this, TypeAlignment) + MacroQualifiedType(UnderlyingTy, Canon, MacroII); + Types.push_back(newType); + return QualType(newType, 0); +} + QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, const IdentifierInfo *Name, diff --git a/lib/AST/ASTDiagnostic.cpp b/lib/AST/ASTDiagnostic.cpp index 61900aa4ac..15df865852 100644 --- a/lib/AST/ASTDiagnostic.cpp +++ b/lib/AST/ASTDiagnostic.cpp @@ -41,6 +41,11 @@ static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) { QT = PT->desugar(); continue; } + // ... or a macro defined type ... + if (const MacroQualifiedType *MDT = dyn_cast<MacroQualifiedType>(Ty)) { + QT = MDT->desugar(); + continue; + } // ...or a substituted template type parameter ... if (const SubstTemplateTypeParmType *ST = dyn_cast<SubstTemplateTypeParmType>(Ty)) { diff --git a/lib/AST/ASTStructuralEquivalence.cpp b/lib/AST/ASTStructuralEquivalence.cpp index 71bbd82481..567945c16c 100644 --- a/lib/AST/ASTStructuralEquivalence.cpp +++ b/lib/AST/ASTStructuralEquivalence.cpp @@ -595,6 +595,13 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, return false; break; + case Type::MacroQualified: + if (!IsStructurallyEquivalent( + Context, cast<MacroQualifiedType>(T1)->getUnderlyingType(), + cast<MacroQualifiedType>(T2)->getUnderlyingType())) + return false; + break; + case Type::Typedef: if (!IsStructurallyEquivalent(Context, cast<TypedefType>(T1)->getDecl(), cast<TypedefType>(T2)->getDecl())) diff --git a/lib/AST/ItaniumMangle.cpp b/lib/AST/ItaniumMangle.cpp index 3357756466..930537a934 100644 --- a/lib/AST/ItaniumMangle.cpp +++ b/lib/AST/ItaniumMangle.cpp @@ -1941,6 +1941,7 @@ bool CXXNameMangler::mangleUnresolvedTypeOrSimpleId(QualType Ty, case Type::ObjCTypeParam: case Type::Atomic: case Type::Pipe: + case Type::MacroQualified: llvm_unreachable("type is illegal as a nested name specifier"); case Type::SubstTemplateTypeParmPack: diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index adffe92f95..590e534fbd 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -973,6 +973,7 @@ public: SUGARED_TYPE_CLASS(Typedef) SUGARED_TYPE_CLASS(ObjCTypeParam) + SUGARED_TYPE_CLASS(MacroQualified) QualType VisitAdjustedType(const AdjustedType *T) { QualType originalType = recurse(T->getOriginalType()); @@ -1735,6 +1736,10 @@ namespace { return Visit(T->getModifiedType()); } + Type *VisitMacroQualifiedType(const MacroQualifiedType *T) { + return Visit(T->getUnderlyingType()); + } + Type *VisitAdjustedType(const AdjustedType *T) { return Visit(T->getOriginalType()); } @@ -3160,6 +3165,20 @@ QualType TypedefType::desugar() const { return getDecl()->getUnderlyingType(); } +QualType MacroQualifiedType::desugar() const { return getUnderlyingType(); } + +QualType MacroQualifiedType::getModifiedType() const { + // Step over MacroQualifiedTypes from the same macro to find the type + // ultimately qualified by the macro qualifier. + QualType Inner = cast<AttributedType>(getUnderlyingType())->getModifiedType(); + while (auto *InnerMQT = dyn_cast<MacroQualifiedType>(Inner)) { + if (InnerMQT->getMacroIdentifier() != getMacroIdentifier()) + break; + Inner = InnerMQT->getModifiedType(); + } + return Inner; +} + TypeOfExprType::TypeOfExprType(Expr *E, QualType can) : Type(TypeOfExpr, can, E->isTypeDependent(), E->isInstantiationDependent(), diff --git a/lib/AST/TypePrinter.cpp b/lib/AST/TypePrinter.cpp index 82a2fa09c7..fed39cadcb 100644 --- a/lib/AST/TypePrinter.cpp +++ b/lib/AST/TypePrinter.cpp @@ -259,6 +259,7 @@ bool TypePrinter::canPrefixQualifiers(const Type *T, case Type::Paren: case Type::PackExpansion: case Type::SubstTemplateTypeParm: + case Type::MacroQualified: CanPrefixQualifiers = false; break; @@ -963,6 +964,21 @@ void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) { printTypeSpec(T->getDecl(), OS); } +void TypePrinter::printMacroQualifiedBefore(const MacroQualifiedType *T, + raw_ostream &OS) { + StringRef MacroName = T->getMacroIdentifier()->getName(); + OS << MacroName << " "; + + // Since this type is meant to print the macro instead of the whole attribute, + // we trim any attributes and go directly to the original modified type. + printBefore(T->getModifiedType(), OS); +} + +void TypePrinter::printMacroQualifiedAfter(const MacroQualifiedType *T, + raw_ostream &OS) { + printAfter(T->getModifiedType(), OS); +} + void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) {} void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T, diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index 3656602c3d..4c43c00cd5 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -2844,6 +2844,9 @@ static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { case Type::Paren: T = cast<ParenType>(T)->getInnerType(); break; + case Type::MacroQualified: + T = cast<MacroQualifiedType>(T)->getUnderlyingType(); + break; case Type::SubstTemplateTypeParm: T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); break; @@ -3023,6 +3026,7 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) { case Type::DeducedTemplateSpecialization: case Type::Elaborated: case Type::Paren: + case Type::MacroQualified: case Type::SubstTemplateTypeParm: case Type::TypeOfExpr: case Type::TypeOf: diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 7a8d79ba3b..d7bb4a54c1 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -2149,6 +2149,7 @@ void CodeGenFunction::EmitVariablyModifiedType(QualType type) { case Type::Attributed: case Type::SubstTemplateTypeParm: case Type::PackExpansion: + case Type::MacroQualified: // Keep walking after single level desugaring. type = type.getSingleStepDesugaredType(getContext()); break; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 130cd9f206..4e5ca2ee8f 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -85,6 +85,23 @@ static bool isAttributeLateParsed(const IdentifierInfo &II) { #undef CLANG_ATTR_LATE_PARSED_LIST } +/// Check if the a start and end source location expand to the same macro. +bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc, + SourceLocation EndLoc) { + if (!StartLoc.isMacroID() || !EndLoc.isMacroID()) + return false; + + SourceManager &SM = PP.getSourceManager(); + if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc)) + return false; + + bool AttrStartIsInMacro = + Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts()); + bool AttrEndIsInMacro = + Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts()); + return AttrStartIsInMacro && AttrEndIsInMacro; +} + /// ParseGNUAttributes - Parse a non-empty attributes list. /// /// [GNU] attributes: @@ -133,7 +150,10 @@ void Parser::ParseGNUAttributes(ParsedAttributes &attrs, assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); while (Tok.is(tok::kw___attribute)) { - ConsumeToken(); + SourceLocation AttrTokLoc = ConsumeToken(); + unsigned OldNumAttrs = attrs.size(); + unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0; + if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "attribute")) { SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; @@ -201,6 +221,24 @@ void Parser::ParseGNUAttributes(ParsedAttributes &attrs, SkipUntil(tok::r_paren, StopAtSemi); if (endLoc) *endLoc = Loc; + + // If this was declared in a macro, attach the macro IdentifierInfo to the + // parsed attribute. + if (FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) { + auto &SM = PP.getSourceManager(); + CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc); + StringRef FoundName = + Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts()); + IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName); + + for (unsigned i = OldNumAttrs; i < attrs.size(); ++i) + attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin()); + + if (LateAttrs) { + for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i) + (*LateAttrs)[i]->MacroII = MacroII; + } + } } } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 9564c8f0cb..3507361412 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -4096,6 +4096,7 @@ static void captureVariablyModifiedType(ASTContext &Context, QualType T, case Type::Attributed: case Type::SubstTemplateTypeParm: case Type::PackExpansion: + case Type::MacroQualified: // Keep walking after single level desugaring. T = T.getSingleStepDesugaredType(Context); break; @@ -13695,8 +13696,8 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, // Look for an explicit signature in that function type. FunctionProtoTypeLoc ExplicitSignature; - if ((ExplicitSignature = - Sig->getTypeLoc().getAsAdjusted<FunctionProtoTypeLoc>())) { + if ((ExplicitSignature = Sig->getTypeLoc() + .getAsAdjusted<FunctionProtoTypeLoc>())) { // Check whether that explicit signature was synthesized by // GetTypeForDeclarator. If so, don't save that as part of the diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index feb0052d42..34a769b251 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -3385,10 +3385,10 @@ bool LocalTypedefNameReferencer::VisitRecordType(const RecordType *RT) { } TypeLoc Sema::getReturnTypeLoc(FunctionDecl *FD) const { - TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); - while (auto ATL = TL.getAs<AttributedTypeLoc>()) - TL = ATL.getModifiedLoc().IgnoreParens(); - return TL.castAs<FunctionProtoTypeLoc>().getReturnLoc(); + return FD->getTypeSourceInfo() + ->getTypeLoc() + .getAsAdjusted<FunctionProtoTypeLoc>() + .getReturnLoc(); } /// Deduce the return type for a function from a returned expression, per diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 3b25595a33..65cf3f59b3 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -5643,6 +5643,9 @@ namespace { assert(Chunk.Kind == DeclaratorChunk::Pipe); TL.setKWLoc(Chunk.Loc); } + void VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { + TL.setExpansionLoc(Chunk.Loc); + } void VisitTypeLoc(TypeLoc TL) { llvm_unreachable("unsupported TypeLoc kind in declarator!"); @@ -5721,6 +5724,9 @@ GetTypeSourceInfoForDeclarator(TypeProcessingState &State, CurrTL = ATL.getValueLoc().getUnqualifiedLoc(); } + while (MacroQualifiedTypeLoc TL = CurrTL.getAs<MacroQualifiedTypeLoc>()) + CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); + while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) { fillAttributedTypeLoc(TL, State); CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); @@ -6981,12 +6987,16 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr, return true; } -bool Sema::hasExplicitCallingConv(QualType &T) { - QualType R = T.IgnoreParens(); - while (const AttributedType *AT = dyn_cast<AttributedType>(R)) { +bool Sema::hasExplicitCallingConv(QualType T) { + const AttributedType *AT; + + // Stop if we'd be stripping off a typedef sugar node to reach the + // AttributedType. + while ((AT = T->getAs<AttributedType>()) && + AT->getAs<TypedefType>() == T->getAs<TypedefType>()) { if (AT->isCallingConv()) return true; - R = AT->getModifiedType().IgnoreParens(); + T = AT->getModifiedType(); } return false; } @@ -7571,6 +7581,15 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type, distributeFunctionTypeAttr(state, attr, type); break; } + + // Handle attributes that are defined in a macro. We do not want this to be + // applied to ObjC builtin attributes. + if (isa<AttributedType>(type) && attr.hasMacroIdentifier() && + !type.getQualifiers().hasObjCLifetime() && + !type.getQualifiers().hasObjCGCAttr()) { + const IdentifierInfo *MacroII = attr.getMacroIdentifier(); + type = state.getSema().Context.getMacroQualifiedType(type, MacroII); + } } if (!state.getSema().getLangOpts().OpenCL || diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index a8ea6b01d1..dddb2476c7 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -883,6 +883,12 @@ public: return SemaRef.Context.getTypeDeclType(Typedef); } + /// Build a new MacroDefined type. + QualType RebuildMacroQualifiedType(QualType T, + const IdentifierInfo *MacroII) { + return SemaRef.Context.getMacroQualifiedType(T, MacroII); + } + /// Build a new class/struct/union type. QualType RebuildRecordType(RecordDecl *Record) { return SemaRef.Context.getTypeDeclType(Record); @@ -6193,6 +6199,27 @@ TreeTransform<Derived>::TransformParenType(TypeLocBuilder &TLB, return Result; } +template <typename Derived> +QualType +TreeTransform<Derived>::TransformMacroQualifiedType(TypeLocBuilder &TLB, + MacroQualifiedTypeLoc TL) { + QualType Inner = getDerived().TransformType(TLB, TL.getInnerLoc()); + if (Inner.isNull()) + return QualType(); + + QualType Result = TL.getType(); + if (getDerived().AlwaysRebuild() || Inner != TL.getInnerLoc().getType()) { + Result = + getDerived().RebuildMacroQualifiedType(Inner, TL.getMacroIdentifier()); + if (Result.isNull()) + return QualType(); + } + + MacroQualifiedTypeLoc NewTL = TLB.push<MacroQualifiedTypeLoc>(Result); + NewTL.setExpansionLoc(TL.getExpansionLoc()); + return Result; +} + template<typename Derived> QualType TreeTransform<Derived>::TransformDependentNameType( TypeLocBuilder &TLB, DependentNameTypeLoc TL) { diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index a6ff54568d..64af0fa8a1 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -6200,6 +6200,16 @@ QualType ASTReader::readTypeRecord(unsigned Index) { return Context.getParenType(InnerType); } + case TYPE_MACRO_QUALIFIED: { + if (Record.size() != 2) { + Error("incorrect encoding of macro defined type"); + return QualType(); + } + QualType UnderlyingTy = readType(*Loc.F, Record, Idx); + IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx); + return Context.getMacroQualifiedType(UnderlyingTy, MacroII); + } + case TYPE_PACK_EXPANSION: { if (Record.size() != 2) { Error("incorrect encoding of pack expansion type"); @@ -6521,6 +6531,10 @@ void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { // nothing to do } +void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { + TL.setExpansionLoc(ReadSourceLocation()); +} + void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { TL.setCaretLoc(ReadSourceLocation()); } diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 756411a8c5..accd8ccdfa 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -516,6 +516,12 @@ void ASTTypeWriter::VisitParenType(const ParenType *T) { Code = TYPE_PAREN; } +void ASTTypeWriter::VisitMacroQualifiedType(const MacroQualifiedType *T) { + Record.AddTypeRef(T->getUnderlyingType()); + Record.AddIdentifierRef(T->getMacroIdentifier()); + Code = TYPE_MACRO_QUALIFIED; +} + void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { Record.push_back(T->getKeyword()); Record.AddNestedNameSpecifier(T->getQualifier()); @@ -802,6 +808,10 @@ void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { Record.AddSourceLocation(TL.getRParenLoc()); } +void TypeLocWriter::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { + Record.AddSourceLocation(TL.getExpansionLoc()); +} + void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { Record.AddSourceLocation(TL.getElaboratedKeywordLoc()); Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc()); @@ -1219,6 +1229,7 @@ void ASTWriter::WriteBlockInfoBlock() { RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); RECORD(TYPE_DEPENDENT_SIZED_ARRAY); RECORD(TYPE_PAREN); + RECORD(TYPE_MACRO_QUALIFIED); RECORD(TYPE_PACK_EXPANSION); RECORD(TYPE_ATTRIBUTED); RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); |