Skip to content

Commit 8f7c7d5

Browse files
committed
[llvm][Dwarf] Add LanguageDescription API that accounts for version
Currently `llvm::dwarf::LanguageDescription` returns a stringified `DW_LNAME`. It would be useful to have an API that returns the language name for a particular `DW_LNAME_`/version pair. LLDB's use case is that it wants to display a human readable description of the language we got from debug-info in diagnostics. We could maintain a side-table in LLDB but though this might generally be useful to live next to the `LanguageDescription` API. (cherry picked from commit 544d861)
1 parent 162b87b commit 8f7c7d5

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,15 @@ toDW_LNAME(SourceLanguage language) {
500500
return {};
501501
}
502502

503+
/// Returns a version-independent language name.
503504
LLVM_ABI llvm::StringRef LanguageDescription(SourceLanguageName name);
504505

506+
/// Returns a language name corresponding to the specified version.
507+
/// If the version is not recognized for the specified language, returns
508+
/// the version-independent name.
509+
LLVM_ABI llvm::StringRef LanguageDescription(SourceLanguageName Name,
510+
uint32_t Version);
511+
505512
inline bool isCPlusPlus(SourceLanguage S) {
506513
bool result = false;
507514
// Deliberately enumerate all the language options so we get a warning when

llvm/lib/BinaryFormat/Dwarf.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,117 @@ StringRef llvm::dwarf::LanguageDescription(dwarf::SourceLanguageName lname) {
472472
return "Unknown";
473473
}
474474

475+
StringRef llvm::dwarf::LanguageDescription(dwarf::SourceLanguageName Name,
476+
uint32_t Version) {
477+
switch (Name) {
478+
// YYYY
479+
case DW_LNAME_Ada: {
480+
if (Version <= 1983)
481+
return "Ada 83";
482+
if (Version <= 1995)
483+
return "Ada 95";
484+
if (Version <= 2005)
485+
return "Ada 2005";
486+
if (Version <= 2012)
487+
return "Ada 2012";
488+
} break;
489+
490+
case DW_LNAME_Cobol: {
491+
if (Version <= 1974)
492+
return "COBOL-74";
493+
if (Version <= 1985)
494+
return "COBOL-85";
495+
} break;
496+
497+
case DW_LNAME_Fortran: {
498+
if (Version <= 1977)
499+
return "FORTRAN 77";
500+
if (Version <= 1990)
501+
return "FORTRAN 90";
502+
if (Version <= 1995)
503+
return "Fortran 95";
504+
if (Version <= 2003)
505+
return "Fortran 2003";
506+
if (Version <= 2008)
507+
return "Fortran 2008";
508+
if (Version <= 2018)
509+
return "Fortran 2018";
510+
} break;
511+
512+
// YYYYMM
513+
case DW_LNAME_C: {
514+
if (Version == 0)
515+
break;
516+
if (Version <= 198912)
517+
return "C89";
518+
if (Version <= 199901)
519+
return "C99";
520+
if (Version <= 201112)
521+
return "C11";
522+
if (Version <= 201710)
523+
return "C17";
524+
} break;
525+
526+
case DW_LNAME_C_plus_plus: {
527+
if (Version == 0)
528+
break;
529+
if (Version <= 199711)
530+
return "C++98";
531+
if (Version <= 200310)
532+
return "C++03";
533+
if (Version <= 201103)
534+
return "C++11";
535+
if (Version <= 201402)
536+
return "C++14";
537+
if (Version <= 201703)
538+
return "C++17";
539+
if (Version <= 202002)
540+
return "C++20";
541+
} break;
542+
543+
case DW_LNAME_ObjC_plus_plus:
544+
case DW_LNAME_ObjC:
545+
case DW_LNAME_Move:
546+
case DW_LNAME_SYCL:
547+
case DW_LNAME_BLISS:
548+
case DW_LNAME_Crystal:
549+
case DW_LNAME_D:
550+
case DW_LNAME_Dylan:
551+
case DW_LNAME_Go:
552+
case DW_LNAME_Haskell:
553+
case DW_LNAME_HLSL:
554+
case DW_LNAME_Java:
555+
case DW_LNAME_Julia:
556+
case DW_LNAME_Kotlin:
557+
case DW_LNAME_Modula2:
558+
case DW_LNAME_Modula3:
559+
case DW_LNAME_OCaml:
560+
case DW_LNAME_OpenCL_C:
561+
case DW_LNAME_Pascal:
562+
case DW_LNAME_PLI:
563+
case DW_LNAME_Python:
564+
case DW_LNAME_RenderScript:
565+
case DW_LNAME_Rust:
566+
case DW_LNAME_Swift:
567+
case DW_LNAME_UPC:
568+
case DW_LNAME_Zig:
569+
case DW_LNAME_Assembly:
570+
case DW_LNAME_C_sharp:
571+
case DW_LNAME_Mojo:
572+
case DW_LNAME_GLSL:
573+
case DW_LNAME_GLSL_ES:
574+
case DW_LNAME_OpenCL_CPP:
575+
case DW_LNAME_CPP_for_OpenCL:
576+
case DW_LNAME_Ruby:
577+
case DW_LNAME_Hylo:
578+
case DW_LNAME_Metal:
579+
break;
580+
}
581+
582+
// Fallback to un-versioned name.
583+
return LanguageDescription(Name);
584+
}
585+
475586
StringRef llvm::dwarf::CaseString(unsigned Case) {
476587
switch (Case) {
477588
case DW_ID_case_sensitive:

0 commit comments

Comments
 (0)