Skip to content

Fix #35272: Don't replace typedefs in extern c scope #69102

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

Merged
merged 10 commits into from
Dec 26, 2023

Conversation

da-viper
Copy link
Contributor

Fixes #35272

@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2023

@llvm/pr-subscribers-clang-tidy

Author: None (Da-Viper)

Changes

Fixes #35272


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

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp (+12-4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp (+14)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index e6293ed48bfddbb..841ffb4c9bfe66e 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -11,6 +11,12 @@
 #include "clang/Lex/Lexer.h"
 
 using namespace clang::ast_matchers;
+namespace {
+
+AST_MATCHER(clang::LinkageSpecDecl, isExternCLinkage) {
+  return Node.getLanguage() == clang::LinkageSpecDecl::lang_c;
+}
+} // namespace
 
 namespace clang::tidy::modernize {
 
@@ -27,10 +33,12 @@ void UseUsingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(typedefDecl(unless(isInstantiated()),
-                                 hasParent(decl().bind(ParentDeclName)))
-                         .bind(TypedefName),
-                     this);
+  Finder->addMatcher(
+      typedefDecl(unless(anyOf(isInstantiated(), hasAncestor(linkageSpecDecl(
+                                                     isExternCLinkage())))),
+                  hasParent(decl().bind(ParentDeclName)))
+          .bind(TypedefName),
+      this);
 
   // This matcher is used to find tag declarations in source code within
   // typedefs. They appear in the AST just *prior* to the typedefs.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 422abee11a71962..0f8f14502d5ca3c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -325,3 +325,17 @@ typedef bool (*ISSUE_65055_2)(int);
 typedef class ISSUE_67529_1 *ISSUE_67529;
 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
 // CHECK-FIXES: using ISSUE_67529 = class ISSUE_67529_1 *;
+
+// Some Header
+extern "C" {
+
+typedef int InExternC;
+}
+
+extern "C++" {
+
+typedef int InExternCPP;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' [modernize-use-using]
+// CHECK-FIXES: using InExternCPP = int;
+
+}

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

please update release note and doc

@PiotrZSL
Copy link
Member

I got mixed feelings about this. Simply we can use 'using' within extern "C", there is nothing wrong with this. using/typedef doesn't haven linkage, that just type alias. And extern "C" does not impact it. In oryginal issue I think someone mistook extern "C" into something where only "C" code should be put, and that's not true. All what extern "C" does it's using C style symbol mangling for functions inside.

The only reason to not use 'using' in extern 'C' would be just readability, to avoid some confusion.
As for this change, release notes & doc is needed.

@da-viper
Copy link
Contributor Author

da-viper commented Oct 15, 2023 via email

@PiotrZSL
Copy link
Member

PiotrZSL commented Oct 15, 2023

  1. where do I find the release notes.

clang-tools-extra/docs/ReleaseNotes.rst

Update ReleaseNotes.rst with the changes made
@da-viper da-viper force-pushed the #35272_dont_change_code_in_extern_c branch from 37e50e9 to 521dec9 Compare October 16, 2023 22:37
Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

Loooks, good, minor nit in release notes.

@PiotrZSL PiotrZSL merged commit 9dcc665 into llvm:main Dec 26, 2023
PiotrZSL added a commit that referenced this pull request Dec 26, 2023
PiotrZSL pushed a commit that referenced this pull request Dec 26, 2023
Added IgnoreExternC option to modernize-use-using check.
Fixes #35272
PiotrZSL added a commit that referenced this pull request Dec 26, 2023
@da-viper da-viper deleted the #35272_dont_change_code_in_extern_c branch March 20, 2025 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

modernize-use-using: don't change code in extern "C" {}
4 participants