[clang-tools-extra] r260217 - [clang-tidy] Make readability-container-size-empty work with inline namespaces. Fix PR25812.
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 9 06:44:14 PST 2016
I agree, that would be awesome. I could achieve the same functionality
using some anyOf and hasName, but I considered it messier and probably less
performant. Anyways, what about inline namespace support for matchesName?
On 9 February 2016 at 12:54, Alexander Kornienko <alexfh at google.com> wrote:
> I think, we need to add a `hasAnyName()` matcher that would take a list of
> names.
>
> On Tue, Feb 9, 2016 at 11:20 AM, Gabor Horvath via cfe-commits <
> cfe-commits at lists.llvm.org> wrote:
>
>> Author: xazax
>> Date: Tue Feb 9 04:20:48 2016
>> New Revision: 260217
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=260217&view=rev
>> Log:
>> [clang-tidy] Make readability-container-size-empty work with inline
>> namespaces. Fix PR25812.
>>
>> Modified:
>>
>> clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
>>
>> clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp
>>
>> Modified:
>> clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp?rev=260217&r1=260216&r2=260217&view=diff
>>
>> ==============================================================================
>> ---
>> clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
>> (original)
>> +++
>> clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
>> Tue Feb 9 04:20:48 2016
>> @@ -14,23 +14,23 @@
>>
>> using namespace clang::ast_matchers;
>>
>> -static bool isContainer(llvm::StringRef ClassName) {
>> - static const char *const ContainerNames[] = {"std::array",
>> - "std::deque",
>> - "std::forward_list",
>> - "std::list",
>> - "std::map",
>> - "std::multimap",
>> - "std::multiset",
>> - "std::priority_queue",
>> - "std::queue",
>> - "std::set",
>> - "std::stack",
>> - "std::unordered_map",
>> - "std::unordered_multimap",
>> - "std::unordered_multiset",
>> - "std::unordered_set",
>> - "std::vector"};
>> +static bool isContainerName(llvm::StringRef ClassName) {
>> + static const char *const ContainerNames[] = {"array",
>> + "deque",
>> + "forward_list",
>> + "list",
>> + "map",
>> + "multimap",
>> + "multiset",
>> + "priority_queue",
>> + "queue",
>> + "set",
>> + "stack",
>> + "unordered_map",
>> + "unordered_multimap",
>> + "unordered_multiset",
>> + "unordered_set",
>> + "vector"};
>> return std::binary_search(std::begin(ContainerNames),
>> std::end(ContainerNames), ClassName);
>> }
>> @@ -38,7 +38,10 @@ static bool isContainer(llvm::StringRef
>> namespace clang {
>> namespace {
>> AST_MATCHER(NamedDecl, stlContainer) {
>> - return isContainer(Node.getQualifiedNameAsString());
>> + if (!isContainerName(Node.getName()))
>> + return false;
>> +
>> + return StringRef(Node.getQualifiedNameAsString()).startswith("std::");
>> }
>> } // namespace
>>
>>
>> Modified:
>> clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp?rev=260217&r1=260216&r2=260217&view=diff
>>
>> ==============================================================================
>> ---
>> clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp
>> (original)
>> +++
>> clang-tools-extra/trunk/test/clang-tidy/readability-container-size-empty.cpp
>> Tue Feb 9 04:20:48 2016
>> @@ -2,17 +2,32 @@
>>
>> namespace std {
>> template <typename T> struct vector {
>> - vector() {}
>> + vector();
>> unsigned long size() const;
>> bool empty() const;
>> };
>> +
>> +inline namespace __v2 {
>> +template <typename T> struct set {
>> + set();
>> + unsigned long size() const;
>> + bool empty() const;
>> +};
>> +}
>> +
>> +
>> }
>>
>> int main() {
>> + std::set<int> intSet;
>> + if (intSet.size() == 0)
>> + ;
>> + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should
>> be used to check for emptiness instead of 'size'
>> [readability-container-size-empty]
>> + // CHECK-FIXES: {{^ }}if (intSet.empty()){{$}}
>> std::vector<int> vect;
>> if (vect.size() == 0)
>> ;
>> - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should
>> be used to check for emptiness instead of 'size'
>> [readability-container-size-empty]
>> + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: the 'empty' method should
>> be used
>> // CHECK-FIXES: {{^ }}if (vect.empty()){{$}}
>> if (vect.size() != 0)
>> ;
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160209/c1ac280a/attachment.html>
More information about the cfe-commits
mailing list