| 1 |
//===--- SuspiciousIncludeCheck.cpp - clang-tidy --------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "SuspiciousIncludeCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/Lex/Preprocessor.h" |
| 12 |
#include <optional> |
| 13 |
|
| 14 |
namespace clang::tidy::bugprone { |
| 15 |
|
| 16 |
namespace { |
| 17 |
class SuspiciousIncludePPCallbacks : public PPCallbacks { |
| 18 |
public: |
| 19 |
explicit SuspiciousIncludePPCallbacks(SuspiciousIncludeCheck &Check, |
| 20 |
const SourceManager &SM, |
| 21 |
Preprocessor *PP) |
| 22 |
: Check(Check), PP(PP) {} |
| 23 |
|
| 24 |
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 25 |
StringRef FileName, bool IsAngled, |
| 26 |
CharSourceRange FilenameRange, |
| 27 |
OptionalFileEntryRef File, StringRef SearchPath, |
| 28 |
StringRef RelativePath, const Module *Imported, |
| 29 |
SrcMgr::CharacteristicKind FileType) override; |
| 30 |
|
| 31 |
private: |
| 32 |
SuspiciousIncludeCheck &Check; |
| 33 |
Preprocessor *PP; |
| 34 |
}; |
| 35 |
} // namespace |
| 36 |
|
| 37 |
SuspiciousIncludeCheck::SuspiciousIncludeCheck(StringRef Name, |
| 38 |
ClangTidyContext *Context) |
| 39 |
: ClangTidyCheck(Name, Context), |
| 40 |
RawStringHeaderFileExtensions(Options.getLocalOrGlobal( |
| 41 |
"HeaderFileExtensions", utils::defaultHeaderFileExtensions())), |
| 42 |
RawStringImplementationFileExtensions(Options.getLocalOrGlobal( |
| 43 |
"ImplementationFileExtensions", |
| 44 |
utils::defaultImplementationFileExtensions())) { |
| 45 |
if (!utils::parseFileExtensions(RawStringImplementationFileExtensions, |
| 46 |
ImplementationFileExtensions, |
| 47 |
utils::defaultFileExtensionDelimiters())) { |
| 48 |
this->configurationDiag("Invalid implementation file extension: '%0'") |
| 49 |
<< RawStringImplementationFileExtensions; |
| 50 |
} |
| 51 |
|
| 52 |
if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, |
| 53 |
HeaderFileExtensions, |
| 54 |
utils::defaultFileExtensionDelimiters())) { |
| 55 |
this->configurationDiag("Invalid header file extension: '%0'") |
| 56 |
<< RawStringHeaderFileExtensions; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void SuspiciousIncludeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 61 |
Options.store(Opts, "ImplementationFileExtensions", |
| 62 |
RawStringImplementationFileExtensions); |
| 63 |
Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); |
| 64 |
} |
| 65 |
|
| 66 |
void SuspiciousIncludeCheck::registerPPCallbacks( |
| 67 |
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { |
| 68 |
PP->addPPCallbacks( |
| 69 |
::std::make_unique<SuspiciousIncludePPCallbacks>(*this, SM, PP)); |
| 70 |
} |
| 71 |
|
| 72 |
void SuspiciousIncludePPCallbacks::InclusionDirective( |
| 73 |
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, |
| 74 |
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, |
| 75 |
StringRef SearchPath, StringRef RelativePath, const Module *Imported, |
| 76 |
SrcMgr::CharacteristicKind FileType) { |
| 77 |
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import) |
| 78 |
return; |
| 79 |
|
| 80 |
SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1); |
| 81 |
|
| 82 |
const std::optional<StringRef> IFE = |
| 83 |
utils::getFileExtension(FileName, Check.ImplementationFileExtensions); |
| 84 |
if (!IFE) |
| 85 |
return; |
| 86 |
|
| 87 |
Check.diag(DiagLoc, "suspicious #%0 of file with '%1' extension") |
| 88 |
<< IncludeTok.getIdentifierInfo()->getName() << *IFE; |
| 89 |
|
| 90 |
for (const auto &HFE : Check.HeaderFileExtensions) { |
| 91 |
SmallString<128> GuessedFileName(FileName); |
| 92 |
llvm::sys::path::replace_extension(GuessedFileName, |
| 93 |
(HFE.size() ? "." : "") + HFE); |
| 94 |
|
| 95 |
OptionalFileEntryRef File = |
| 96 |
PP->LookupFile(DiagLoc, GuessedFileName, IsAngled, nullptr, nullptr, |
| 97 |
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); |
| 98 |
if (File) { |
| 99 |
Check.diag(DiagLoc, "did you mean to include '%0'?", DiagnosticIDs::Note) |
| 100 |
<< GuessedFileName; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} // namespace clang::tidy::bugprone |