Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang] Language to String function #69487

Merged
merged 3 commits into from
Oct 27, 2023
Merged

[clang] Language to String function #69487

merged 3 commits into from
Oct 27, 2023

Conversation

ysyeda
Copy link
Contributor

@ysyeda ysyeda commented Oct 18, 2023

This PR adds a function which converts the language to string. This is intended to be used by the z/OS target, see the patch here: #68926

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 18, 2023
@llvmbot
Copy link

llvmbot commented Oct 18, 2023

@llvm/pr-subscribers-clang

Author: Yusra Syeda (ysyeda)

Changes

This PR adds a function which converts the language to string.


Full diff: https://github.com/llvm/llvm-project/pull/69487.diff

2 Files Affected:

  • (modified) clang/include/clang/Basic/LangStandard.h (+1)
  • (modified) clang/lib/Basic/LangStandards.cpp (+40)
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"

@ysyeda ysyeda requested a review from muiez October 18, 2023 17:25
Copy link
Member

@muiez muiez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shafik
Copy link
Collaborator

shafik commented Oct 26, 2023

What is the intended use for this?

@MaskRay
Copy link
Member

MaskRay commented Oct 26, 2023

same question: if a dependent patch uses languageToString, please link to it.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with others; what's the in-tree use case for this?

@uweigand
Copy link
Member

This is intended to be used by the z/OS target, see the patch here: #68926

I asked Yusra to pull this part out into a separate PR.

@AaronBallman
Copy link
Collaborator

This is intended to be used by the z/OS target, see the patch here: #68926

I asked Yusra to pull this part out into a separate PR.

Thank you for the context!

clang/lib/Basic/LangStandards.cpp Outdated Show resolved Hide resolved
clang/lib/Basic/LangStandards.cpp Outdated Show resolved Hide resolved
clang/include/clang/Basic/LangStandard.h Outdated Show resolved Hide resolved
clang/lib/Basic/LangStandards.cpp Outdated Show resolved Hide resolved
clang/lib/Basic/LangStandards.cpp Outdated Show resolved Hide resolved
clang/lib/Basic/LangStandards.cpp Outdated Show resolved Hide resolved
clang/lib/Basic/LangStandards.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@MaskRay
Copy link
Member

MaskRay commented Oct 27, 2023

This is intended to be used by the z/OS target, see the patch here: #68926

Can the description (first comment) #69487 (comment) be amended to link to this PR?

@ysyeda ysyeda merged commit 703895b into llvm:main Oct 27, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants