[clang] [clang] Language to String function (PR #69487)

Yusra Syeda via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 26 13:14:52 PDT 2023


https://github.com/ysyeda updated https://github.com/llvm/llvm-project/pull/69487

>From 7214e2ad580724c6b747ce5da126f4e8dbdc825c Mon Sep 17 00:00:00 2001
From: Yusra Syeda <yusra.syeda at ibm.com>
Date: Wed, 18 Oct 2023 13:20:58 -0400
Subject: [PATCH 1/3] add language to string function

---
 clang/include/clang/Basic/LangStandard.h |  1 +
 clang/lib/Basic/LangStandards.cpp        | 40 ++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h
index 6356f16acc811e0..66b2ca0a8397136 100644
--- a/clang/include/clang/Basic/LangStandard.h
+++ b/clang/include/clang/Basic/LangStandard.h
@@ -43,6 +43,7 @@ enum class Language : uint8_t {
   HLSL,
   ///@}
 };
+const char *languageToString(Language L);
 
 enum LangFeatures {
   LineComment = (1 << 0),
diff --git a/clang/lib/Basic/LangStandards.cpp b/clang/lib/Basic/LangStandards.cpp
index af9cf4f273920ee..2c894fdafa1cacb 100644
--- a/clang/lib/Basic/LangStandards.cpp
+++ b/clang/lib/Basic/LangStandards.cpp
@@ -10,9 +10,49 @@
 #include "clang/Config/config.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "llvm/TargetParser/Triple.h"
 using namespace clang;
 
+const char *clang::languageToString(Language L) {
+  // I would like to make this function and the definition of Language
+  // in the .h file simply expand the contents of a .def file.
+  // However, in the .h the members of the enum have doxygen annotations
+  // and/or comments which would be lost.
+  switch (L) {
+  case Language::Unknown:
+    return "Unknown";
+  case Language::Asm:
+    return "Asm";
+  case Language::LLVM_IR:
+    return "LLVM_IR";
+  case Language::C:
+    return "C";
+  case Language::CXX:
+    return "CXX";
+  case Language::ObjC:
+    return "ObjC";
+  case Language::ObjCXX:
+    return "ObjCXX";
+  case Language::OpenCL:
+    return "OpenCL";
+  case Language::OpenCLCXX:
+    return "OpenCLCXX";
+  case Language::CUDA:
+    return "CUDA";
+  case Language::RenderScript:
+    return "RenderScript";
+  case Language::HIP:
+    return "HIP";
+  case Language::HLSL:
+    return "HLSL";
+  }
+
+  std::string msg = llvm::formatv(
+      "Unknown value ({0}) passed to languageToString", (unsigned int)L);
+  llvm_unreachable(msg.c_str());
+}
+
 #define LANGSTANDARD(id, name, lang, desc, features)                           \
   static const LangStandard Lang_##id = {name, desc, features, Language::lang};
 #include "clang/Basic/LangStandards.def"

>From e38d717dd1ca527acbc7ab724ab09b438ed365b6 Mon Sep 17 00:00:00 2001
From: Yusra Syeda <yusra.syeda at ibm.com>
Date: Thu, 26 Oct 2023 13:51:39 -0400
Subject: [PATCH 2/3] address review comments

---
 clang/include/clang/Basic/LangStandard.h |  2 +-
 clang/lib/Basic/LangStandards.cpp        | 13 +++----------
 2 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h
index 66b2ca0a8397136..bc49669a82ad2db 100644
--- a/clang/include/clang/Basic/LangStandard.h
+++ b/clang/include/clang/Basic/LangStandard.h
@@ -43,7 +43,7 @@ enum class Language : uint8_t {
   HLSL,
   ///@}
 };
-const char *languageToString(Language L);
+StringRef languageToString(Language L);
 
 enum LangFeatures {
   LineComment = (1 << 0),
diff --git a/clang/lib/Basic/LangStandards.cpp b/clang/lib/Basic/LangStandards.cpp
index 2c894fdafa1cacb..ade84cf2fa77ec9 100644
--- a/clang/lib/Basic/LangStandards.cpp
+++ b/clang/lib/Basic/LangStandards.cpp
@@ -10,22 +10,17 @@
 #include "clang/Config/config.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/FormatVariadic.h"
 #include "llvm/TargetParser/Triple.h"
 using namespace clang;
 
-const char *clang::languageToString(Language L) {
-  // I would like to make this function and the definition of Language
-  // in the .h file simply expand the contents of a .def file.
-  // However, in the .h the members of the enum have doxygen annotations
-  // and/or comments which would be lost.
+StringRef clang::languageToString(Language L) {
   switch (L) {
   case Language::Unknown:
     return "Unknown";
   case Language::Asm:
     return "Asm";
   case Language::LLVM_IR:
-    return "LLVM_IR";
+    return "LLVM IR";
   case Language::C:
     return "C";
   case Language::CXX:
@@ -48,9 +43,7 @@ const char *clang::languageToString(Language L) {
     return "HLSL";
   }
 
-  std::string msg = llvm::formatv(
-      "Unknown value ({0}) passed to languageToString", (unsigned int)L);
-  llvm_unreachable(msg.c_str());
+  llvm_unreachable("unhandled language kind");
 }
 
 #define LANGSTANDARD(id, name, lang, desc, features)                           \

>From 605f64f2031a4b418b0a5485c0d32ebd28c5e03b Mon Sep 17 00:00:00 2001
From: Yusra Syeda <yusra.syeda at ibm.com>
Date: Thu, 26 Oct 2023 16:14:38 -0400
Subject: [PATCH 3/3] update lang name

---
 clang/lib/Basic/LangStandards.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Basic/LangStandards.cpp b/clang/lib/Basic/LangStandards.cpp
index ade84cf2fa77ec9..ab09c7221dda92f 100644
--- a/clang/lib/Basic/LangStandards.cpp
+++ b/clang/lib/Basic/LangStandards.cpp
@@ -24,15 +24,15 @@ StringRef clang::languageToString(Language L) {
   case Language::C:
     return "C";
   case Language::CXX:
-    return "CXX";
+    return "C++";
   case Language::ObjC:
-    return "ObjC";
+    return "Objective-C";
   case Language::ObjCXX:
-    return "ObjCXX";
+    return "Objective-C++";
   case Language::OpenCL:
     return "OpenCL";
   case Language::OpenCLCXX:
-    return "OpenCLCXX";
+    return "OpenCLC++";
   case Language::CUDA:
     return "CUDA";
   case Language::RenderScript:



More information about the cfe-commits mailing list