This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Enhance misc-suspicious-string-compare by matching string.compare
Needs RevisionPublic

Authored by etienneb on Apr 26 2016, 7:53 PM.

Details

Reviewers
alexfh
Summary

This patch added the support for method "compare" on string-like classes.

The LLVM stringRef is supported. The checker assume that StringRef is returning -1, 0 or 1.
Which is not the case for other functions returning <0, 0 or >0.

Diff Detail

Event Timeline

etienneb updated this revision to Diff 55157.Apr 26 2016, 7:53 PM
etienneb retitled this revision from to [clang-tidy] Enhance misc-suspicious-string-compare by matching string.compare.
etienneb updated this object.
etienneb added a reviewer: alexfh.
etienneb added a subscriber: cfe-commits.
alexfh edited edge metadata.Apr 27 2016, 4:21 AM

I'm somewhat reluctant to add LLVM-specific checks to misc-. I would prefer either to split the LLVM-specific part to a separate check in llvm/ (which might derive from this check or somehow reuse the logic) or make the check configurable enough so that these checks can be enabled for LLVM in the config file.

clang-tidy/misc/SuspiciousStringCompareCheck.cpp
119

This is inconsistent with the code above. I'd also use ofClass(hasName("::llvm::StringRef")), hasAnyName("compare", ...).

alexfh requested changes to this revision.Apr 27 2016, 4:21 AM
alexfh edited edge metadata.
This revision now requires changes to proceed.Apr 27 2016, 4:21 AM
etienneb updated this revision to Diff 55240.Apr 27 2016, 8:35 AM
etienneb edited edge metadata.
etienneb marked an inline comment as done.

address alexfh comments, add a macro FP.

I'm somewhat reluctant to add LLVM-specific checks to misc-. I would prefer either to split the LLVM-specific part to a separate check in llvm/ (which might derive from this check or somehow reuse the logic) or make the check configurable enough so that these checks can be enabled for LLVM in the config file.

How should we solve adding other code base to checkers.
It will be common to have these code base: chromium, llvm, boost, stl.

Should we use the local/global to configure them?

Please add check options descriptions to documentation.

alexfh requested changes to this revision.Jul 19 2016, 4:50 PM
alexfh edited edge metadata.

I'm somewhat reluctant to add LLVM-specific checks to misc-. I would prefer either to split the LLVM-specific part to a separate check in llvm/ (which might derive from this check or somehow reuse the logic) or make the check configurable enough so that these checks can be enabled for LLVM in the config file.

How should we solve adding other code base to checkers.
It will be common to have these code base: chromium, llvm, boost, stl.

Should we use the local/global to configure them?

(we might have discussed this offline, but I'm not sure now, so posting it here)

I don't think that checks should know about specifics of different code bases, since this doesn't scale well. One of alternative approaches should be used instead, depending on the amount and complexity of the project-specific parts:

  1. Provide enough configuration options to support known and unknown code bases (within certain limits). If needed, register checks with a project-specific alias and corresponding option values. Alternatively, specify options in the project's .clang-tidy file. Example: google-readability-namespace-comments check.
  2. Make the check extensible via inheritance. Example: llvm-header-guard check.
  3. Pull out some utility functions/matchers/classes/... and make a separate check for each code base.

One more thing to consider is that it's usually better to keep the overlap in patterns detected by the different variants of the check minimal. E.g. a check detecting a certain problematic pattern applied to LLVM containers should not complain about the same pattern applied to STL containers to avoid duplicate warnings in case both checks are turned on (consider a code base that uses LLVM, boost and STL, and needs three variants of some check).

This revision now requires changes to proceed.Jul 19 2016, 4:50 PM
Prazek edited edge metadata.Jul 19 2016, 5:03 PM
Prazek added a project: Restricted Project.