1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com
SPDX-FileContributor: Sérgio Martins <sergio.martins@kdab.com>
SPDX-FileCopyrightText: 2015-2017 Sergio Martins <smartins@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "checkbase.h"
#include "ClazyContext.h"
#include "SuppressionManager.h"
#include "Utils.h"
#include "clazy_stl.h"
#include <clang/AST/DeclBase.h>
#include <clang/AST/Stmt.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/DiagnosticIDs.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Lex/MacroInfo.h>
#include <clang/Lex/Preprocessor.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/raw_ostream.h>
#include <memory>
#include <vector>
namespace clang
{
class MacroArgs;
class Token;
} // namespace clang
using namespace clang;
using namespace clang::ast_matchers;
ClazyPreprocessorCallbacks::ClazyPreprocessorCallbacks(CheckBase *check)
: check(check)
{
}
void ClazyPreprocessorCallbacks::MacroExpands(const Token ¯oNameTok, const MacroDefinition &md, SourceRange range, const MacroArgs *)
{
check->VisitMacroExpands(macroNameTok, range, md.getMacroInfo());
}
void ClazyPreprocessorCallbacks::Defined(const Token ¯oNameTok, const MacroDefinition &, SourceRange range)
{
check->VisitDefined(macroNameTok, range);
}
void ClazyPreprocessorCallbacks::Ifdef(SourceLocation loc, const Token ¯oNameTok, const MacroDefinition &)
{
check->VisitIfdef(loc, macroNameTok);
}
void ClazyPreprocessorCallbacks::Ifndef(SourceLocation loc, const Token ¯oNameTok, const MacroDefinition &)
{
check->VisitIfndef(loc, macroNameTok);
}
void ClazyPreprocessorCallbacks::If(SourceLocation loc, SourceRange conditionRange, PPCallbacks::ConditionValueKind conditionValue)
{
check->VisitIf(loc, conditionRange, conditionValue);
}
void ClazyPreprocessorCallbacks::Elif(SourceLocation loc, SourceRange conditionRange, PPCallbacks::ConditionValueKind conditionValue, SourceLocation ifLoc)
{
check->VisitElif(loc, conditionRange, conditionValue, ifLoc);
}
void ClazyPreprocessorCallbacks::Else(SourceLocation loc, SourceLocation ifLoc)
{
check->VisitElse(loc, ifLoc);
}
void ClazyPreprocessorCallbacks::Endif(SourceLocation loc, SourceLocation ifLoc)
{
check->VisitEndif(loc, ifLoc);
}
void ClazyPreprocessorCallbacks::MacroDefined(const Token ¯oNameTok, const MacroDirective *)
{
check->VisitMacroDefined(macroNameTok);
}
void ClazyPreprocessorCallbacks::InclusionDirective(clang::SourceLocation HashLoc,
const clang::Token &IncludeTok,
llvm::StringRef FileName,
bool IsAngled,
clang::CharSourceRange FilenameRange,
clazy::OptionalFileEntryRef File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath,
const clang::Module *SuggestedModule,
bool ModuleImported,
clang::SrcMgr::CharacteristicKind FileType)
{
check->VisitInclusionDirective(HashLoc,
IncludeTok,
FileName,
IsAngled,
FilenameRange,
File,
SearchPath,
RelativePath,
SuggestedModule,
ModuleImported,
FileType);
}
CheckBase::CheckBase(const std::string &name, Options options)
: m_name(name)
, m_preprocessorCallbacks(new ClazyPreprocessorCallbacks(this))
, m_options(options)
{
}
CheckBase::~CheckBase() = default;
void CheckBase::VisitStmt(Stmt *)
{
}
void CheckBase::VisitDecl(Decl *)
{
}
void CheckBase::VisitMacroExpands(const Token &, const SourceRange &, const clang::MacroInfo *)
{
}
void CheckBase::VisitMacroDefined(const Token &)
{
}
void CheckBase::VisitDefined(const Token &, const SourceRange &)
{
}
void CheckBase::VisitIfdef(clang::SourceLocation, const clang::Token &)
{
}
void CheckBase::VisitIfndef(SourceLocation, const Token &)
{
}
void CheckBase::VisitIf(SourceLocation, SourceRange, clang::PPCallbacks::ConditionValueKind)
{
}
void CheckBase::VisitElif(SourceLocation, SourceRange, clang::PPCallbacks::ConditionValueKind, SourceLocation)
{
}
void CheckBase::VisitElse(SourceLocation, SourceLocation)
{
}
void CheckBase::VisitEndif(SourceLocation, SourceLocation)
{
}
void CheckBase::VisitInclusionDirective(clang::SourceLocation,
const clang::Token &,
llvm::StringRef,
bool,
clang::CharSourceRange,
clazy::OptionalFileEntryRef,
llvm::StringRef,
llvm::StringRef,
const clang::Module *,
bool ModuleImported,
clang::SrcMgr::CharacteristicKind)
{
}
void CheckBase::enablePreProcessorCallbacks(clang::Preprocessor &pp)
{
pp.addPPCallbacks(std::unique_ptr<PPCallbacks>(m_preprocessorCallbacks));
}
bool CheckBase::shouldIgnoreFile(SourceLocation loc) const
{
if (m_filesToIgnore.empty()) {
return false;
}
if (!loc.isValid()) {
return true;
}
std::string filename = sm().getFilename(loc).str();
return std::ranges::any_of(m_filesToIgnore, [filename](const std::string &ignored) {
return clazy::contains(filename, ignored);
});
}
void CheckBase::emitWarning(const clang::Decl *d, const std::string &error, bool printWarningTag)
{
emitWarning(d->getBeginLoc(), error, printWarningTag);
}
void CheckBase::emitWarning(const clang::Stmt *s, const std::string &error, bool printWarningTag)
{
emitWarning(s->getBeginLoc(), error, printWarningTag);
}
void CheckBase::emitWarning(clang::SourceLocation loc, const std::string &error, bool printWarningTag)
{
emitWarning(loc, error, {}, printWarningTag);
}
std::string CheckBase::tag() const
{
return !m_context || m_context->m_isClangTidy ? "" : " [-Wclazy-" + m_name + ']';
}
void CheckBase::emitWarning(clang::SourceLocation loc, std::string error, const std::vector<FixItHint> &fixits, bool printWarningTag)
{
if (m_context->suppressionManager.isSuppressed(m_name, loc, sm(), lo())) {
return;
}
if (m_context->shouldIgnoreFile(loc)) {
return;
}
if (loc.isMacroID()) {
if (warningAlreadyEmitted(loc)) {
return; // For warnings in macro arguments we get a warning in each place the argument is used within the expanded macro, so filter all the dups
}
m_emittedWarningsInMacro.push_back(loc.getRawEncoding());
}
if (printWarningTag) {
error += tag();
}
reallyEmitWarning(loc, error, fixits);
for (const auto &l : m_queuedManualInterventionWarnings) {
std::string msg("FixIt failed, requires manual intervention: ");
if (!l.second.empty()) {
msg += ' ' + l.second;
}
reallyEmitWarning(l.first, msg + tag(), {});
}
m_queuedManualInterventionWarnings.clear();
}
void CheckBase::emitInternalError(SourceLocation loc, std::string error)
{
llvm::errs() << tag() << ": internal error: " << error << " at " << loc.printToString(sm()) << "\n";
}
void CheckBase::reallyEmitWarning(clang::SourceLocation loc, const std::string &error, const std::vector<FixItHint> &fixits)
{
auto severity = (m_context->treatAsError(m_name)
|| (m_context->astContext && m_context->astContext->getDiagnostics().getWarningsAsErrors() && !m_context->userDisabledWError()))
? DiagnosticIDs::Error
: DiagnosticIDs::Warning;
m_context->p_warningReporter(name(), loc, severity, error, fixits);
}
void CheckBase::queueManualFixitWarning(clang::SourceLocation loc, const std::string &message)
{
if (!manualFixitAlreadyQueued(loc)) {
m_queuedManualInterventionWarnings.emplace_back(loc, message);
m_emittedManualFixItsWarningsInMacro.emplace_back(loc.getRawEncoding());
}
}
bool CheckBase::warningAlreadyEmitted(SourceLocation loc) const
{
PresumedLoc ploc = sm().getPresumedLoc(loc);
for (auto rawLoc : m_emittedWarningsInMacro) {
SourceLocation l = SourceLocation::getFromRawEncoding(rawLoc);
PresumedLoc p = sm().getPresumedLoc(l);
if (Utils::presumedLocationsEqual(p, ploc)) {
return true;
}
}
return false;
}
bool CheckBase::manualFixitAlreadyQueued(SourceLocation loc) const
{
PresumedLoc ploc = sm().getPresumedLoc(loc);
for (auto loc : m_emittedManualFixItsWarningsInMacro) {
SourceLocation l = SourceLocation::getFromRawEncoding(loc);
PresumedLoc p = sm().getPresumedLoc(l);
if (Utils::presumedLocationsEqual(p, ploc)) {
return true;
}
}
return false;
}
bool CheckBase::isOptionSet(const std::string &optionName) const
{
const std::string qualifiedName = name() + '-' + optionName;
return m_context->isOptionSet(qualifiedName);
}
ClazyAstMatcherCallback::ClazyAstMatcherCallback(CheckBase *check)
: MatchFinder::MatchCallback()
, m_check(check)
{
}
|