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-tidy] Fix readability-duplicate-include for includes with macro #87433

Merged
merged 4 commits into from
Apr 6, 2024

Conversation

FruitClover
Copy link
Contributor

@FruitClover FruitClover commented Apr 2, 2024

Completely skip include directives that form the filename using macros.

fixes #87303

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 3, 2024

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

@llvm/pr-subscribers-clang-tidy

Author: Mike (FruitClover)

Changes

Skip hint creation for include directives that form the filename using macros.

fixes #87303


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

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp (+3)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include.cpp (+30)
diff --git a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
index 67147164946ab4..7dcba2489e0fe6 100644
--- a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp
@@ -18,6 +18,9 @@ namespace clang::tidy::readability {
 static SourceLocation advanceBeyondCurrentLine(const SourceManager &SM,
                                                SourceLocation Start,
                                                int Offset) {
+  // Keep macro location as it is
+  if (Start.isMacroID())
+    return Start;
   const FileID Id = SM.getFileID(Start);
   const unsigned LineNumber = SM.getSpellingLineNumber(Start);
   while (SM.getFileID(Start) == Id &&
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include.cpp
index dd954c705514fb..5ee173bdd27aa2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/duplicate-include.cpp
@@ -70,3 +70,33 @@ int r;
 // CHECK-FIXES:      {{^int q;$}}
 // CHECK-FIXES-NEXT: {{^#include <sys/types.h>$}}
 // CHECK-FIXES-NEXT: {{^int r;$}}
+
+namespace Issue_87303 {
+// Include name from macro
+#define MACRO_FILENAME "duplicate-include.h"
+int a;
+#include MACRO_FILENAME
+int b;
+#include "duplicate-include.h"
+int c;
+// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include
+// CHECK-FIXES:      {{^int a;$}}
+// CHECK-FIXES-NEXT: {{^#include MACRO_FILENAME$}}
+// CHECK-FIXES-NEXT: {{^int b;$}}
+// CHECK-FIXES-NEXT: {{^int c;$}}
+
+// Keep macro
+#define MACRO_FILENAME_2 <duplicate-include2.h>
+int d;
+#include <duplicate-include2.h>
+int e;
+#include MACRO_FILENAME_2
+int f;
+// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: duplicate include
+// CHECK-FIXES:      {{^int d;$}}
+// CHECK-FIXES-NEXT: {{^#include <duplicate-include2.h>$}}
+// CHECK-FIXES-NEXT: {{^int e;$}}
+// CHECK-FIXES-NEXT: {{^#include MACRO_FILENAME_2$}}
+// CHECK-FIXES-NEXT: {{^int f;$}}
+
+} // Issue_87303

@EugeneZelenko
Copy link
Contributor

Please mention fix in Release Notes.

@FruitClover
Copy link
Contributor Author

Please mention fix in Release Notes.

Added

@FruitClover FruitClover force-pushed the fix87303 branch 2 times, most recently from 841c296 to 579acdc Compare April 4, 2024 03:54
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.

LGTM, but to be honest I would probably just exclude includes behind macros from a check completely. Still can be this way.

@FruitClover
Copy link
Contributor Author

LGTM, but to be honest I would probably just exclude includes behind macros from a check completely. Still can be this way.

makes sense, agreed, PTAL

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.

LGTM

@FruitClover FruitClover merged commit ed4e505 into llvm:main Apr 6, 2024
5 checks passed
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.

tidy crashes with readability-duplicate-include
5 participants