Skip to content

Commit

Permalink
Fix clang deprecations by porting to FileEntryRef
Browse files Browse the repository at this point in the history
The methods we override only use the ref in clang 15, but the other code
may already utilize it in clang 14.
  • Loading branch information
alex1701c committed Sep 28, 2024
1 parent ccb232e commit ef4fa16
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/ClazyContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ClazyContext::ClazyContext(const clang::CompilerInstance &compiler,
if (exportFixesFilename.empty()) {
// Only clazy-standalone sets the filename by argument.
// clazy plugin sets it automatically here:
const FileEntry *fileEntry = sm.getFileEntryForID(sm.getMainFileID());
const auto fileEntry = sm.getFileEntryRefForID(sm.getMainFileID());
exportFixesFilename = fileEntry->getName().str() + ".clazy.yaml";
}

Expand Down
22 changes: 13 additions & 9 deletions src/ClazyContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class ClazyContext
// check has to support this feature i.e. has clazy::CheckBase::Option_CanIgnoreIncludes set
};
using ClazyOptions = int;
#if LLVM_VERSION_MAJOR >= 16
using OptionalFileEntryRef = clang::CustomizableOptional<clang::FileEntryRef>;
#else
using OptionalFileEntryRef = clang::Optional<clang::FileEntryRef>;
#endif

explicit ClazyContext(const clang::CompilerInstance &ci,
const std::string &headerFilter,
Expand Down Expand Up @@ -96,30 +101,29 @@ class ClazyContext
return clazy::contains(extraOptions, optionName);
}

bool fileMatchesLoc(const std::unique_ptr<llvm::Regex> &regex, clang::SourceLocation loc, const clang::FileEntry **file) const
bool fileMatchesLoc(const std::unique_ptr<llvm::Regex> &regex, clang::SourceLocation loc, ClazyContext::OptionalFileEntryRef &file) const
{
if (!regex) {
return false;
}

if (!(*file)) {
if (!file) {
clang::FileID fid = sm.getDecomposedExpansionLoc(loc).first;
*file = sm.getFileEntryForID(fid);
if (!(*file)) {
file = sm.getFileEntryRefForID(fid);
if (!file) {
return false;
}
}

llvm::StringRef fileName((*file)->getName());
return regex->match(fileName);
return regex->match(file->getName());
}

bool shouldIgnoreFile(clang::SourceLocation loc) const
{
// 1. Process the regexp that excludes files
const clang::FileEntry *file = nullptr;
ClazyContext::OptionalFileEntryRef file;
if (ignoreDirsRegex) {
const bool matches = fileMatchesLoc(ignoreDirsRegex, loc, &file);
const bool matches = fileMatchesLoc(ignoreDirsRegex, loc, file);
if (matches) {
return true;
}
Expand All @@ -130,7 +134,7 @@ class ClazyContext
return false;
}

const bool matches = fileMatchesLoc(headerFilterRegex, loc, &file);
const bool matches = fileMatchesLoc(headerFilterRegex, loc, file);
if (!file) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FixItExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void FixItExporter::BeginSourceFile(const LangOptions &LangOpts, const Preproces
}

const auto id = SourceMgr.getMainFileID();
const auto *const entry = SourceMgr.getFileEntryForID(id);
getTuDiag().MainSourceFile = static_cast<std::string>(entry->getName());
const auto entry = SourceMgr.getFileEntryRefForID(id);
getTuDiag().MainSourceFile = entry->getName().str();
}

bool FixItExporter::IncludeInDiagnosticCounts() const
Expand Down
2 changes: 1 addition & 1 deletion src/MiniAstDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool MiniASTDumperConsumer::VisitStmt(Stmt *)
void MiniASTDumperConsumer::HandleTranslationUnit(ASTContext &ctx)
{
auto &sm = ctx.getSourceManager();
const FileEntry *fileEntry = sm.getFileEntryForID(sm.getMainFileID());
const auto fileEntry = sm.getFileEntryRefForID(sm.getMainFileID());
llvm::errs() << "Found TU: " << fileEntry->getName() << "\n";
TraverseDecl(ctx.getTranslationUnitDecl());
}
Expand Down
1 change: 0 additions & 1 deletion src/SourceCompatibilityHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

namespace clazy
{

inline auto getBuffer(const clang::SourceManager &sm, clang::FileID id, bool *invalid)
{
#if LLVM_VERSION_MAJOR >= 16
Expand Down
1 change: 1 addition & 0 deletions src/checkbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef CHECK_BASE_H
#define CHECK_BASE_H

#include "SourceCompatibilityHelpers.h"
#include "clazy_stl.h" // IWYU pragma: keep

#include <clang/AST/ASTContext.h>
Expand Down
1 change: 0 additions & 1 deletion src/clazy_stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ inline std::string unquoteString(const std::string &str)

return str;
}

}

#endif

0 comments on commit ef4fa16

Please sign in to comment.