[cfe-commits] r127085 - in /cfe/trunk: include/clang/AST/TypeLoc.h lib/Sema/SemaType.cpp lib/Sema/TreeTransform.h lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp
Douglas Gregor
dgregor at apple.com
Sun Mar 6 10:00:32 PST 2011
On Mar 5, 2011, at 6:42 AM, Abramo Bagnara wrote:
> Author: abramo
> Date: Sat Mar 5 08:42:21 2011
> New Revision: 127085
>
> URL: http://llvm.org/viewvc/llvm-project?rev=127085&view=rev
> Log:
> Improved MemberPointerType source locations.
Very nice. Could you add some tests, either to test/SemaCXX/nested-name-spec-locations.cpp or test/Index/annotation-nested-name-specifier.cpp, so we're sure that we don't lose this location information over time?
Just one comment inline.
- Doug
> Modified:
> cfe/trunk/include/clang/AST/TypeLoc.h
> cfe/trunk/lib/Sema/SemaType.cpp
> cfe/trunk/lib/Sema/TreeTransform.h
> cfe/trunk/lib/Serialization/ASTReader.cpp
> cfe/trunk/lib/Serialization/ASTWriter.cpp
>
> Modified: cfe/trunk/include/clang/AST/TypeLoc.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/TypeLoc.h?rev=127085&r1=127084&r2=127085&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/AST/TypeLoc.h (original)
> +++ cfe/trunk/include/clang/AST/TypeLoc.h Sat Mar 5 08:42:21 2011
> @@ -943,10 +943,14 @@
> }
> };
>
> +struct MemberPointerLocInfo : public PointerLikeLocInfo {
> + TypeSourceInfo *ClassTInfo;
> +};
>
> /// \brief Wrapper for source info for member pointers.
> class MemberPointerTypeLoc : public PointerLikeTypeLoc<MemberPointerTypeLoc,
> - MemberPointerType> {
> + MemberPointerType,
> + MemberPointerLocInfo> {
> public:
> SourceLocation getStarLoc() const {
> return getSigilLoc();
> @@ -954,6 +958,28 @@
> void setStarLoc(SourceLocation Loc) {
> setSigilLoc(Loc);
> }
> +
> + const Type *getClass() const {
> + return getTypePtr()->getClass();
> + }
> + TypeSourceInfo *getClassTInfo() const {
> + return getLocalData()->ClassTInfo;
> + }
> + void setClassTInfo(TypeSourceInfo* TI) {
> + getLocalData()->ClassTInfo = TI;
> + }
> +
> + void initializeLocal(ASTContext &Context, SourceLocation Loc) {
> + setSigilLoc(Loc);
> + setClassTInfo(0);
> + }
> +
> + SourceRange getLocalSourceRange() const {
> + if (TypeSourceInfo *TI = getClassTInfo())
> + return SourceRange(TI->getTypeLoc().getBeginLoc(), getStarLoc());
> + else
> + return SourceRange(getStarLoc());
> + }
> };
>
> /// Wraps an ObjCPointerType with source location information.
>
> Modified: cfe/trunk/lib/Sema/SemaType.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=127085&r1=127084&r2=127085&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaType.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaType.cpp Sat Mar 5 08:42:21 2011
> @@ -2345,10 +2345,12 @@
> };
>
> class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
> + ASTContext &Context;
> const DeclaratorChunk &Chunk;
>
> public:
> - DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
> + DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
> + : Context(Context), Chunk(Chunk) {}
>
> void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
> llvm_unreachable("qualified type locs not expected here!");
> @@ -2368,8 +2370,48 @@
> }
> void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
> assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
> + const CXXScopeSpec& SS = Chunk.Mem.Scope();
> + NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
> +
> + const Type* ClsTy = TL.getClass();
> + QualType ClsQT = QualType(ClsTy, 0);
> + TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
> + // Now copy source location info into the type loc component.
> + TypeLoc ClsTL = ClsTInfo->getTypeLoc();
> + switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
> + case NestedNameSpecifier::Identifier:
> + assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
> + {
> + DependentNameTypeLoc DNTLoc = *cast<DependentNameTypeLoc>(&ClsTL);
FWIW, you can just use
cast<DependentNameTypeLoc>(ClsTL)
here.
> + DNTLoc.setKeywordLoc(SourceLocation());
> + DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
> + DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
> + }
> + break;
> +
> + case NestedNameSpecifier::TypeSpec:
> + case NestedNameSpecifier::TypeSpecWithTemplate:
> + if (isa<ElaboratedType>(ClsTy)) {
> + ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
> + ETLoc.setKeywordLoc(SourceLocation());
> + ETLoc.setQualifierLoc(NNSLoc.getPrefix());
> + TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
> + NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
> + } else {
> + ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
> + }
> + break;
> +
> + case NestedNameSpecifier::Namespace:
> + case NestedNameSpecifier::NamespaceAlias:
> + case NestedNameSpecifier::Global:
> + llvm_unreachable("Nested-name-specifier must name a type");
> + break;
> + }
> +
> + // Finally fill in MemberPointerLocInfo fields.
> TL.setStarLoc(Chunk.Loc);
> - // FIXME: nested name specifier
> + TL.setClassTInfo(ClsTInfo);
> }
> void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
> assert(Chunk.Kind == DeclaratorChunk::Reference);
> @@ -2440,7 +2482,7 @@
> CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
> }
>
> - DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
> + DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
> CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
> }
>
>
> Modified: cfe/trunk/lib/Sema/TreeTransform.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=127085&r1=127084&r2=127085&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/TreeTransform.h (original)
> +++ cfe/trunk/lib/Sema/TreeTransform.h Sat Mar 5 08:42:21 2011
> @@ -3308,23 +3308,34 @@
> QualType
> TreeTransform<Derived>::TransformMemberPointerType(TypeLocBuilder &TLB,
> MemberPointerTypeLoc TL) {
> - const MemberPointerType *T = TL.getTypePtr();
> -
> QualType PointeeType = getDerived().TransformType(TLB, TL.getPointeeLoc());
> if (PointeeType.isNull())
> return QualType();
>
> - // TODO: preserve source information for this.
> - QualType ClassType
> - = getDerived().TransformType(QualType(T->getClass(), 0));
> - if (ClassType.isNull())
> - return QualType();
> + TypeSourceInfo* OldClsTInfo = TL.getClassTInfo();
> + TypeSourceInfo* NewClsTInfo = 0;
> + if (OldClsTInfo) {
> + NewClsTInfo = getDerived().TransformType(OldClsTInfo);
> + if (!NewClsTInfo)
> + return QualType();
> + }
> +
> + const MemberPointerType *T = TL.getTypePtr();
> + QualType OldClsType = QualType(T->getClass(), 0);
> + QualType NewClsType;
> + if (NewClsTInfo)
> + NewClsType = NewClsTInfo->getType();
> + else {
> + NewClsType = getDerived().TransformType(OldClsType);
> + if (NewClsType.isNull())
> + return QualType();
> + }
>
> QualType Result = TL.getType();
> if (getDerived().AlwaysRebuild() ||
> PointeeType != T->getPointeeType() ||
> - ClassType != QualType(T->getClass(), 0)) {
> - Result = getDerived().RebuildMemberPointerType(PointeeType, ClassType,
> + NewClsType != OldClsType) {
> + Result = getDerived().RebuildMemberPointerType(PointeeType, NewClsType,
> TL.getStarLoc());
> if (Result.isNull())
> return QualType();
> @@ -3332,6 +3343,7 @@
>
> MemberPointerTypeLoc NewTL = TLB.push<MemberPointerTypeLoc>(Result);
> NewTL.setSigilLoc(TL.getSigilLoc());
> + NewTL.setClassTInfo(NewClsTInfo);
>
> return Result;
> }
>
> Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=127085&r1=127084&r2=127085&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
> +++ cfe/trunk/lib/Serialization/ASTReader.cpp Sat Mar 5 08:42:21 2011
> @@ -3401,6 +3401,7 @@
> }
> void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
> TL.setStarLoc(ReadSourceLocation(Record, Idx));
> + TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
> }
> void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
> TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
>
> Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=127085&r1=127084&r2=127085&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
> +++ cfe/trunk/lib/Serialization/ASTWriter.cpp Sat Mar 5 08:42:21 2011
> @@ -418,6 +418,7 @@
> }
> void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
> Writer.AddSourceLocation(TL.getStarLoc(), Record);
> + Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
> }
> void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
> Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list