-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Conversation
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Mike (FruitClover) ChangesSkip 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:
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
|
Please mention fix in Release Notes. |
Added |
Skip hint creation for include directives that form the filename using macros.
841c296
to
579acdc
Compare
There was a problem hiding this 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.
makes sense, agreed, PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Completely skip include directives that form the filename using macros.
fixes #87303