[clang-tools-extra] r260535 - [clang-tidy] google-runtime-int: fix a false positive in implicit code.

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 11 08:22:59 PST 2016


Author: alexfh
Date: Thu Feb 11 10:22:58 2016
New Revision: 260535

URL: http://llvm.org/viewvc/llvm-project?rev=260535&view=rev
Log:
[clang-tidy] google-runtime-int: fix a false positive in implicit code.

Modified:
    clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.cpp
    clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.h
    clang-tools-extra/trunk/test/clang-tidy/google-runtime-int.cpp

Modified: clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.cpp?rev=260535&r1=260534&r2=260535&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.cpp Thu Feb 11 10:22:58 2016
@@ -12,14 +12,34 @@
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Basic/CharInfo.h"
+#include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Lex/Lexer.h"
 
 namespace clang {
+
+using namespace ast_matchers;
+
+static Token getTokenAtLoc(SourceLocation Loc,
+                           const MatchFinder::MatchResult &MatchResult,
+                           IdentifierTable &IdentTable) {
+  Token Tok;
+  if (Lexer::getRawToken(Loc, Tok, *MatchResult.SourceManager,
+                         MatchResult.Context->getLangOpts(), false))
+    return Tok;
+
+  if (Tok.is(tok::raw_identifier)) {
+    IdentifierInfo &Info = IdentTable.get(Tok.getRawIdentifier());
+    Tok.setIdentifierInfo(&Info);
+    Tok.setKind(Info.getTokenID());
+  }
+  return Tok;
+}
+
 namespace tidy {
 namespace google {
 namespace runtime {
 
-using namespace ast_matchers;
 
 IntegerTypesCheck::IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
@@ -35,8 +55,10 @@ void IntegerTypesCheck::storeOptions(Cla
 
 void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
   // Find all TypeLocs. The relevant Style Guide rule only applies to C++.
-  if (getLangOpts().CPlusPlus)
-    Finder->addMatcher(typeLoc(loc(isInteger())).bind("tl"), this);
+  if (!getLangOpts().CPlusPlus)
+    return;
+  Finder->addMatcher(typeLoc(loc(isInteger())).bind("tl"), this);
+  IdentTable = llvm::make_unique<IdentifierTable>(getLangOpts());
 }
 
 void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
@@ -54,6 +76,15 @@ void IntegerTypesCheck::check(const Matc
   if (!BuiltinLoc)
     return;
 
+  Token Tok = getTokenAtLoc(Loc, Result, *IdentTable);
+  // Ensure the location actually points to one of the builting integral type
+  // names we're interested in. Otherwise, we might be getting this match from
+  // implicit code (e.g. an implicit assignment operator of a class containing
+  // an array of non-POD types).
+  if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
+                   tok::kw_signed))
+    return;
+
   bool IsSigned;
   unsigned Width;
   const TargetInfo &TargetInfo = Result.Context->getTargetInfo();

Modified: clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.h?rev=260535&r1=260534&r2=260535&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/google/IntegerTypesCheck.h Thu Feb 11 10:22:58 2016
@@ -12,7 +12,12 @@
 
 #include "../ClangTidy.h"
 
+#include <memory>
+
 namespace clang {
+
+class IdentifierTable;
+
 namespace tidy {
 namespace google {
 namespace runtime {
@@ -32,6 +37,8 @@ private:
   const std::string UnsignedTypePrefix;
   const std::string SignedTypePrefix;
   const std::string TypeSuffix;
+
+  std::unique_ptr<IdentifierTable> IdentTable;
 };
 
 } // namespace runtime

Modified: clang-tools-extra/trunk/test/clang-tidy/google-runtime-int.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-runtime-int.cpp?rev=260535&r1=260534&r2=260535&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-runtime-int.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-runtime-int.cpp Thu Feb 11 10:22:58 2016
@@ -65,3 +65,10 @@ struct some_value {};
 constexpr some_value operator"" _some_literal(unsigned long long int i);
 // CHECK-MESSAGES: [[@LINE-1]]:47: warning: consider replacing 'unsigned long long'
 
+struct A { A& operator=(const A&); };
+class B { A a[0]; };
+
+void fff() {
+  B a, b;
+  a = b;
+}




More information about the cfe-commits mailing list