diff options
| author | Gabor Horvath <xazax.hun@gmail.com> | 2016-02-09 10:20:48 +0000 |
|---|---|---|
| committer | Gabor Horvath <xazax.hun@gmail.com> | 2016-02-09 10:20:48 +0000 |
| commit | f7c39d56b6ae95d3188eadf9df72e6cd7825ca16 (patch) | |
| tree | 3bc4040a2bd93bc65817b90195fcdf788ceb0db9 | |
| parent | bf8f8073dc26529760440d3ce4a610f16ec4c23c (diff) | |
| download | bcm5719-llvm-f7c39d56b6ae95d3188eadf9df72e6cd7825ca16.tar.gz bcm5719-llvm-f7c39d56b6ae95d3188eadf9df72e6cd7825ca16.zip | |
[clang-tidy] Make readability-container-size-empty work with inline namespaces. Fix PR25812.
llvm-svn: 260217
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp | 39 | ||||
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp | 19 |
2 files changed, 38 insertions, 20 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp index 53eed8f3c98..f194b47ad90 100644 --- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -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 ClassName) { namespace clang { namespace { AST_MATCHER(NamedDecl, stlContainer) { - return isContainer(Node.getQualifiedNameAsString()); + if (!isContainerName(Node.getName())) + return false; + + return StringRef(Node.getQualifiedNameAsString()).startswith("std::"); } } // namespace diff --git a/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp b/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp index 7fcfb500a25..eb453fcbb09 100644 --- a/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp +++ b/clang-tools-extra/test/clang-tidy/readability-container-size-empty.cpp @@ -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) ; |

