Skip to content

Conversation

@camc
Copy link
Contributor

@camc camc commented Nov 21, 2025

Fixes #168901

Adds a err_attribute_string_literal_invalid_ident diagnostic and emits it when a gnu::abi_tag attribute has a string argument with an invalid identifier.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2025

@llvm/pr-subscribers-clang

Author: None (camc)

Changes

Fixes #168901

Adds a err_attribute_string_literal_invalid_ident diagnostic and emits it when a gnu::abi_tag attribute has a string argument with an invalid identifier.


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

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+17)
  • (modified) clang/test/SemaCXX/attr-abi-tag-syntax.cpp (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 62c8c0130c3d0..9a5efe97d0595 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -449,6 +449,8 @@ Improvements to Clang's diagnostics
 - A new warning ``-Wenum-compare-typo`` has been added to detect potential erroneous
   comparison operators when mixed with bitwise operators in enum value initializers.
   This can be locally disabled by explicitly casting the initializer value.
+- Clang now emits a diagnostic when an invalid identifier string is passed to
+  the `gnu::abi_tag` attribute (#GH168901).
 
 Improvements to Clang's time-trace
 ----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 53aa86a7dabde..ce52066d38957 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3326,6 +3326,8 @@ def err_attribute_no_member_function : Error<
 def err_attribute_parameter_types : Error<
   "%0 attribute parameter types do not match: parameter %1 of function %2 has type %3, "
   "but parameter %4 of function %5 has type %6">;
+def err_attribute_string_literal_invalid_ident : Error<
+  "%0 attribute parameters must be string literals containing valid identifiers">;
 
 def err_attribute_too_many_arguments : Error<
   "%0 attribute takes no more than %1 argument%s1">;
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index e3af5023c74d0..bd941f0aa05f3 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6172,12 +6172,29 @@ static void handleMSConstexprAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   D->addAttr(::new (S.Context) MSConstexprAttr(S.Context, AL));
 }
 
+static bool checkIsRawIdentifier(const Sema &S, StringRef StrRef) {
+  std::string Str = StrRef.str();
+  auto FileLoc = S.getSourceManager().getLocForStartOfFile(
+      S.getSourceManager().getMainFileID());
+  Lexer Lex(FileLoc, S.getLangOpts(), Str.c_str(), Str.c_str(),
+      Str.c_str() + Str.size());
+  Token Tok;
+  bool LexedToEnd = Lex.LexFromRawLexer(Tok);
+  return LexedToEnd && Tok.is(tok::raw_identifier);
+}
+
 static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   SmallVector<StringRef, 4> Tags;
   for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
     StringRef Tag;
     if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
       return;
+    if (!checkIsRawIdentifier(S, Tag)) {
+      S.Diag(AL.getArgAsExpr(I)->getBeginLoc(),
+             diag::err_attribute_string_literal_invalid_ident)
+          << AL;
+      return;
+    }
     Tags.push_back(Tag);
   }
 
diff --git a/clang/test/SemaCXX/attr-abi-tag-syntax.cpp b/clang/test/SemaCXX/attr-abi-tag-syntax.cpp
index 4f14a3c043b5c..ea3f5b184aba2 100644
--- a/clang/test/SemaCXX/attr-abi-tag-syntax.cpp
+++ b/clang/test/SemaCXX/attr-abi-tag-syntax.cpp
@@ -31,3 +31,13 @@ extern int a2;
 // expected-note@-1 {{previous declaration is here}}
 __attribute__((abi_tag("A")))extern int a2;
 // expected-error@-1 {{cannot add 'abi_tag' attribute in a redeclaration}}
+
+[[gnu::abi_tag("[[something]]")]] // expected-error {{'gnu::abi_tag' attribute parameters must be string literals containing valid identifiers}}
+int f1() { 
+	return 0;
+}
+
+[[gnu::abi_tag("something")]]
+int f2() { 
+	return 0;
+}

@github-actions
Copy link

github-actions bot commented Nov 21, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 91137 tests passed
  • 2416 tests skipped
  • 2 tests failed

Failed Tests

(click on a test name to see its output)

ORC-x86_64-linux

ORC-x86_64-linux.TestCases/Linux/x86-64/lljit-ehframe.cpp (Likely Already Failing) This test is already failing at the base commit.
Exit Code: -9
Timeout: Reached timeout of 1200 seconds

Command Output (stderr):
--
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/clang  --driver-mode=g++  -m64  -fPIC -emit-llvm -c -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-bins/compiler-rt/test/orc/X86_64LinuxConfig/TestCases/Linux/x86-64/Output/lljit-ehframe.cpp.tmp /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-ehframe.cpp # RUN: at line 1
+ /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/clang --driver-mode=g++ -m64 -fPIC -emit-llvm -c -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-bins/compiler-rt/test/orc/X86_64LinuxConfig/TestCases/Linux/x86-64/Output/lljit-ehframe.cpp.tmp /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-ehframe.cpp
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/lli -jit-kind=orc -jit-linker=jitlink -orc-runtime=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib/../lib/clang/22/lib/x86_64-unknown-linux-gnu/liborc_rt.a -relocation-model=pic /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-bins/compiler-rt/test/orc/X86_64LinuxConfig/TestCases/Linux/x86-64/Output/lljit-ehframe.cpp.tmp | FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-ehframe.cpp # RUN: at line 2
+ /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/lli -jit-kind=orc -jit-linker=jitlink -orc-runtime=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib/../lib/clang/22/lib/x86_64-unknown-linux-gnu/liborc_rt.a -relocation-model=pic /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-bins/compiler-rt/test/orc/X86_64LinuxConfig/TestCases/Linux/x86-64/Output/lljit-ehframe.cpp.tmp
+ FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-ehframe.cpp
JIT session error: Invalid arch in ELF object file: 0

--

ORC-x86_64-linux.TestCases/Linux/x86-64/lljit-initialize-deinitialize.ll (Likely Already Failing) This test is already failing at the base commit.
Exit Code: -9
Timeout: Reached timeout of 1200 seconds

Command Output (stderr):
--
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/lli -jit-kind=orc -jit-linker=jitlink -orc-runtime=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib/../lib/clang/22/lib/x86_64-unknown-linux-gnu/liborc_rt.a /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-initialize-deinitialize.ll | FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-initialize-deinitialize.ll # RUN: at line 1
+ /home/gha/actions-runner/_work/llvm-project/llvm-project/build/./bin/lli -jit-kind=orc -jit-linker=jitlink -orc-runtime=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/./lib/../lib/clang/22/lib/x86_64-unknown-linux-gnu/liborc_rt.a /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-initialize-deinitialize.ll
+ FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/compiler-rt/test/orc/TestCases/Linux/x86-64/lljit-initialize-deinitialize.ll
JIT session error: Invalid arch in ELF object file: 0

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

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.

[Clang] Clang accepts NonIdentifier string in the gnu::abi_tag attribute

2 participants