diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h index 106fee39cb9f6..ed9855e0180eb 100644 --- a/llvm/include/llvm/TableGen/Record.h +++ b/llvm/include/llvm/TableGen/Record.h @@ -1347,11 +1347,12 @@ class DefInit : public TypedInit { class VarDefInit final : public TypedInit, public FoldingSetNode, public TrailingObjects { + SMLoc Loc; Record *Class; DefInit *Def = nullptr; // after instantiation unsigned NumArgs; - explicit VarDefInit(Record *Class, unsigned N); + explicit VarDefInit(SMLoc Loc, Record *Class, unsigned N); DefInit *instantiate(); @@ -1365,7 +1366,8 @@ class VarDefInit final : public TypedInit, static bool classof(const Init *I) { return I->getKind() == IK_VarDefInit; } - static VarDefInit *get(Record *Class, ArrayRef Args); + static VarDefInit *get(SMLoc Loc, Record *Class, + ArrayRef Args); void Profile(FoldingSetNodeID &ID) const; diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index c0c89836171b1..cf8b3165a4e0f 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -2292,11 +2292,12 @@ static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class, ID.AddPointer(I); } -VarDefInit::VarDefInit(Record *Class, unsigned N) - : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), +VarDefInit::VarDefInit(SMLoc Loc, Record *Class, unsigned N) + : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class), NumArgs(N) {} -VarDefInit *VarDefInit::get(Record *Class, ArrayRef Args) { +VarDefInit *VarDefInit::get(SMLoc Loc, Record *Class, + ArrayRef Args) { FoldingSetNodeID ID; ProfileVarDefInit(ID, Class, Args); @@ -2307,7 +2308,7 @@ VarDefInit *VarDefInit::get(Record *Class, ArrayRef Args) { void *Mem = RK.Allocator.Allocate( totalSizeToAlloc(Args.size()), alignof(VarDefInit)); - VarDefInit *I = new (Mem) VarDefInit(Class, Args.size()); + VarDefInit *I = new (Mem) VarDefInit(Loc, Class, Args.size()); std::uninitialized_copy(Args.begin(), Args.end(), I->getTrailingObjects()); RK.TheVarDefInitPool.InsertNode(I, IP); @@ -2323,9 +2324,8 @@ DefInit *VarDefInit::instantiate() { return Def; RecordKeeper &Records = Class->getRecords(); - auto NewRecOwner = - std::make_unique(Records.getNewAnonymousName(), Class->getLoc(), - Records, Record::RK_AnonymousDef); + auto NewRecOwner = std::make_unique( + Records.getNewAnonymousName(), Loc, Records, Record::RK_AnonymousDef); Record *NewRec = NewRecOwner.get(); // Copy values from class to instance @@ -2389,7 +2389,7 @@ Init *VarDefInit::resolveReferences(Resolver &R) const { } if (Changed) { - auto New = VarDefInit::get(Class, NewArgs); + auto *New = VarDefInit::get(Loc, Class, NewArgs); if (!UR.foundUnresolved()) return New->instantiate(); return New; diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp index 6793205e09380..2df84742c73b9 100644 --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -2719,7 +2719,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, const RecTy *ItemType, if (TrackReferenceLocs) Class->appendReferenceLoc(NameLoc); - return VarDefInit::get(Class, Args)->Fold(); + return VarDefInit::get(NameLoc.Start, Class, Args)->Fold(); } case tgtok::l_brace: { // Value ::= '{' ValueList '}' SMLoc BraceLoc = Lex.getLoc(); diff --git a/llvm/test/TableGen/anonymous-location.td b/llvm/test/TableGen/anonymous-location.td new file mode 100644 index 0000000000000..ffeba6ebcb686 --- /dev/null +++ b/llvm/test/TableGen/anonymous-location.td @@ -0,0 +1,16 @@ +// RUN: llvm-tblgen --print-detailed-records %s | FileCheck %s -DFILE=anonymous-location.td + +class A { + int Num = a; +} + +// Verify that the location of the anonymous record instantiated +// for A<10> and A<11> is correct. It should show the line where the +// anonymous record was instantiated and not the line where the class +// was defined. +def y { + // CHECK: anonymous_0 |[[FILE]]:[[@LINE+1]]| + int x = A<10>.Num; + // CHECK: anonymous_1 |[[FILE]]:[[@LINE+1]]| + int y = A<11>.Num; +}