Skip to content

Commit 72ee9b8

Browse files
authored
[NFC][analyzer] Rename CheckerBase::getCheckerName to getName (#130953)
The method name `getCheckerName` would imply "get the name of the checker associated with `this`", so it's suitable for e.g. `BugType::getCheckerName` -- but a method that just "gets the name of `this`" should be simply called `getName`. This change eliminates the redundant and ugly pattern `Checker->getCheckerName()` and helps to visually distinguish this method from `BugType::getCheckerName`. In the constructor of `BugType` the call of this method was completely removed (instead of just changing the name) because that call was dead code (when the data member `Checker` is non-null, the string stored in `CheckerName` is irrelevant) and was often querying the name of the checker before it was properly initialized. Moreover, in `ReturnValueChecker.cpp` the static helper function `getName` (which gets a function name from a `CallEvent`) was renamed to the more specific `getFunctionName` to avoid the name collision. This change is yet another cleanup commit before my planned changes that would add support for multi-part checkers to this method.
1 parent b76e396 commit 72ee9b8

File tree

7 files changed

+21
-23
lines changed

7 files changed

+21
-23
lines changed

clang/include/clang/StaticAnalyzer/Core/BugReporter/BugType.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ class BugType {
4141
Checker(nullptr), SuppressOnSink(SuppressOnSink) {}
4242
BugType(const CheckerBase *Checker, StringRef Desc,
4343
StringRef Cat = categories::LogicError, bool SuppressOnSink = false)
44-
: CheckerName(Checker->getCheckerName()), Description(Desc),
45-
Category(Cat), Checker(Checker), SuppressOnSink(SuppressOnSink) {}
44+
: CheckerName(), Description(Desc), Category(Cat), Checker(Checker),
45+
SuppressOnSink(SuppressOnSink) {}
4646
virtual ~BugType() = default;
4747

4848
StringRef getDescription() const { return Description; }
4949
StringRef getCategory() const { return Category; }
5050
StringRef getCheckerName() const {
51-
// FIXME: This is a workaround to ensure that the correct checerk name is
51+
// FIXME: This is a workaround to ensure that the correct checker name is
5252
// used. The checker names are set after the constructors are run.
5353
// In case the BugType object is initialized in the checker's ctor
5454
// the CheckerName field will be empty. To circumvent this problem we use
5555
// CheckerBase whenever it is possible.
56-
StringRef Ret = Checker ? Checker->getCheckerName() : CheckerName;
56+
StringRef Ret = Checker ? Checker->getName() : CheckerName;
5757
assert(!Ret.empty() && "Checker name is not set properly.");
5858
return Ret;
5959
}

clang/include/clang/StaticAnalyzer/Core/Checker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class CheckerBase : public ProgramPointTag {
490490

491491
public:
492492
StringRef getTagDescription() const override;
493-
CheckerNameRef getCheckerName() const;
493+
CheckerNameRef getName() const;
494494

495495
/// See CheckerManager::runCheckersForPrintState.
496496
virtual void printState(raw_ostream &Out, ProgramStateRef State,

clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,8 @@ void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE,
430430
const LangOptions &Opts = C.getLangOpts();
431431
const SourceManager &SM = C.getSourceManager();
432432
FullSourceLoc FL(CE->getArg(0)->getBeginLoc(), SM);
433-
std::string HashContent =
434-
getIssueString(FL, getCheckerName(), "Category",
435-
C.getLocationContext()->getDecl(), Opts);
433+
std::string HashContent = getIssueString(
434+
FL, getName(), "Category", C.getLocationContext()->getDecl(), Opts);
436435

437436
reportBug(HashContent, C);
438437
}

clang/lib/StaticAnalyzer/Checkers/ReturnValueChecker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ReturnValueChecker : public Checker<check::PostCall> {
6060
};
6161
} // namespace
6262

63-
static std::string getName(const CallEvent &Call) {
63+
static std::string getFunctionName(const CallEvent &Call) {
6464
std::string Name;
6565
if (const auto *MD = dyn_cast<CXXMethodDecl>(Call.getDecl()))
6666
if (const CXXRecordDecl *RD = MD->getParent())
@@ -84,7 +84,7 @@ void ReturnValueChecker::checkPostCall(const CallEvent &Call,
8484
if (ProgramStateRef StTrue = State->assume(*ReturnV, true)) {
8585
// The return value can be true, so transition to a state where it's true.
8686
std::string Msg =
87-
formatv("'{0}' returns true (by convention)", getName(Call));
87+
formatv("'{0}' returns true (by convention)", getFunctionName(Call));
8888
C.addTransition(StTrue, C.getNoteTag(Msg, /*IsPrunable=*/true));
8989
return;
9090
}
@@ -94,7 +94,7 @@ void ReturnValueChecker::checkPostCall(const CallEvent &Call,
9494
// Note that this checker is 'hidden' so it cannot produce a bug report.
9595
std::string Msg = formatv("'{0}' returned false, breaking the convention "
9696
"that it always returns true",
97-
getName(Call));
97+
getFunctionName(Call));
9898
C.addTransition(State, C.getNoteTag(Msg, /*IsPrunable=*/true));
9999
}
100100

clang/lib/StaticAnalyzer/Core/BugReporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,8 +3418,8 @@ void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
34183418
PathDiagnosticLocation Loc,
34193419
ArrayRef<SourceRange> Ranges,
34203420
ArrayRef<FixItHint> Fixits) {
3421-
EmitBasicReport(DeclWithIssue, Checker->getCheckerName(), Name, Category, Str,
3422-
Loc, Ranges, Fixits);
3421+
EmitBasicReport(DeclWithIssue, Checker->getName(), Name, Category, Str, Loc,
3422+
Ranges, Fixits);
34233423
}
34243424

34253425
void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,

clang/lib/StaticAnalyzer/Core/Checker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ using namespace ento;
1818

1919
int ImplicitNullDerefEvent::Tag;
2020

21-
StringRef CheckerBase::getTagDescription() const { return getCheckerName(); }
21+
StringRef CheckerBase::getTagDescription() const { return getName(); }
2222

23-
CheckerNameRef CheckerBase::getCheckerName() const { return Name; }
23+
CheckerNameRef CheckerBase::getName() const { return Name; }
2424

2525
CheckerProgramPointTag::CheckerProgramPointTag(StringRef CheckerName,
2626
StringRef Msg)
2727
: SimpleProgramPointTag(CheckerName, Msg) {}
2828

2929
CheckerProgramPointTag::CheckerProgramPointTag(const CheckerBase *Checker,
3030
StringRef Msg)
31-
: SimpleProgramPointTag(Checker->getCheckerName(), Msg) {}
31+
: SimpleProgramPointTag(Checker->getName(), Msg) {}
3232

3333
raw_ostream& clang::ento::operator<<(raw_ostream &Out,
3434
const CheckerBase &Checker) {
35-
Out << Checker.getCheckerName();
35+
Out << Checker.getName();
3636
return Out;
3737
}

clang/lib/StaticAnalyzer/Core/CheckerManager.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ std::string checkerScopeName(StringRef Name, const CheckerBase *Checker) {
139139
if (!llvm::timeTraceProfilerEnabled())
140140
return "";
141141
StringRef CheckerName =
142-
Checker ? static_cast<StringRef>(Checker->getCheckerName()) : "<unknown>";
142+
Checker ? static_cast<StringRef>(Checker->getName()) : "<unknown>";
143143
return (Name + ":" + CheckerName).str();
144144
}
145145

@@ -722,12 +722,12 @@ void CheckerManager::runCheckersForEvalCall(ExplodedNodeSet &Dst,
722722
"while the {2} checker also tried to evaluate the same call. At "
723723
"most one checker supposed to evaluate a call.",
724724
toString(Call), evaluatorChecker,
725-
EvalCallChecker.Checker->getCheckerName());
725+
EvalCallChecker.Checker->getName());
726726
llvm_unreachable(AssertionMessage.c_str());
727727
}
728728
#endif
729729
if (evaluated) {
730-
evaluatorChecker = EvalCallChecker.Checker->getCheckerName();
730+
evaluatorChecker = EvalCallChecker.Checker->getName();
731731
Dst.insert(checkDst);
732732
#ifdef NDEBUG
733733
break; // on release don't check that no other checker also evals.
@@ -798,9 +798,8 @@ void CheckerManager::runCheckersForPrintStateJson(raw_ostream &Out,
798798
if (TempBuf.empty())
799799
continue;
800800

801-
Indent(Out, Space, IsDot)
802-
<< "{ \"checker\": \"" << CT.second->getCheckerName()
803-
<< "\", \"messages\": [" << NL;
801+
Indent(Out, Space, IsDot) << "{ \"checker\": \"" << CT.second->getName()
802+
<< "\", \"messages\": [" << NL;
804803
Indent(Out, InnerSpace, IsDot)
805804
<< '\"' << TempBuf.str().trim() << '\"' << NL;
806805
Indent(Out, Space, IsDot) << "]}";

0 commit comments

Comments
 (0)