Skip to content

Commit e8cae48

Browse files
author
Balazs Benics
committed
Revert "[clang-tidy] modernize-deprecated-headers check should respect extern "C" blocks"
This reverts commit 7e3ea55. Looks like this breaks tests: http://45.33.8.238/linux/76033/step_8.txt
1 parent a1025e6 commit e8cae48

File tree

5 files changed

+22
-192
lines changed

5 files changed

+22
-192
lines changed

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp

Lines changed: 22 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,23 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "DeprecatedHeadersCheck.h"
10-
#include "clang/AST/RecursiveASTVisitor.h"
1110
#include "clang/Frontend/CompilerInstance.h"
1211
#include "clang/Lex/PPCallbacks.h"
1312
#include "clang/Lex/Preprocessor.h"
1413
#include "llvm/ADT/StringMap.h"
1514
#include "llvm/ADT/StringSet.h"
1615

17-
#include <algorithm>
1816
#include <vector>
1917

2018
namespace clang {
2119
namespace tidy {
2220
namespace modernize {
23-
namespace detail {
24-
bool operator<(const IncludeMarker &LHS, const IncludeMarker &RHS) {
25-
return LHS.DecomposedDiagLoc < RHS.DecomposedDiagLoc;
26-
}
27-
bool operator<(const IncludeMarker &LHS,
28-
const std::pair<FileID, unsigned> &RHS) {
29-
return LHS.DecomposedDiagLoc < RHS;
30-
}
31-
bool operator<(const std::pair<FileID, unsigned> &LHS,
32-
const IncludeMarker &RHS) {
33-
return LHS < RHS.DecomposedDiagLoc;
34-
}
3521

22+
namespace {
3623
class IncludeModernizePPCallbacks : public PPCallbacks {
3724
public:
38-
explicit IncludeModernizePPCallbacks(DeprecatedHeadersCheck &Check,
39-
LangOptions LangOpts,
40-
const SourceManager &SM);
25+
explicit IncludeModernizePPCallbacks(ClangTidyCheck &Check,
26+
LangOptions LangOpts);
4127

4228
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
4329
StringRef FileName, bool IsAngled,
@@ -47,98 +33,22 @@ class IncludeModernizePPCallbacks : public PPCallbacks {
4733
SrcMgr::CharacteristicKind FileType) override;
4834

4935
private:
50-
DeprecatedHeadersCheck &Check;
36+
ClangTidyCheck &Check;
5137
LangOptions LangOpts;
5238
llvm::StringMap<std::string> CStyledHeaderToCxx;
5339
llvm::StringSet<> DeleteHeaders;
54-
const SourceManager &SM;
55-
};
56-
57-
class ExternCRefutationVisitor
58-
: public RecursiveASTVisitor<ExternCRefutationVisitor> {
59-
std::vector<IncludeMarker> &IncludesToBeProcessed;
60-
const SourceManager &SM;
61-
62-
public:
63-
ExternCRefutationVisitor(std::vector<IncludeMarker> &IncludesToBeProcessed,
64-
SourceManager &SM)
65-
: IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) {}
66-
bool shouldWalkTypesOfTypeLocs() const { return false; }
67-
bool shouldVisitLambdaBody() const { return false; }
68-
69-
bool VisitLinkageSpecDecl(LinkageSpecDecl *LinkSpecDecl) const {
70-
if (LinkSpecDecl->getLanguage() != LinkageSpecDecl::lang_c ||
71-
!LinkSpecDecl->hasBraces())
72-
return true;
73-
74-
auto ExternCBlockBegin =
75-
SM.getDecomposedExpansionLoc(LinkSpecDecl->getBeginLoc());
76-
auto ExternCBlockEnd =
77-
SM.getDecomposedExpansionLoc(LinkSpecDecl->getEndLoc());
78-
79-
auto Begin = IncludesToBeProcessed.begin();
80-
auto End = IncludesToBeProcessed.end();
81-
auto Low = std::lower_bound(Begin, End, ExternCBlockBegin);
82-
auto Up = std::upper_bound(Low, End, ExternCBlockEnd);
83-
IncludesToBeProcessed.erase(Low, Up);
84-
return true;
85-
}
8640
};
87-
} // namespace detail
41+
} // namespace
8842

8943
void DeprecatedHeadersCheck::registerPPCallbacks(
9044
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
91-
PP->addPPCallbacks(::std::make_unique<detail::IncludeModernizePPCallbacks>(
92-
*this, getLangOpts(), PP->getSourceManager()));
93-
}
94-
void DeprecatedHeadersCheck::registerMatchers(
95-
ast_matchers::MatchFinder *Finder) {
96-
// Even though the checker operates on a "preprocessor" level, we still need
97-
// to act on a "TranslationUnit" to acquire the AST where we can walk each
98-
// Decl and look for `extern "C"` blocks where we will suppress the report we
99-
// collected during the preprocessing phase.
100-
// The `onStartOfTranslationUnit()` won't suffice, since we need some handle
101-
// to the `ASTContext`.
102-
Finder->addMatcher(ast_matchers::translationUnitDecl().bind("TU"), this);
103-
}
104-
105-
void DeprecatedHeadersCheck::onEndOfTranslationUnit() {
106-
IncludesToBeProcessed.clear();
107-
}
108-
109-
void DeprecatedHeadersCheck::check(
110-
const ast_matchers::MatchFinder::MatchResult &Result) {
111-
SourceManager &SM = Result.Context->getSourceManager();
112-
using detail::IncludeMarker;
113-
114-
llvm::sort(IncludesToBeProcessed);
115-
116-
// Suppress includes wrapped by `extern "C" { ... }` blocks.
117-
detail::ExternCRefutationVisitor Visitor(IncludesToBeProcessed, SM);
118-
Visitor.TraverseAST(*Result.Context);
119-
120-
// Emit all the remaining reports.
121-
for (const IncludeMarker &Entry : IncludesToBeProcessed) {
122-
SourceLocation DiagLoc = SM.getComposedLoc(Entry.DecomposedDiagLoc.first,
123-
Entry.DecomposedDiagLoc.second);
124-
if (Entry.Replacement.empty()) {
125-
diag(DiagLoc, "including '%0' has no effect in C++; consider removing it")
126-
<< Entry.FileName << FixItHint::CreateRemoval(Entry.ReplacementRange);
127-
} else {
128-
diag(DiagLoc, "inclusion of deprecated C++ header "
129-
"'%0'; consider using '%1' instead")
130-
<< Entry.FileName << Entry.Replacement
131-
<< FixItHint::CreateReplacement(
132-
Entry.ReplacementRange,
133-
(llvm::Twine("<") + Entry.Replacement + ">").str());
134-
}
135-
}
45+
PP->addPPCallbacks(
46+
::std::make_unique<IncludeModernizePPCallbacks>(*this, getLangOpts()));
13647
}
13748

138-
detail::IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
139-
DeprecatedHeadersCheck &Check, LangOptions LangOpts,
140-
const SourceManager &SM)
141-
: Check(Check), LangOpts(LangOpts), SM(SM) {
49+
IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(ClangTidyCheck &Check,
50+
LangOptions LangOpts)
51+
: Check(Check), LangOpts(LangOpts) {
14252
for (const auto &KeyValue :
14353
std::vector<std::pair<llvm::StringRef, std::string>>(
14454
{{"assert.h", "cassert"},
@@ -179,7 +89,7 @@ detail::IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
17989
}
18090
}
18191

182-
void detail::IncludeModernizePPCallbacks::InclusionDirective(
92+
void IncludeModernizePPCallbacks::InclusionDirective(
18393
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
18494
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
18595
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
@@ -191,16 +101,19 @@ void detail::IncludeModernizePPCallbacks::InclusionDirective(
191101
// 1. Insert std prefix for every such symbol occurrence.
192102
// 2. Insert `using namespace std;` to the beginning of TU.
193103
// 3. Do nothing and let the user deal with the migration himself.
194-
std::pair<FileID, unsigned> DiagLoc =
195-
SM.getDecomposedExpansionLoc(FilenameRange.getBegin());
196104
if (CStyledHeaderToCxx.count(FileName) != 0) {
197-
Check.IncludesToBeProcessed.push_back(
198-
IncludeMarker{CStyledHeaderToCxx[FileName], FileName,
199-
FilenameRange.getAsRange(), DiagLoc});
105+
std::string Replacement =
106+
(llvm::Twine("<") + CStyledHeaderToCxx[FileName] + ">").str();
107+
Check.diag(FilenameRange.getBegin(), "inclusion of deprecated C++ header "
108+
"'%0'; consider using '%1' instead")
109+
<< FileName << CStyledHeaderToCxx[FileName]
110+
<< FixItHint::CreateReplacement(FilenameRange.getAsRange(),
111+
Replacement);
200112
} else if (DeleteHeaders.count(FileName) != 0) {
201-
Check.IncludesToBeProcessed.push_back(
202-
IncludeMarker{std::string{}, FileName,
203-
SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc});
113+
Check.diag(FilenameRange.getBegin(),
114+
"including '%0' has no effect in C++; consider removing it")
115+
<< FileName << FixItHint::CreateRemoval(
116+
SourceRange(HashLoc, FilenameRange.getEnd()));
204117
}
205118
}
206119

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ namespace clang {
1515
namespace tidy {
1616
namespace modernize {
1717

18-
namespace detail {
19-
class IncludeModernizePPCallbacks;
20-
class ExternCRefutationVisitor;
21-
struct IncludeMarker {
22-
std::string Replacement;
23-
StringRef FileName;
24-
SourceRange ReplacementRange;
25-
std::pair<FileID, unsigned> DecomposedDiagLoc;
26-
};
27-
bool operator<(const IncludeMarker &LHS, const IncludeMarker &RHS);
28-
bool operator<(const IncludeMarker &LHS,
29-
const std::pair<FileID, unsigned> &RHS);
30-
bool operator<(const std::pair<FileID, unsigned> &LHS,
31-
const IncludeMarker &RHS);
32-
} // namespace detail
33-
3418
/// This check replaces deprecated C library headers with their C++ STL
3519
/// alternatives.
3620
///
@@ -57,14 +41,6 @@ class DeprecatedHeadersCheck : public ClangTidyCheck {
5741
}
5842
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
5943
Preprocessor *ModuleExpanderPP) override;
60-
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
61-
void onEndOfTranslationUnit() override;
62-
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
63-
64-
private:
65-
friend class detail::IncludeModernizePPCallbacks;
66-
friend class detail::ExternCRefutationVisitor;
67-
std::vector<detail::IncludeMarker> IncludesToBeProcessed;
6844
};
6945

7046
} // namespace modernize

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ Changes in existing checks
170170
<clang-tidy/checks/misc-redundant-expression>` involving assignments in
171171
conditions. This fixes `Issue 35853 <https://github.com/llvm/llvm-project/issues/35853>`_.
172172

173-
- Fixed a false positive in :doc:`modernize-deprecated-headers
174-
<clang-tidy/checks/modernize-deprecated-headers>` involving including
175-
C header files from C++ files wrapped by ``extern "C" { ... }`` blocks.
176-
Such includes will be ignored by now.
177-
178173
- Improved :doc:`performance-inefficient-vector-operation
179174
<clang-tidy/checks/performance-inefficient-vector-operation>` to work when
180175
the vector is a member of a structure.

clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-deprecated-headers/mylib.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

clang-tools-extra/test/clang-tidy/checkers/modernize-deprecated-headers-extern-c.cpp

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)