Skip to content

Commit

Permalink
[clang-tidy] Don't emit the whole spelling include header in include-…
Browse files Browse the repository at this point in the history
…cleaner diagnostic message

To keep the message short and consistent with clangd, and the diagnostics are
attached to the #include line, users have enough context to understand the whole #include.

Differential Revision: https://reviews.llvm.org/D154434
  • Loading branch information
hokein committed Jul 4, 2023
1 parent 7e48c2d commit 2444fb9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include <optional>
#include <string>
Expand Down Expand Up @@ -171,7 +172,8 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {

for (const auto *Inc : Unused) {
diag(Inc->HashLocation, "included header %0 is not used directly")
<< Inc->quote()
<< llvm::sys::path::filename(Inc->Spelled,
llvm::sys::path::Style::posix)
<< FixItHint::CreateRemoval(CharSourceRange::getCharRange(
SM->translateLineCol(SM->getMainFileID(), Inc->Line, 1),
SM->translateLineCol(SM->getMainFileID(), Inc->Line + 1, 1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "bar.h"
// CHECK-FIXES: {{^}}#include "baz.h"{{$}}
#include "foo.h"
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: included header "foo.h" is not used directly [misc-include-cleaner]
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: included header foo.h is not used directly [misc-include-cleaner]
// CHECK-FIXES: {{^}}
// CHECK-FIXES: {{^}}#include <string>{{$}}
#include <vector.h>
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: included header <vector.h> is not used directly [misc-include-cleaner]
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: included header vector.h is not used directly [misc-include-cleaner]
// CHECK-FIXES: {{^}}
int BarResult = bar();
int BazResult = baz();
Expand Down

0 comments on commit 2444fb9

Please sign in to comment.