[clang-tidy] Prune dead code. NFC.

This commit is contained in:
Benjamin Kramer
2020-06-17 21:16:59 +02:00
parent f392b75fd8
commit c76edaabdd
9 changed files with 8 additions and 194 deletions
@@ -109,33 +109,6 @@ static constexpr std::pair<StringRef, IdentifierNamingCheck::CaseType>
{"Camel_Snake_Case", IdentifierNamingCheck::CT_CamelSnakeCase},
{"camel_Snake_Back", IdentifierNamingCheck::CT_CamelSnakeBack}};
namespace {
/// Callback supplies macros to IdentifierNamingCheck::checkMacro
class IdentifierNamingCheckPPCallbacks : public PPCallbacks {
public:
IdentifierNamingCheckPPCallbacks(Preprocessor *PP,
IdentifierNamingCheck *Check)
: PP(PP), Check(Check) {}
/// MacroDefined calls checkMacro for macros in the main file
void MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) override {
Check->checkMacro(PP->getSourceManager(), MacroNameTok, MD->getMacroInfo());
}
/// MacroExpands calls expandMacro for macros in the main file
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange /*Range*/,
const MacroArgs * /*Args*/) override {
Check->expandMacro(MacroNameTok, MD.getMacroInfo());
}
private:
Preprocessor *PP;
IdentifierNamingCheck *Check;
};
} // namespace
IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
ClangTidyContext *Context)
: RenamerClangTidyCheck(Name, Context),
@@ -70,40 +70,6 @@ constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
return DeclRefs;
}
// Finds all DeclRefExprs where a const method is called on VarDecl or VarDecl
// is the a const reference or value argument to a CallExpr or CXXConstructExpr.
SmallPtrSet<const DeclRefExpr *, 16>
constReferenceDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl,
ASTContext &Context) {
auto DeclRefToVar =
declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef");
auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
// Match method call expressions where the variable is referenced as the this
// implicit object argument and opertor call expression for member operators
// where the variable is the 0-th argument.
auto Matches =
match(decl(forEachDescendant(expr(
anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
cxxOperatorCallExpr(ConstMethodCallee,
hasArgument(0, DeclRefToVar)))))),
Decl, Context);
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
extractNodesByIdTo(Matches, "declRef", DeclRefs);
auto ConstReferenceOrValue =
qualType(anyOf(referenceType(pointee(qualType(isConstQualified()))),
unless(anyOf(referenceType(), pointerType()))));
auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValue)));
Matches = match(decl(forEachDescendant(callExpr(UsedAsConstRefOrValueArg))),
Decl, Context);
extractNodesByIdTo(Matches, "declRef", DeclRefs);
Matches =
match(decl(forEachDescendant(cxxConstructExpr(UsedAsConstRefOrValueArg))),
Decl, Context);
extractNodesByIdTo(Matches, "declRef", DeclRefs);
return DeclRefs;
}
bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
ASTContext &Context) {
// Collect all DeclRefExprs to the loop variable and all CallExprs and
@@ -41,12 +41,6 @@ llvm::SmallPtrSet<const DeclRefExpr *, 16>
constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
ASTContext &Context);
/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Decl`` where
/// ``VarDecl`` is guaranteed to be accessed in a const fashion.
llvm::SmallPtrSet<const DeclRefExpr *, 16>
constReferenceDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl,
ASTContext &Context);
/// Returns ``true`` if ``DeclRefExpr`` is the argument of a copy-constructor
/// call expression within ``Decl``.
bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
@@ -21,9 +21,6 @@ namespace fixit {
/// Creates fix to make ``VarDecl`` a reference by adding ``&``.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context);
/// Creates fix to make ``VarDecl`` const qualified.
FixItHint changeVarDeclToConst(const VarDecl &Var);
/// This enum defines where the qualifier shall be preferably added.
enum class QualifierPolicy {
Left, // Add the qualifier always to the left side, if that is possible.
@@ -37,7 +37,7 @@ private:
IncludeInserter::IncludeInserter(const SourceManager &SourceMgr,
const LangOptions &LangOpts,
IncludeSorter::IncludeStyle Style)
: SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {}
: SourceMgr(SourceMgr), Style(Style) {}
IncludeInserter::~IncludeInserter() {}
@@ -52,7 +52,7 @@ IncludeSorter &IncludeInserter::getOrCreate(FileID FileID) {
if (!Entry) {
// If it wasn't found, Entry will be default constructed to nullptr.
Entry = std::make_unique<IncludeSorter>(
&SourceMgr, &LangOpts, FileID,
&SourceMgr, FileID,
SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)), Style);
}
return *Entry;
@@ -76,7 +76,6 @@ private:
llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
const SourceManager &SourceMgr;
const LangOptions &LangOpts;
const IncludeSorter::IncludeStyle Style;
friend class IncludeInserterCallback;
};
@@ -83,11 +83,10 @@ DetermineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
} // namespace
IncludeSorter::IncludeSorter(const SourceManager *SourceMgr,
const LangOptions *LangOpts, const FileID FileID,
StringRef FileName, IncludeStyle Style)
: SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style),
CurrentFileID(FileID), CanonicalFile(MakeCanonicalName(FileName, Style)) {
}
const FileID FileID, StringRef FileName,
IncludeStyle Style)
: SourceMgr(SourceMgr), Style(Style), CurrentFileID(FileID),
CanonicalFile(MakeCanonicalName(FileName, Style)) {}
void IncludeSorter::AddInclude(StringRef FileName, bool IsAngled,
SourceLocation HashLocation,
@@ -175,106 +174,6 @@ Optional<FixItHint> IncludeSorter::CreateIncludeInsertion(StringRef FileName,
IncludeStmt);
}
std::vector<FixItHint> IncludeSorter::GetEdits() {
if (SourceLocations.empty())
return {};
typedef std::map<int, std::pair<SourceRange, std::string>>
FileLineToSourceEditMap;
FileLineToSourceEditMap Edits;
auto SourceLocationIterator = SourceLocations.begin();
auto SourceLocationIteratorEnd = SourceLocations.end();
// Compute the Edits that need to be done to each line to add, replace, or
// delete inclusions.
for (int IncludeKind = 0; IncludeKind < IK_InvalidInclude; ++IncludeKind) {
std::sort(IncludeBucket[IncludeKind].begin(),
IncludeBucket[IncludeKind].end());
for (const auto &IncludeEntry : IncludeBucket[IncludeKind]) {
auto &Location = IncludeLocations[IncludeEntry];
SourceRangeVector::iterator LocationIterator = Location.begin();
SourceRangeVector::iterator LocationIteratorEnd = Location.end();
SourceRange FirstLocation = *LocationIterator;
// If the first occurrence of a particular include is on the current
// source line we are examining, leave it alone.
if (FirstLocation == *SourceLocationIterator)
++LocationIterator;
// Add the deletion Edits for any (remaining) instances of this inclusion,
// and remove their Locations from the source Locations to be processed.
for (; LocationIterator != LocationIteratorEnd; ++LocationIterator) {
int LineNumber =
SourceMgr->getSpellingLineNumber(LocationIterator->getBegin());
Edits[LineNumber] = std::make_pair(*LocationIterator, "");
SourceLocationIteratorEnd =
std::remove(SourceLocationIterator, SourceLocationIteratorEnd,
*LocationIterator);
}
if (FirstLocation == *SourceLocationIterator) {
// Do nothing except move to the next source Location (Location of an
// inclusion in the original, unchanged source file).
++SourceLocationIterator;
continue;
}
// Add (or append to) the replacement text for this line in source file.
int LineNumber =
SourceMgr->getSpellingLineNumber(SourceLocationIterator->getBegin());
if (Edits.find(LineNumber) == Edits.end()) {
Edits[LineNumber].first =
SourceRange(SourceLocationIterator->getBegin());
}
StringRef SourceText = Lexer::getSourceText(
CharSourceRange::getCharRange(FirstLocation), *SourceMgr, *LangOpts);
Edits[LineNumber].second.append(SourceText.data(), SourceText.size());
}
// Clear the bucket.
IncludeBucket[IncludeKind].clear();
}
// Go through the single-line Edits and combine them into blocks of Edits.
int CurrentEndLine = 0;
SourceRange CurrentRange;
std::string CurrentText;
std::vector<FixItHint> Fixes;
for (const auto &LineEdit : Edits) {
// If the current edit is on the next line after the previous edit, add it
// to the current block edit.
if (LineEdit.first == CurrentEndLine + 1 &&
CurrentRange.getBegin() != CurrentRange.getEnd()) {
SourceRange EditRange = LineEdit.second.first;
if (EditRange.getBegin() != EditRange.getEnd()) {
++CurrentEndLine;
CurrentRange.setEnd(EditRange.getEnd());
}
CurrentText += LineEdit.second.second;
// Otherwise report the current block edit and start a new block.
} else {
if (CurrentEndLine) {
Fixes.push_back(FixItHint::CreateReplacement(
CharSourceRange::getCharRange(CurrentRange), CurrentText));
}
CurrentEndLine = LineEdit.first;
CurrentRange = LineEdit.second.first;
CurrentText = LineEdit.second.second;
}
}
// Finally, report the current block edit if there is one.
if (CurrentEndLine) {
Fixes.push_back(FixItHint::CreateReplacement(
CharSourceRange::getCharRange(CurrentRange), CurrentText));
}
// Reset the remaining internal state.
SourceLocations.clear();
IncludeLocations.clear();
return Fixes;
}
llvm::ArrayRef<std::pair<StringRef, IncludeSorter::IncludeStyle>>
IncludeSorter::getMapping() {
static constexpr std::pair<StringRef, IncludeSorter::IncludeStyle> Mapping[] =
@@ -38,22 +38,13 @@ public:
/// ``IncludeSorter`` constructor; takes the FileID and name of the file to be
/// processed by the sorter.
IncludeSorter(const SourceManager *SourceMgr, const LangOptions *LangOpts,
const FileID FileID, StringRef FileName, IncludeStyle Style);
/// Returns the ``SourceManager``-specific file ID for the file being handled
/// by the sorter.
const FileID current_FileID() const { return CurrentFileID; }
IncludeSorter(const SourceManager *SourceMgr, const FileID FileID,
StringRef FileName, IncludeStyle Style);
/// Adds the given include directive to the sorter.
void AddInclude(StringRef FileName, bool IsAngled,
SourceLocation HashLocation, SourceLocation EndLocation);
/// Returns the edits needed to sort the current set of includes and reset the
/// internal state (so that different blocks of includes are sorted separately
/// within the same file).
std::vector<FixItHint> GetEdits();
/// Creates a quoted inclusion directive in the right sort order. Returns None
/// on error or if header inclusion directive for header already exists.
Optional<FixItHint> CreateIncludeInsertion(StringRef FileName, bool IsAngled);
@@ -62,7 +53,6 @@ private:
typedef SmallVector<SourceRange, 1> SourceRangeVector;
const SourceManager *SourceMgr;
const LangOptions *LangOpts;
const IncludeStyle Style;
FileID CurrentFileID;
/// The file name stripped of common suffixes.
@@ -195,10 +195,6 @@ public:
NameLookup() : NameLookup(nullptr) {}
bool hasMultipleResolutions() const { return Data.getInt(); }
bool hasDecl() const {
assert(!hasMultipleResolutions() && "Found multiple decls");
return Data.getPointer() != nullptr;
}
const NamedDecl *getDecl() const {
assert(!hasMultipleResolutions() && "Found multiple decls");
return Data.getPointer();