Skip to content

Commit e3fe2f7

Browse files
committed
[CIR][NFC] Simplify struct type name creation
During review of a patch for upstreaming the cir.struct type support, Erich Keane observed that the code we use for creating our type name for structures with templates was likely to be error prone. He recommended using getNameForDiagnostic instead. After some experimentation, I found that with a couple of changes to the printing policy, I could get the same strings we currently produce by calling getNameForDiagnostic. This change does that. Erich also pointed out that RecordDecls always have a DeclContext so a few other lines could be eliminated where that was checked.
1 parent 98e8811 commit e3fe2f7

File tree

2 files changed

+5
-23
lines changed

2 files changed

+5
-23
lines changed

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,13 @@ std::string CIRGenTypes::getRecordTypeName(const clang::RecordDecl *recordDecl,
6161

6262
PrintingPolicy policy = recordDecl->getASTContext().getPrintingPolicy();
6363
policy.SuppressInlineNamespace = false;
64+
policy.AlwaysIncludeTypeForTemplateArgument = true;
65+
policy.PrintCanonicalTypes = true;
6466

6567
if (recordDecl->getIdentifier()) {
66-
if (recordDecl->getDeclContext())
67-
recordDecl->printQualifiedName(outStream, policy);
68-
else
69-
recordDecl->printName(outStream, policy);
70-
71-
// Ensure each template specialization has a unique name.
72-
if (auto *templateSpecialization =
73-
llvm::dyn_cast<ClassTemplateSpecializationDecl>(recordDecl)) {
74-
outStream << '<';
75-
const auto args = templateSpecialization->getTemplateArgs().asArray();
76-
const auto printer = [&policy, &outStream](const TemplateArgument &arg) {
77-
/// Print this template argument to the given output stream.
78-
arg.print(policy, outStream, /*IncludeType=*/true);
79-
};
80-
llvm::interleaveComma(args, outStream, printer);
81-
outStream << '>';
82-
}
83-
68+
recordDecl->getNameForDiagnostic(outStream, policy, true);
8469
} else if (auto *typedefNameDecl = recordDecl->getTypedefNameForAnonDecl()) {
85-
if (typedefNameDecl->getDeclContext())
86-
typedefNameDecl->printQualifiedName(outStream, policy);
87-
else
88-
typedefNameDecl->printName(outStream);
70+
typedefNameDecl->printQualifiedName(outStream, policy);
8971
} else {
9072
outStream << Builder.getUniqueAnonRecordName();
9173
}

clang/test/CIR/CodeGen/sourcelocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int s0(int a, int b) {
8484
// LLVM: !llvm.module.flags = !{!2}
8585

8686
// LLVM: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly)
87-
// LLVM: !1 = !DIFile(filename: "sourcelocation.cpp", directory: "/data/users/lanza/Projects/clangir/clang/test/CIR/CodeGen")
87+
// LLVM: !1 = !DIFile(filename: "sourcelocation.cpp", directory: "{{.*}}clang/test/CIR/CodeGen")
8888
// LLVM: !2 = !{i32 2, !"Debug Info Version", i32 3}
8989
// LLVM: !3 = distinct !DISubprogram(name: "_Z2s0ii", linkageName: "_Z2s0ii", scope: !1, file: !1, line: 6, type: !4, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
9090
// LLVM: !4 = !DISubroutineType(cc: DW_CC_normal, types: !5)

0 commit comments

Comments
 (0)