Skip to content

Commit 4f06d97

Browse files
committed
[TableGen] Fix source location for anonymous records
Fix source location for anonymous to be the one of the lines where that record is instantiated as opposed to the location of the class that was anonymously instantiated. Current code tags the location of the class on the record that is created when a class is anonymously instantiated via `VarDefInit::instantiate`. Instead, pass in the `SMLoc` for the place where the anonymous instantiation happens and use as the location when the record is instantiated. If there are multiple instantiations with the same paramaters, the location for the record created will be one of these instantiation locations as opposed to the class location. Added unit test using detailed record printer. It only prints the base name of the file when printing locations, so added support for `%basename_s` in LLVM's LIT testing framework to get the base name of the source file.
1 parent 45ad1ac commit 4f06d97

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

llvm/docs/CommandGuide/lit.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ TestRunner.py:
535535
%{fs-tmp-root} root component of file system paths pointing to the test's temporary directory
536536
%{fs-sep} file system path separator
537537
%t temporary file name unique to the test
538+
%basename_s The last path component of %s
538539
%basename_t The last path component of %t but without the ``.tmp`` extension
539540
%T parent directory of %t (not unique, deprecated, do not use)
540541
%% %

llvm/include/llvm/TableGen/Record.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,11 +1347,12 @@ class DefInit : public TypedInit {
13471347
class VarDefInit final : public TypedInit,
13481348
public FoldingSetNode,
13491349
public TrailingObjects<VarDefInit, ArgumentInit *> {
1350+
SMLoc Loc;
13501351
Record *Class;
13511352
DefInit *Def = nullptr; // after instantiation
13521353
unsigned NumArgs;
13531354

1354-
explicit VarDefInit(Record *Class, unsigned N);
1355+
explicit VarDefInit(SMLoc Loc, Record *Class, unsigned N);
13551356

13561357
DefInit *instantiate();
13571358

@@ -1365,7 +1366,8 @@ class VarDefInit final : public TypedInit,
13651366
static bool classof(const Init *I) {
13661367
return I->getKind() == IK_VarDefInit;
13671368
}
1368-
static VarDefInit *get(Record *Class, ArrayRef<ArgumentInit *> Args);
1369+
static VarDefInit *get(SMLoc Loc, Record *Class,
1370+
ArrayRef<ArgumentInit *> Args);
13691371

13701372
void Profile(FoldingSetNodeID &ID) const;
13711373

llvm/lib/TableGen/Record.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,11 +2292,12 @@ static void ProfileVarDefInit(FoldingSetNodeID &ID, Record *Class,
22922292
ID.AddPointer(I);
22932293
}
22942294

2295-
VarDefInit::VarDefInit(Record *Class, unsigned N)
2296-
: TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class),
2295+
VarDefInit::VarDefInit(SMLoc Loc, Record *Class, unsigned N)
2296+
: TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class),
22972297
NumArgs(N) {}
22982298

2299-
VarDefInit *VarDefInit::get(Record *Class, ArrayRef<ArgumentInit *> Args) {
2299+
VarDefInit *VarDefInit::get(SMLoc Loc, Record *Class,
2300+
ArrayRef<ArgumentInit *> Args) {
23002301
FoldingSetNodeID ID;
23012302
ProfileVarDefInit(ID, Class, Args);
23022303

@@ -2307,7 +2308,7 @@ VarDefInit *VarDefInit::get(Record *Class, ArrayRef<ArgumentInit *> Args) {
23072308

23082309
void *Mem = RK.Allocator.Allocate(
23092310
totalSizeToAlloc<ArgumentInit *>(Args.size()), alignof(VarDefInit));
2310-
VarDefInit *I = new (Mem) VarDefInit(Class, Args.size());
2311+
VarDefInit *I = new (Mem) VarDefInit(Loc, Class, Args.size());
23112312
std::uninitialized_copy(Args.begin(), Args.end(),
23122313
I->getTrailingObjects<ArgumentInit *>());
23132314
RK.TheVarDefInitPool.InsertNode(I, IP);
@@ -2323,9 +2324,8 @@ DefInit *VarDefInit::instantiate() {
23232324
return Def;
23242325

23252326
RecordKeeper &Records = Class->getRecords();
2326-
auto NewRecOwner =
2327-
std::make_unique<Record>(Records.getNewAnonymousName(), Class->getLoc(),
2328-
Records, Record::RK_AnonymousDef);
2327+
auto NewRecOwner = std::make_unique<Record>(
2328+
Records.getNewAnonymousName(), Loc, Records, Record::RK_AnonymousDef);
23292329
Record *NewRec = NewRecOwner.get();
23302330

23312331
// Copy values from class to instance
@@ -2389,7 +2389,7 @@ Init *VarDefInit::resolveReferences(Resolver &R) const {
23892389
}
23902390

23912391
if (Changed) {
2392-
auto New = VarDefInit::get(Class, NewArgs);
2392+
auto New = VarDefInit::get(Loc, Class, NewArgs);
23932393
if (!UR.foundUnresolved())
23942394
return New->instantiate();
23952395
return New;

llvm/lib/TableGen/TGParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2719,7 +2719,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, const RecTy *ItemType,
27192719

27202720
if (TrackReferenceLocs)
27212721
Class->appendReferenceLoc(NameLoc);
2722-
return VarDefInit::get(Class, Args)->Fold();
2722+
return VarDefInit::get(NameLoc.Start, Class, Args)->Fold();
27232723
}
27242724
case tgtok::l_brace: { // Value ::= '{' ValueList '}'
27252725
SMLoc BraceLoc = Lex.getLoc();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: llvm-tblgen --print-detailed-records %s | FileCheck %s -DFILE=%basename_s
2+
3+
class A<int a> {
4+
int Num = a;
5+
}
6+
7+
// Verify that the location of the anonymous record instantiated
8+
// for A<10> and A<11> is correct. It should show the line where the
9+
// anonymous record was instantiated and not the line where the class
10+
// was defined.
11+
def y {
12+
// CHECK: anonymous_0 |[[FILE]]:[[@LINE+1]]|
13+
int x = A<10>.Num;
14+
// CHECK: anonymous_1 |[[FILE]]:[[@LINE+1]]|
15+
int y = A<11>.Num;
16+
}

llvm/utils/lit/lit/TestRunner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,10 +1394,12 @@ def getDefaultSubstitutions(test, tmpDir, tmpBase, normalize_slashes=False):
13941394
substitutions = []
13951395
substitutions.extend(test.config.substitutions)
13961396
tmpName = tmpBase + ".tmp"
1397-
baseName = os.path.basename(tmpBase)
1397+
tmpBaseName = os.path.basename(tmpBase)
1398+
srcBaseName = os.path.basename(sourcepath)
13981399

13991400
substitutions.append(("%{pathsep}", os.pathsep))
1400-
substitutions.append(("%basename_t", baseName))
1401+
substitutions.append(("%basename_t", tmpBaseName))
1402+
substitutions.append(("%basename_s", srcBaseName))
14011403

14021404
substitutions.extend(
14031405
[

0 commit comments

Comments
 (0)