Skip to content

[clang-tidy][misc-use-internal-linkage] fix false positives for function or variable in header file which contains macro expansion #129594

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 1 commit into from
Mar 8, 2025

Conversation

HerrCai0907
Copy link
Contributor

When check whether in main file, spelling loc will lead to <scratch space>. instead, expansion loc is close to loc after preprocess. It is suitable to analyze linkage.

…le which contains macro expansion

When check whether in main file, spelling loc will lead to `<scratch space>`. instead, expansion loc is close to loc after preprocess. It is suitable to analyze linkage.
@llvmbot
Copy link
Member

llvmbot commented Mar 3, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

When check whether in main file, spelling loc will lead to &lt;scratch space&gt;. instead, expansion loc is close to loc after preprocess. It is suitable to analyze linkage.


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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp (+1-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (added) clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-macro.hpp (+5)
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 4778182944abd..a1a20c0782230 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -52,7 +52,7 @@ AST_MATCHER(FunctionDecl, hasBody) { return Node.hasBody(); }
 static bool isInMainFile(SourceLocation L, SourceManager &SM,
                          const FileExtensionsSet &HeaderFileExtensions) {
   for (;;) {
-    if (utils::isSpellingLocInHeaderFile(L, SM, HeaderFileExtensions))
+    if (utils::isExpansionLocInHeaderFile(L, SM, HeaderFileExtensions))
       return false;
     if (SM.isInMainFile(L))
       return true;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 71edb704b49d6..43744315559eb 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -129,6 +129,10 @@ Changes in existing checks
   <clang-tidy/checks/misc/redundant-expression>` check by providing additional
   examples and fixing some macro related false positives.
 
+- Improved :doc:`misc-use-internal-linkage
+  <clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
+  for function or variable in header file which contains macro expansion.
+
 - Improved :doc:`performance/unnecessary-value-param
   <clang-tidy/checks/performance/unnecessary-value-param>` check performance by
   tolerating fix-it breaking compilation when functions is used as pointers 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-macro.hpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-macro.hpp
new file mode 100644
index 0000000000000..209bd56028c3d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-macro.hpp
@@ -0,0 +1,5 @@
+// RUN: %check_clang_tidy %s misc-use-internal-linkage %t -- -- -I%S/Inputs/use-internal-linkage
+
+#define B A##C
+
+inline void B() {}

@HerrCai0907 HerrCai0907 changed the title [clang-tidy]fix false positives for function or variable in header file which contains macro expansion [clang-tidy][misc-use-internal-linkage] fix false positives for function or variable in header file which contains macro expansion Mar 8, 2025
@HerrCai0907 HerrCai0907 merged commit 6f7570c into llvm:main Mar 8, 2025
15 checks passed
@HerrCai0907 HerrCai0907 deleted the fix/misc-use-internal-linkage branch March 8, 2025 05: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.

2 participants