Re: [PATCH] D15560: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-17 Thread Haojian Wu via cfe-commits
hokein added a comment.

Oh, this is my first attempt to submit a patch, and it isn't ready for review. 
Just ignore it.


Repository:
  rL LLVM

http://reviews.llvm.org/D15560



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15805: [clang-tidy] Cleanup code in CERT module.

2016-01-04 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 43870.
hokein added a comment.

Rename: 'CERT' => 'cert'


http://reviews.llvm.org/D15805

Files:
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/SetLongJmpCheck.cpp
  clang-tidy/cert/SetLongJmpCheck.h
  clang-tidy/cert/StaticObjectExceptionCheck.cpp
  clang-tidy/cert/StaticObjectExceptionCheck.h
  clang-tidy/cert/ThrownExceptionTypeCheck.cpp
  clang-tidy/cert/ThrownExceptionTypeCheck.h
  clang-tidy/cert/VariadicFunctionDefCheck.cpp
  clang-tidy/cert/VariadicFunctionDefCheck.h

Index: clang-tidy/cert/VariadicFunctionDefCheck.h
===
--- clang-tidy/cert/VariadicFunctionDefCheck.h
+++ clang-tidy/cert/VariadicFunctionDefCheck.h
@@ -14,6 +14,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 /// Guards against any C-style variadic function definitions (not declarations).
 ///
@@ -27,8 +28,8 @@
   void check(const ast_matchers::MatchFinder::MatchResult ) override;
 };
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
 
 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_VARIADICFUNCTIONDEF_H
-
Index: clang-tidy/cert/VariadicFunctionDefCheck.cpp
===
--- clang-tidy/cert/VariadicFunctionDefCheck.cpp
+++ clang-tidy/cert/VariadicFunctionDefCheck.cpp
@@ -15,6 +15,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 void VariadicFunctionDefCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
@@ -36,6 +37,6 @@
"parameter pack or currying instead");
 }
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
-
Index: clang-tidy/cert/ThrownExceptionTypeCheck.h
===
--- clang-tidy/cert/ThrownExceptionTypeCheck.h
+++ clang-tidy/cert/ThrownExceptionTypeCheck.h
@@ -14,6 +14,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 /// Checks whether a thrown object is nothrow copy constructible.
 ///
@@ -27,8 +28,8 @@
   void check(const ast_matchers::MatchFinder::MatchResult ) override;
 };
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
 
 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_THROWNEXCEPTIONTYPECHECK_H
-
Index: clang-tidy/cert/ThrownExceptionTypeCheck.cpp
===
--- clang-tidy/cert/ThrownExceptionTypeCheck.cpp
+++ clang-tidy/cert/ThrownExceptionTypeCheck.cpp
@@ -16,6 +16,8 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
+
 void ThrownExceptionTypeCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
@@ -34,6 +36,6 @@
"thrown exception type is not nothrow copy constructible");
 }
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
-
Index: clang-tidy/cert/StaticObjectExceptionCheck.h
===
--- clang-tidy/cert/StaticObjectExceptionCheck.h
+++ clang-tidy/cert/StaticObjectExceptionCheck.h
@@ -14,6 +14,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 /// Checks whether the constructor for a static or thread_local object will
 /// throw.
@@ -28,8 +29,8 @@
   void check(const ast_matchers::MatchFinder::MatchResult ) override;
 };
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
 
 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_ERR58_CPP_H
-
Index: clang-tidy/cert/StaticObjectExceptionCheck.cpp
===
--- clang-tidy/cert/StaticObjectExceptionCheck.cpp
+++ clang-tidy/cert/StaticObjectExceptionCheck.cpp
@@ -16,6 +16,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 void StaticObjectExceptionCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
@@ -44,6 +45,6 @@
DiagnosticIDs::Note);
 }
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
-
Index: clang-tidy/cert/SetLongJmpCheck.h
===
--- clang-tidy/cert/SetLongJmpCheck.h
+++ clang-tidy/cert/SetLongJmpCheck.h
@@ -14,6 +14,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 /// Guards against use of setjmp/longjmp in C++ code
 ///
@@ -30,8 +31,8 @@
   static const char DiagWording[];
 };
 
+} // namespace cert
 } // namespace tidy
 } // namespace clang
 
 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_SETLONGJMPCHECK_H
-
Index: clang-tidy/cert/SetLongJmpCheck.cpp
===
--- clang-tidy/cert/SetLongJmpCheck.cpp
+++ clang-tidy/cert/SetLongJmpCheck.cpp
@@ -18,6 +18,7 @@
 
 namespace clang {
 namespace tidy {
+namespace cert {
 
 const char SetLongJmpCheck::DiagWording[] =
 "do not call %0; consider using exception handling instead";
@@ -73,5 +74,6 @@
   diag(E->getExprLoc(), DiagWording) << cast(E->getCalleeDecl());
 }
 
+} // namespace cert
 } // namespace 

Re: [PATCH] D15805: [clang-tidy] Cleanup code in CERT module.

2016-01-04 Thread Haojian Wu via cfe-commits
hokein added a comment.

> Yes, but just the namespaces as you do in this patch (not anything in user 
> documentation, for instance). This patch LG; if you need me to commit on your 
> behalf, I'm happy to do so.


Done. But My account has no write access to the code repo. Can I commit the 
patch by myself? If not, I'm happy you can land it on my behalf :)


http://reviews.llvm.org/D15805



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-04 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 43887.
hokein added a comment.

Add !inMainFile check.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker', '.hpp']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,131 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // ok
+  void f2();
+  template
+  T f3() {  // ok
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4(); // ok
+};
+  };
+  static void f4(); // ok
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() { // ok
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // ok
+  static int a; // ok
+};
+
+template 
+void CB::f1() { // ok
+}
+
+template 
+void CB::CCA::f2(T a) { // ok
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() { // ok
+}
+
+template 
+int CB::a = 2; // ok;
+
+template 
+T tf() { // ok
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // ok
+inline int f6() { return 1; } // ok
+namespace {
+  int f7() { return 1; } // ok
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+  const int c = 1; // ok;
+}
+
+class CC {
+  static int d;
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace {
+  int e = 2; // ok
+}
+
+const char* const g = "foo"; // ok
+static int h = 1; // ok
+const int i = 1; // ok
+extern int j; // ok
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-definitions-in-headers.rst
@@ -0,0 +1,36 @@
+misc-definitions-in-headers
+===
+
+Finds non-extern non-inline function and variable definitions in header files, which can lead to potential ODR violations.
+
+.. code:: c++

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-04 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 43886.
hokein added a comment.

Move header file check to AST matcher.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker', '.hpp']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,131 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // ok
+  void f2();
+  template
+  T f3() {  // ok
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4(); // ok
+};
+  };
+  static void f4(); // ok
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() { // ok
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // ok
+  static int a; // ok
+};
+
+template 
+void CB::f1() { // ok
+}
+
+template 
+void CB::CCA::f2(T a) { // ok
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() { // ok
+}
+
+template 
+int CB::a = 2; // ok;
+
+template 
+T tf() { // ok
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // ok
+inline int f6() { return 1; } // ok
+namespace {
+  int f7() { return 1; } // ok
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+  const int c = 1; // ok;
+}
+
+class CC {
+  static int d;
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace {
+  int e = 2; // ok
+}
+
+const char* const g = "foo"; // ok
+static int h = 1; // ok
+const int i = 1; // ok
+extern int j; // ok
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-definitions-in-headers.rst
@@ -0,0 +1,36 @@
+misc-definitions-in-headers
+===
+
+Finds non-extern non-inline function and variable definitions in header files, which can lead to potential ODR violations.

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-04 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.


Comment at: clang-tidy/misc/DefinitionsInHeadersCheck.cpp:25
@@ +24,3 @@
+  SourceLocation ExpansionLoc = SM.getExpansionLoc(Node.getLocStart());
+  StringRef Filename = SM.getFilename(ExpansionLoc);
+  return Filename.endswith(".h") || Filename.endswith(".hh") ||

aaron.ballman wrote:
> > We're looking at the problem from different angles. My view is that a 
> > reasonable file naming convention (which at least makes interface header 
> > files, textual headers and main files distinguishable) is a widespread 
> > enough practice, and the benefits it brings outweigh the costs of enforcing 
> > it. However, the opposite point of view also has its right to exist, so we 
> > need a solution that fits both ;)
> >> Perhaps another solution to this is use isInMainFile() || 
> >> usesHeaderFileExtension().
> >You probably meant !isInMainFile() || usesHeaderFileExtension(). I guess, 
> >that will work for us. We could also make the list of header file extensions 
> >(or a regular expression pattern for header files) configurable, so that the 
> >usesHeaderFileExtension() part could be disabled, if needed.
> 
> Oops, you are correct, I meant !isInMainFile(). :-) I definitely agree that 
> we should make the header file extensions configurable. Would it make sense 
> if this were a global option that any checker can use? We have 3-4 other 
> checkers that care about header files as well, and it would be nice if they 
> all behaved consistently without the user having to write a lot of options 
> for each checker.
I'm :+1 on making header file extensions configurable. I think we can do that 
in a new patch.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-04 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.


Comment at: docs/clang-tidy/checks/misc-definitions-in-headers.rst:19
@@ +18,3 @@
+   }
+
+   // error

About the internal linkage variable definition, how about only warning the 
variable in unnamed namespace?

The const/static variable definitions in header are used more widely (We also 
find there are a lot of such usages in Google code repo), and Google C++ code 
style doesn't explicitly give suggestions about such const/static usages.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-05 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: docs/clang-tidy/checks/misc-definitions-in-headers.rst:20
@@ +19,3 @@
+   const int c = 1;
+   namespace {
+ int f = 2;

Done. I have also updated my comments here for these cases. Right now it should 
be ready for review again. Thanks.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-05 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44010.
hokein marked 4 inline comments as done.
hokein added a comment.

Update.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,133 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // OK: inline class member function definition
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4();
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// OK: member function definition of a nested template class in a class
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // OK: forward declaration
+  static int a; // OK: class static data member declaration
+};
+
+template 
+void CB::f1() { // OK: Member function definition of a class template
+}
+
+template 
+void CB::CCA::f2(T a) {
+// OK: member function definition of a nested class in a class template
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// OK: member function definition of a nested class in a class template
+}
+
+template 
+int CB::a = 2; // OK: static data member definition of a class template
+
+template 
+T tf() { // OK: template function definition
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // OK: function declaration
+inline int f6() { return 1; } // OK: inline function definition
+namespace {
+  int f7() { return 1; } // OK: internal linkage function definition
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed
+  const int c = 1; // OK: internal linkage variable definition
+}
+
+class CC {
+  static int d; // OK: class static data member declaration
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed
+
+namespace {
+  int e = 2; // OK: internal linkage variable definition
+}
+
+const char* const g = "foo"; // OK: internal linkage variable definition
+static int h = 1; // OK: internal linkage variable definition
+const int i = 1; // OK: internal linkage variable definition
+extern int j; // OK: internal linkage variable definition
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-05 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44006.
hokein added a comment.

Update doc.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker', '.hpp']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,131 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // ok
+  void f2();
+  template
+  T f3() {  // ok
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4(); // ok
+};
+  };
+  static void f4(); // ok
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() { // ok
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // ok
+  static int a; // ok
+};
+
+template 
+void CB::f1() { // ok
+}
+
+template 
+void CB::CCA::f2(T a) { // ok
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() { // ok
+}
+
+template 
+int CB::a = 2; // ok;
+
+template 
+T tf() { // ok
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // ok
+inline int f6() { return 1; } // ok
+namespace {
+  int f7() { return 1; } // ok
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+  const int c = 1; // ok;
+}
+
+class CC {
+  static int d;
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace {
+  int e = 2; // ok
+}
+
+const char* const g = "foo"; // ok
+static int h = 1; // ok
+const int i = 1; // ok
+extern int j; // ok
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-definitions-in-headers.rst
@@ -0,0 +1,41 @@
+misc-definitions-in-headers
+===
+
+Finds non-extern non-inline function and variable definitions in header files, which can lead to potential ODR violations.
+
+.. code:: c++
+   // 

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-05 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44008.
hokein added a comment.

Update doc and address comments on test file.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,133 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // ok: inline class member function definition
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4(); // ok
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// ok: member function definition of a nested template class in a class
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // ok: forward declaration
+  static int a; // ok: class static data member declaration
+};
+
+template 
+void CB::f1() { // ok: Member function definition of a class template
+}
+
+template 
+void CB::CCA::f2(T a) {
+// ok: member function definition of a nested class in a class template
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// ok: member function definition of a nested class in a class template
+}
+
+template 
+int CB::a = 2; // ok: static data member definition of a class template
+
+template 
+T tf() { // ok: template function definition
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // ok: function declaration
+inline int f6() { return 1; } // ok: inline function definition
+namespace {
+  int f7() { return 1; } // ok: internal linkage function definition
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed
+  const int c = 1; // ok: internal linkage variable definition
+}
+
+class CC {
+  static int d; // ok: class static data member declaration
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed
+
+namespace {
+  int e = 2; // ok: internal linkage variable definition
+}
+
+const char* const g = "foo"; // ok: internal linkage variable definition
+static int h = 1; // ok: internal linkage variable definition
+const int i = 1; // ok: internal linkage variable definition
+extern int j; // ok: internal linkage variable definition
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-05 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44013.
hokein marked 2 inline comments as done.
hokein added a comment.

Correct punctuation usage.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,133 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // OK: inline class member function definition.
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4();
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// OK: member function definition of a nested template class in a class.
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // OK: forward declaration.
+  static int a; // OK: class static data member declaration.
+};
+
+template 
+void CB::f1() { // OK: Member function definition of a class template.
+}
+
+template 
+void CB::CCA::f2(T a) {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+int CB::a = 2; // OK: static data member definition of a class template.
+
+template 
+T tf() { // OK: template function definition.
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // OK: function declaration.
+inline int f6() { return 1; } // OK: inline function definition.
+namespace {
+  int f7() { return 1; } // OK: internal linkage function definition.
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed
+  const int c = 1; // OK: internal linkage variable definition.
+}
+
+class CC {
+  static int d; // OK: class static data member declaration.
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed
+
+namespace {
+  int e = 2; // OK: internal linkage variable definition.
+}
+
+const char* const g = "foo"; // OK: internal linkage variable definition.
+static int h = 1; // OK: internal linkage variable definition.
+const int i = 1; // OK: internal linkage variable definition.
+extern int j; // OK: internal linkage variable definition.
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: 

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-05 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44003.
hokein added a comment.

Add UseHeaderFileExtension option.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker', '.hpp']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,131 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // ok
+  void f2();
+  template
+  T f3() {  // ok
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4(); // ok
+};
+  };
+  static void f4(); // ok
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() { // ok
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // ok
+  static int a; // ok
+};
+
+template 
+void CB::f1() { // ok
+}
+
+template 
+void CB::CCA::f2(T a) { // ok
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() { // ok
+}
+
+template 
+int CB::a = 2; // ok;
+
+template 
+T tf() { // ok
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // ok
+inline int f6() { return 1; } // ok
+namespace {
+  int f7() { return 1; } // ok
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+  const int c = 1; // ok;
+}
+
+class CC {
+  static int d;
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace {
+  int e = 2; // ok
+}
+
+const char* const g = "foo"; // ok
+static int h = 1; // ok
+const int i = 1; // ok
+extern int j; // ok
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-definitions-in-headers.rst
@@ -0,0 +1,36 @@
+misc-definitions-in-headers
+===
+
+Finds non-extern non-inline function and variable definitions in header files, which can lead to potential ODR violations.
+

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-07 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44193.
hokein added a comment.

Address Aaron's comments.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,135 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // OK: inline class member function definition.
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4();
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// OK: member function definition of a nested template class in a class.
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // OK: forward declaration.
+  static int a; // OK: class static data member declaration.
+};
+
+template 
+void CB::f1() { // OK: Member function definition of a class template.
+}
+
+template 
+void CB::CCA::f2(T a) {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+int CB::a = 2; // OK: static data member definition of a class template.
+
+template 
+T tf() { // OK: template function definition.
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // OK: function declaration.
+inline int f6() { return 1; } // OK: inline function definition.
+namespace {
+  int f7() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed
+  const int c = 1; // OK: internal linkage variable definition.
+}
+
+class CC {
+  static int d; // OK: class static data member declaration.
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed
+
+namespace {
+  int e = 2;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed
+}
+
+const char* const g = "foo"; // OK: internal linkage variable definition.
+static int h = 1; // OK: internal linkage variable definition.
+const int i = 1; // OK: internal linkage variable definition.
+extern int j; // OK: internal linkage variable definition.
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: 

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-07 Thread Haojian Wu via cfe-commits
hokein added a comment.

In http://reviews.llvm.org/D15710#321198, @aaron.ballman wrote:

> LGTM, thank you for working on this!


Ping @Alexfh. After the patch gets merged, I will work on the configuration of 
header file extension.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-31 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 43826.
hokein added a comment.

Move unittest to lit test.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker', '.hpp']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,131 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // ok
+  void f2();
+  template
+  T f3() {  // ok
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4(); // ok
+};
+  };
+  static void f4(); // ok
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() { // ok
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // ok
+  static int a; // ok
+};
+
+template 
+void CB::f1() { // ok
+}
+
+template 
+void CB::CCA::f2(T a) { // ok
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() { // ok
+}
+
+template 
+int CB::a = 2; // ok;
+
+template 
+T tf() { // ok
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function definition is not allowed in header file [misc-definitions-in-headers]
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // ok
+inline int f6() { return 1; } // ok
+namespace {
+  int f7() { return 1; } // ok
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+  const int c = 1; // ok;
+}
+
+class CC {
+  static int d;
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable definition is not allowed in header file [misc-definitions-in-headers]
+
+namespace {
+  int e = 2; // ok
+}
+
+const char* const g = "foo"; // ok
+static int h = 1; // ok
+const int i = 1; // ok
+extern int j; // ok
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/misc-definitions-in-headers.rst
@@ -0,0 +1,36 @@
+misc-definitions-in-headers
+===
+
+Finds non-extern non-inline function and variable definitions in header files, which can lead to potential ODR violations.
+
+.. code:: 

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-31 Thread Haojian Wu via cfe-commits
hokein marked 5 inline comments as done.
hokein added a comment.

http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15805: [clang-tidy] Cleanup code in CERT module.

2015-12-31 Thread Haojian Wu via cfe-commits
hokein added a comment.

So shoud we need to rename to `cert` at this patch?


Repository:
  rL LLVM

http://reviews.llvm.org/D15805



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein marked 4 inline comments as done.


Comment at: clang-tidy/misc/DefinitionsInHeadersCheck.cpp:22
@@ +21,3 @@
+
+AST_MATCHER(NamedDecl, useHeaderFileExtension) {
+  SourceManager& SM = Finder->getASTContext().getSourceManager();

alexfh wrote:
> nit: This name is not clear to me. Did you mean `isHeaderFileExtension` or 
> `usesHeaderFileExtension`?
`IsHeaderFileExtension` is more descriptive. Have renamed to it now.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44313.
hokein added a comment.

Address Alex's comments.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,135 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f' defined in a header file;
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // OK: inline class member function definition.
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4();
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'f2' defined in a header file;
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function 'f3' defined in a header file;
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// OK: member function definition of a nested template class in a class.
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // OK: forward declaration.
+  static int a; // OK: class static data member declaration.
+};
+
+template 
+void CB::f1() { // OK: Member function definition of a class template.
+}
+
+template 
+void CB::CCA::f2(T a) {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+int CB::a = 2; // OK: static data member definition of a class template.
+
+template 
+T tf() { // OK: template function definition.
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f' defined in a header file;
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function 'f3' defined in a header file;
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // OK: function declaration.
+inline int f6() { return 1; } // OK: inline function definition.
+namespace {
+  int f7() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file;
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header file;
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable 'a1' defined in a header file;
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'b' defined in a header file;
+  const int c = 1; // OK: internal linkage variable definition.
+}
+
+class CC {
+  static int d; // OK: class static data member declaration.
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'd' defined in a header file;
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'ca' defined in a header file;
+
+namespace {
+  int e = 2;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'e' defined in a header file;
+}
+
+const char* const g = "foo"; // OK: internal linkage variable definition.
+static int h = 1; // OK: internal linkage variable definition.
+const int i = 1; // OK: internal linkage variable definition.
+extern int j; // OK: internal linkage variable definition.
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if (input_file_name.endswith('.hpp')):
+extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
   

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-23 Thread Haojian Wu via cfe-commits
hokein added a comment.

@alexfh The case of member function of a nest class in a class template (like 
`int A::B::fun() {...}`)  is also fixed now. Please review the patch again. 
Thanks.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-23 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 43544.
hokein added a comment.

Update patch to address review comments.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  unittests/clang-tidy/MiscModuleTest.cpp

Index: unittests/clang-tidy/MiscModuleTest.cpp
===
--- unittests/clang-tidy/MiscModuleTest.cpp
+++ unittests/clang-tidy/MiscModuleTest.cpp
@@ -1,5 +1,6 @@
 #include "ClangTidyTest.h"
 #include "misc/ArgumentCommentCheck.h"
+#include "misc/DefinitionsInHeadersCheck.h"
 #include "gtest/gtest.h"
 
 namespace clang {
@@ -34,6 +35,180 @@
 "void f(int xxx, int yyy); void g() { f(/*xxy=*/0, 0); }");
 }
 
+class DefinitionsInHeadersCheckTest : public ::testing::Test {
+protected:
+  std::string runCheckOnCode(const std::string ,
+ const std::string ) {
+std::vector Errors;
+std::vector Args;
+if (!StringRef(Filename).endswith(".cpp")) {
+  Args.emplace_back("-xc++-header");
+}
+test::runCheckOnCode(
+Code, , Filename, Args);
+if (Errors.empty())
+  return "";
+return Errors[0].Message.Message;
+  }
+};
+
+TEST_F(DefinitionsInHeadersCheckTest, FunctionDefinition) {
+  const std::string errorMessage =
+"function definition is not allowed in header file";
+  const std::string okMessage = "";
+  EXPECT_EQ(errorMessage, runCheckOnCode("int f() { return 0; }", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("int f();\n"
+ "int f() { return 1; }", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("class A {\n"
+ "  int f();\n"
+ "};\n"
+ "int A::f() { return 1; }", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("namespace A {\n"
+ " int f() { return 1; }\n"
+ "}", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("template \n"
+ "T f() {\n"
+ "  T a = 1;\n"
+ "  return a;\n"
+ "}\n"
+ "template <>\n"
+ "int f() {\n"
+ "  int a = 1;\n"
+ "  return a;\n"
+ "}", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("struct A {\n"
+ "  template\n"
+ "  T f() {\n"
+ "T a = 1;\n"
+ "return a;\n"
+ "  }\n"
+ "};\n"
+ "template<>\n"
+ "int A::f() {\n"
+ "  int a = 1;\n"
+ "  return a;\n"
+ "}", "foo.h"));
+
+  EXPECT_EQ(okMessage, runCheckOnCode("int f();", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("inline int f() { return 1; }",
+  "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("namespace {\n"
+  " int f() { return 1; }\n"
+  "}", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("int f() { return 1; }\n",
+  "foo.cc"));
+  EXPECT_EQ(okMessage, runCheckOnCode("class A {\n"
+  " int f() { return 1; }\n"
+  "};", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("struct A {\n"
+  "  template\n"
+  "  T f() {\n"
+  "T a = 1;\n"
+  "return a;\n"
+  "  }\n"
+  "};\n", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("template\n"
+  "struct A {\n"
+  "  static void f();\n"
+  "};\n"
+  "template \n"
+  "void A::f() {\n"
+  "  T a;\n"
+  "}", "foo.h"));
+  EXPECT_EQ(okMessage, 

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-23 Thread Haojian Wu via cfe-commits
hokein marked 9 inline comments as done.


Comment at: clang-tidy/google/DefinitionsInHeadersCheck.cpp:52
@@ +51,3 @@
+// Inline function is allowed.
+if (funDecl->isInlined())
+  return;

alexfh wrote:
> This check can be done in the matcher.
The `isInline` AST matcher seems only match the function with `inline` key 
words. It could not detect the member function defined within class, such as:

```
class A {
  int f() { return 1; }
};
```


Comment at: clang-tidy/google/DefinitionsInHeadersCheck.h:17
@@ +16,3 @@
+namespace tidy {
+namespace google {
+

alexfh wrote:
> This check is generic enough. It should go to the `misc` module, IMO.
Done. Move to `misc` module


Comment at: unittests/clang-tidy/GoogleModuleTest.cpp:140
@@ +139,3 @@
+ "}", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("template \n"
+ "T f() {\n"

alexfh wrote:
> I think, we can already use raw string literals (at least, MSVC 2013 seems to 
> support them). Raw string literals would make the test cases more readable, 
> imo.
The Raw string literals doesn't work well with multiple lines in macro. I try 
it on my local machine, it will break compilation.



http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-28 Thread Haojian Wu via cfe-commits
hokein marked 6 inline comments as done.


Comment at: docs/clang-tidy/checks/misc-definitions-in-headers.rst:18
@@ +17,3 @@
+ int c  = 2; // ok
+   }
+

Yeah, it's not a good practice to define an an unnamed namespace in header file 
(Google cpp code style also suggests that). 

But I think internal linkage variables are ok here, the variable only be 
visible in current translation unit which will not cause ODR violation. And 
It's common to define a `static const` constant in C++ header file. 

BTW, the sample code in the link can be compiled in g++ v4.8


Comment at: unittests/clang-tidy/MiscModuleTest.cpp:38
@@ -36,1 +37,3 @@
 
+class DefinitionsInHeadersCheckTest : public ::testing::Test {
+protected:

I'm not sure about this. The reason I put the test in unittest is that I see 
the other header checks(`GlobalNamesInHeadersCheck`) are also in the unittest. 
It would be better to keep the consistency.


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2015-12-28 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 43686.
hokein added a comment.

Address aaron's comments.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  unittests/clang-tidy/MiscModuleTest.cpp

Index: unittests/clang-tidy/MiscModuleTest.cpp
===
--- unittests/clang-tidy/MiscModuleTest.cpp
+++ unittests/clang-tidy/MiscModuleTest.cpp
@@ -1,5 +1,6 @@
 #include "ClangTidyTest.h"
 #include "misc/ArgumentCommentCheck.h"
+#include "misc/DefinitionsInHeadersCheck.h"
 #include "gtest/gtest.h"
 
 namespace clang {
@@ -34,6 +35,180 @@
 "void f(int xxx, int yyy); void g() { f(/*xxy=*/0, 0); }");
 }
 
+class DefinitionsInHeadersCheckTest : public ::testing::Test {
+protected:
+  std::string runCheckOnCode(const std::string ,
+ const std::string ) {
+std::vector Errors;
+std::vector Args;
+if (!StringRef(Filename).endswith(".cpp")) {
+  Args.emplace_back("-xc++-header");
+}
+test::runCheckOnCode(
+Code, , Filename, Args);
+if (Errors.empty())
+  return "";
+return Errors[0].Message.Message;
+  }
+};
+
+TEST_F(DefinitionsInHeadersCheckTest, FunctionDefinition) {
+  const std::string errorMessage =
+"function definition is not allowed in header file";
+  const std::string okMessage = "";
+  EXPECT_EQ(errorMessage, runCheckOnCode("int f() { return 0; }", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("int f();\n"
+ "int f() { return 1; }", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("class A {\n"
+ "  int f();\n"
+ "};\n"
+ "int A::f() { return 1; }", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("namespace A {\n"
+ " int f() { return 1; }\n"
+ "}", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("template \n"
+ "T f() {\n"
+ "  T a = 1;\n"
+ "  return a;\n"
+ "}\n"
+ "template <>\n"
+ "int f() {\n"
+ "  int a = 1;\n"
+ "  return a;\n"
+ "}", "foo.h"));
+  EXPECT_EQ(errorMessage, runCheckOnCode("struct A {\n"
+ "  template\n"
+ "  T f() {\n"
+ "T a = 1;\n"
+ "return a;\n"
+ "  }\n"
+ "};\n"
+ "template<>\n"
+ "int A::f() {\n"
+ "  int a = 1;\n"
+ "  return a;\n"
+ "}", "foo.h"));
+
+  EXPECT_EQ(okMessage, runCheckOnCode("int f();", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("inline int f() { return 1; }",
+  "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("namespace {\n"
+  " int f() { return 1; }\n"
+  "}", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("int f() { return 1; }\n",
+  "foo.cc"));
+  EXPECT_EQ(okMessage, runCheckOnCode("class A {\n"
+  " int f() { return 1; }\n"
+  "};", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("struct A {\n"
+  "  template\n"
+  "  T f() {\n"
+  "T a = 1;\n"
+  "return a;\n"
+  "  }\n"
+  "};\n", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("template\n"
+  "struct A {\n"
+  "  static void f();\n"
+  "};\n"
+  "template \n"
+  "void A::f() {\n"
+  "  T a;\n"
+  "}", "foo.h"));
+  EXPECT_EQ(okMessage, runCheckOnCode("template\n"
+ 

[PATCH] D15802: [clang-tidy] Don't generate duplicated blank line in add_new_check.py script.

2015-12-29 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: alexfh.
hokein added a subscriber: cfe-commits.

http://reviews.llvm.org/D15802

Files:
  clang-tidy/add_new_check.py
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cert/CMakeLists.txt
  docs/clang-tidy/checks/list.rst

Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -2,6 +2,7 @@
 =
 
 .. toctree::   
+   CERT-ttest
cert-setlongjmp
cert-static-object-exception
cert-thrown-exception-type
Index: clang-tidy/cert/CMakeLists.txt
===
--- clang-tidy/cert/CMakeLists.txt
+++ clang-tidy/cert/CMakeLists.txt
@@ -5,6 +5,7 @@
   SetLongJmpCheck.cpp
   StaticObjectExceptionCheck.cpp
   ThrownExceptionTypeCheck.cpp
+  TtestCheck.cpp
   VariadicFunctionDefCheck.cpp
 
   LINK_LIBS
Index: clang-tidy/cert/CERTTidyModule.cpp
===
--- clang-tidy/cert/CERTTidyModule.cpp
+++ clang-tidy/cert/CERTTidyModule.cpp
@@ -19,6 +19,7 @@
 #include "SetLongJmpCheck.h"
 #include "StaticObjectExceptionCheck.h"
 #include "ThrownExceptionTypeCheck.h"
+#include "TtestCheck.h"
 #include "VariadicFunctionDefCheck.h"
 
 namespace clang {
@@ -30,6 +31,8 @@
   void addCheckFactories(ClangTidyCheckFactories ) override {
 // C++ checkers
 // DCL
+CheckFactories.registerCheck(
+"CERT-ttest");
 CheckFactories.registerCheck(
 "cert-dcl50-cpp");
 CheckFactories.registerCheck(
Index: clang-tidy/add_new_check.py
===
--- clang-tidy/add_new_check.py
+++ clang-tidy/add_new_check.py
@@ -92,7 +92,6 @@
 } // namespace clang
 
 #endif // %(header_guard)s
-
 """ % {'header_guard': header_guard,
'check_name': check_name_camel,
'check_name_dashes': check_name_dashes,
@@ -146,7 +145,6 @@
 } // namespace %(module)s
 } // namespace tidy
 } // namespace clang
-
 """ % {'check_name': check_name_camel,
'module': module})
 


Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -2,6 +2,7 @@
 =
 
 .. toctree::   
+   CERT-ttest
cert-setlongjmp
cert-static-object-exception
cert-thrown-exception-type
Index: clang-tidy/cert/CMakeLists.txt
===
--- clang-tidy/cert/CMakeLists.txt
+++ clang-tidy/cert/CMakeLists.txt
@@ -5,6 +5,7 @@
   SetLongJmpCheck.cpp
   StaticObjectExceptionCheck.cpp
   ThrownExceptionTypeCheck.cpp
+  TtestCheck.cpp
   VariadicFunctionDefCheck.cpp
 
   LINK_LIBS
Index: clang-tidy/cert/CERTTidyModule.cpp
===
--- clang-tidy/cert/CERTTidyModule.cpp
+++ clang-tidy/cert/CERTTidyModule.cpp
@@ -19,6 +19,7 @@
 #include "SetLongJmpCheck.h"
 #include "StaticObjectExceptionCheck.h"
 #include "ThrownExceptionTypeCheck.h"
+#include "TtestCheck.h"
 #include "VariadicFunctionDefCheck.h"
 
 namespace clang {
@@ -30,6 +31,8 @@
   void addCheckFactories(ClangTidyCheckFactories ) override {
 // C++ checkers
 // DCL
+CheckFactories.registerCheck(
+"CERT-ttest");
 CheckFactories.registerCheck(
 "cert-dcl50-cpp");
 CheckFactories.registerCheck(
Index: clang-tidy/add_new_check.py
===
--- clang-tidy/add_new_check.py
+++ clang-tidy/add_new_check.py
@@ -92,7 +92,6 @@
 } // namespace clang
 
 #endif // %(header_guard)s
-
 """ % {'header_guard': header_guard,
'check_name': check_name_camel,
'check_name_dashes': check_name_dashes,
@@ -146,7 +145,6 @@
 } // namespace %(module)s
 } // namespace tidy
 } // namespace clang
-
 """ % {'check_name': check_name_camel,
'module': module})
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15805: [clang-tidy] Cleanup code in CERT module.

2015-12-29 Thread Haojian Wu via cfe-commits
hokein added a comment.

I'm wondering whether should we rename  `CERT` to `cert` to keep the same with 
other modules like misc and google.
For adding a new check in CERT module, you need to use the upper name 
explicitly via `python add_new_check.py CERT foo` rather than `python 
add_new_check.py cert foo`.


Repository:
  rL LLVM

http://reviews.llvm.org/D15805



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44318.
hokein added a comment.

Add check name in test.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,135 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // OK: inline class member function definition.
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4();
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'f2' defined in a header file;
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function 'f3' defined in a header file;
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// OK: member function definition of a nested template class in a class.
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // OK: forward declaration.
+  static int a; // OK: class static data member declaration.
+};
+
+template 
+void CB::f1() { // OK: Member function definition of a class template.
+}
+
+template 
+void CB::CCA::f2(T a) {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+int CB::a = 2; // OK: static data member definition of a class template.
+
+template 
+T tf() { // OK: template function definition.
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f' defined in a header file;
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function 'f3' defined in a header file;
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // OK: function declaration.
+inline int f6() { return 1; } // OK: inline function definition.
+namespace {
+  int f7() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file;
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header file; variable definitions in header files can lead to ODR violations [misc-definitions-in-headers]
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable 'a1' defined in a header file;
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'b' defined in a header file;
+  const int c = 1; // OK: internal linkage variable definition.
+}
+
+class CC {
+  static int d; // OK: class static data member declaration.
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'd' defined in a header file;
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'ca' defined in a header file;
+
+namespace {
+  int e = 2;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'e' defined in a header file;
+}
+
+const char* const g = "foo"; // OK: internal linkage variable definition.
+static int h = 1; // OK: internal linkage variable definition.
+const int i = 1; // OK: internal linkage variable definition.
+extern int j; // OK: internal linkage variable definition.
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if 

Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.
hokein added a comment.

http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein marked 3 inline comments as done.


Comment at: test/clang-tidy/misc-definitions-in-headers.hpp:4
@@ +3,3 @@
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f' defined in a header 
file;
+// CHECK-FIXES: inline int f() {

alexfh wrote:
> Please specify each distinct warning message (in your case, there are two of 
> them) completely (including the check name) once. Having it in the test (just 
> once) is useful to verify that the message is formatted correctly (in your 
> case, there's no space after the ';', for example).
Didn't notice it. Done (also add a space after ";").


http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.
hokein added a comment.

http://reviews.llvm.org/D15710



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D15710: [clang-tidy] Add non-inline function definition and variable definition check in header files.

2016-01-08 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 44317.
hokein added a comment.

More updates.


http://reviews.llvm.org/D15710

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tidy/misc/DefinitionsInHeadersCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-definitions-in-headers.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-definitions-in-headers.hpp
  test/lit.cfg

Index: test/lit.cfg
===
--- test/lit.cfg
+++ test/lit.cfg
@@ -43,7 +43,8 @@
 config.test_format = lit.formats.ShTest(execute_external)
 
 # suffixes: A list of file extensions to treat as test files.
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.modularize', '.module-map-checker']
+config.suffixes = ['.c', '.cpp', '.hpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s',
+  '.modularize', '.module-map-checker']
 
 # Test-time dependencies located in directories called 'Inputs' are excluded
 # from test suites; there won't be any lit tests within them.
Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -0,0 +1,135 @@
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+
+int f() {
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f' defined in a header file; function definitions in header files can lead to ODR violations
+// CHECK-FIXES: inline int f() {
+  return 1;
+}
+
+class CA {
+  void f1() {} // OK: inline class member function definition.
+  void f2();
+  template
+  T f3() {
+T a = 1;
+return a;
+  }
+  template
+  struct CAA {
+struct CAB {
+  void f4();
+};
+  };
+};
+
+void CA::f2() { }
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'f2' defined in a header file;
+// CHECK-FIXES: inline void CA::f2() {
+
+template <>
+int CA::f3() {
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function 'f3' defined in a header file;
+  int a = 1;
+  return a;
+}
+
+template 
+void CA::CAA::CAB::f4() {
+// OK: member function definition of a nested template class in a class.
+}
+
+template 
+struct CB {
+  void f1();
+  struct CCA {
+void f2(T a);
+  };
+  struct CCB;  // OK: forward declaration.
+  static int a; // OK: class static data member declaration.
+};
+
+template 
+void CB::f1() { // OK: Member function definition of a class template.
+}
+
+template 
+void CB::CCA::f2(T a) {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+struct CB::CCB {
+  void f3();
+};
+
+template 
+void CB::CCB::f3() {
+// OK: member function definition of a nested class in a class template.
+}
+
+template 
+int CB::a = 2; // OK: static data member definition of a class template.
+
+template 
+T tf() { // OK: template function definition.
+  T a;
+  return a;
+}
+
+
+namespace NA {
+  int f() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f' defined in a header file;
+// CHECK-FIXES: inline int f() { return 1; }
+}
+
+template 
+T f3() {
+  T a = 1;
+  return a;
+}
+
+template <>
+// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: function 'f3' defined in a header file;
+int f3() {
+  int a = 1;
+  return a;
+}
+
+int f5(); // OK: function declaration.
+inline int f6() { return 1; } // OK: inline function definition.
+namespace {
+  int f7() { return 1; }
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file;
+}
+
+int a = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header file; variable definitions in header files can lead to ODR violations
+CA a1;
+// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable 'a1' defined in a header file;
+
+namespace NB {
+  int b = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'b' defined in a header file;
+  const int c = 1; // OK: internal linkage variable definition.
+}
+
+class CC {
+  static int d; // OK: class static data member declaration.
+};
+
+int CC::d = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'd' defined in a header file;
+
+const char* ca = "foo";
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'ca' defined in a header file;
+
+namespace {
+  int e = 2;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'e' defined in a header file;
+}
+
+const char* const g = "foo"; // OK: internal linkage variable definition.
+static int h = 1; // OK: internal linkage variable definition.
+const int i = 1; // OK: internal linkage variable definition.
+extern int j; // OK: internal linkage variable definition.
Index: test/clang-tidy/check_clang_tidy.py
===
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -52,6 +52,8 @@
   extension = '.cpp'
   if (input_file_name.endswith('.c')):
 extension = '.c'
+  if 

Re: [PATCH] D16008: [clang-tidy] Add calling virtual functions in constructors/destructors check.

2016-01-12 Thread Haojian Wu via cfe-commits
hokein added a comment.

In http://reviews.llvm.org/D16008#322811, @Eugene.Zelenko wrote:

> This check is duplicate of clang-analyzer-alpha.cplusplus.VirtualCall.


Oops... Didn't notice there is an implementation already.

> From my point of view, Clang-tidy is better place, since such calls doesn't 
> depend of run-time paths.

> 

> I think will be good idea to try to establish better functionality separation 
> between Clang/Clang-tidy/Clang Static Analyzer. Current situation looks like 
> different teams try to take everything coming to them without considering big 
> picture. This my impression based on including padding check in Clang Static 
> Analyzer.

> 

> I may be wrong, but Clang seems even better place for such warnings.


However clang-tidy has a ' clang-analyzer-*' check option to run the clang 
static analyzer check. I'm not sure whether it's worthwhile to move 
`VirtualCall` check to the module in clang-tidy.


Repository:
  rL LLVM

http://reviews.llvm.org/D16008



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D16113: [clang-tdiy] Add header file extension configuration support.

2016-01-12 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: clang-tidy/ClangTidyOptions.h:216
@@ +215,3 @@
+/// HeaderFileExtensions.
+bool endWithHeaderFileExtensions(llvm::StringRef FileName,
+ llvm::StringRef HeaderFileExtensions);

aaron.ballman wrote:
> endsWithHeaderFileExtension instead? However, given that uses of this all 
> start with a SourceLocation, I wonder if that makes for a cleaner API: 
> isLocInHeaderFile(SourceLocation, );
> 
> Also, how does this work if I want to include an extension-less file as the 
> header file "extension?" It would be plausible if the extensions were passed 
> in as a list, but as it stands it doesn't seem possible without weird 
> conventions like leaving a blank in the list (e.g., `.h,,.hpp`), which seems 
> error-prone.
> 
> Also, I'm not certain what I can pass in. The documentation should be updated 
> to state whether these extensions are intended to include the ".".
> endsWithHeaderFileExtension instead? However, given that uses of this all 
> start with a SourceLocation, I wonder if that makes for a cleaner API: 
> isLocInHeaderFile(SourceLocation, );

Using `SourceLocation` only is not enough to retrieve the belonging file name 
(we need `SourceManager` too).

>Also, how does this work if I want to include an extension-less file as the 
>header file "extension?" It would be plausible if the extensions were passed 
>in as a list, but as it stands it doesn't seem possible without weird 
>conventions like leaving a blank in the list (e.g., .h,,.hpp), which seems 
>error-prone.

Yeah, for extensions-less header file, you can pass the string like `.h,,.hpp`, 
which is a bit of weird. Do you have a better idea here? Passing a string into 
`header-file-extensions` seems the most reasonable choice. 



Repository:
  rL LLVM

http://reviews.llvm.org/D16113



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D20966: [include-fixer] Add the missing header to the file where the unidentified symbol comes from.

2016-06-03 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added reviewers: bkramer, ioeric.
hokein added a subscriber: cfe-commits.

http://reviews.llvm.org/D20966

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/insert_header_in_header.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -44,8 +44,6 @@
   llvm::MemoryBuffer::getMemBuffer("\n"));
   InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
   llvm::MemoryBuffer::getMemBuffer("\n"));
-  InMemoryFileSystem->addFile("header.h", 0,
-  llvm::MemoryBuffer::getMemBuffer("bar b;"));
   return Invocation.run();
 }
 
@@ -188,11 +186,6 @@
 runIncludeFixer("int test = a::b::Green;\n"));
 }
 
-TEST(IncludeFixer, IgnoreSymbolFromHeader) {
-  std::string Code = "#include \"header.h\"";
-  EXPECT_EQ(Code, runIncludeFixer(Code));
-}
-
 // FIXME: add test cases for inserting and sorting multiple headers when
 // include-fixer supports multiple headers insertion.
 TEST(IncludeFixer, InsertAndSortSingleHeader) {
Index: test/include-fixer/insert_header_in_header.cpp
===
--- /dev/null
+++ test/include-fixer/insert_header_in_header.cpp
@@ -0,0 +1,10 @@
+// REQUIRES: shell
+// RUN: echo 'foo f;' > %T/main.h
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=yaml -input=%p/Inputs/fake_yaml_db.yaml %t.cpp --
+// RUN: FileCheck %s -input-file=%T/main.h
+
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+#include "main.h"
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, FilePath: %t.cpp, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
 // CHECK-HEADERS: "Headers": [ "\"foo.h\"", "\"bar.h\"" ]
 //
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -49,11 +49,19 @@
   return p.communicate(input=text)
 
 
-def InsertHeaderToVimBuffer(header, text):
+def InsertHeaderToVimBuffer(header, code):
   command = [binary, "-stdin", "-insert-header="+json.dumps(header),
  vim.current.buffer.name]
-  stdout, stderr = execute(command, text)
+  stdout, stderr = execute(command, code)
+
   if stdout:
+# If the file being inserted is not the current one in vim buffer, just
+# overwrite it in filesystem.
+if header["FilePath"] != vim.current.buffer.name:
+  with open(header["FilePath"], "w") as f:
+f.write(stdout)
+  return
+
 lines = stdout.splitlines()
 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
 for op in reversed(sequence.get_opcodes()):
@@ -72,19 +80,24 @@
 
   # Get the current text.
   buf = vim.current.buffer
-  text = '\n'.join(buf)
+  code = '\n'.join(buf)
 
   # Run command to get all headers.
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
-  stdout, stderr = execute(command, text)
+  stdout, stderr = execute(command, code)
   if stderr:
 print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
 return
 
   include_fixer_context = json.loads(stdout)
   symbol = include_fixer_context["SymbolIdentifier"]
   headers = include_fixer_context["Headers"]
+  file_path = include_fixer_context["FilePath"]
+
+  if file_path != vim.current.buffer.name:
+with open(file_path, "r") as f:
+  code = f.read();
 
   if not symbol:
 print "The file is fine, no need to add a header.\n"
@@ -98,8 +111,11 @@
   # If there is only one suggested header, insert it directly.
   if len(headers) == 1 or maximum_suggested_headers == 1:
 InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
- "Headers":[headers[0]]}, text)
-print "Added #include {0} for {1}.\n".format(headers[0], symbol)
+ "Headers": [headers[0]],

Re: [PATCH] D21132: [include-fixer] Keep dot dot in SymbolInfo file paths.

2016-06-08 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL272152: [include-fixer] Keep dot dot in SymbolInfo file 
paths. (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D21132?vs=60041=60042#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D21132

Files:
  clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp
  
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
  clang-tools-extra/trunk/test/include-fixer/include_path.cpp
  
clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp

Index: clang-tools-extra/trunk/test/include-fixer/include_path.cpp
===
--- clang-tools-extra/trunk/test/include-fixer/include_path.cpp
+++ clang-tools-extra/trunk/test/include-fixer/include_path.cpp
@@ -1,14 +1,20 @@
 // REQUIRES: shell
 // RUN: mkdir -p %T/include-fixer/include
+// RUN: mkdir -p %T/include-fixer/symbols
 // RUN: mkdir -p %T/include-fixer/build
 // RUN: mkdir -p %T/include-fixer/src
 // RUN: sed 's|test_dir|%T/include-fixer|g' %S/Inputs/database_template.json > 
%T/include-fixer/build/compile_commands.json
-// RUN: cp %S/Inputs/fake_yaml_db.yaml %T/include-fixer/build/fake_yaml_db.yaml
-// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
-// RUN: touch %T/include-fixer/include/bar.h
+// RUN: echo -e '#include "bar.h"\nb::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: echo 'namespace b { namespace a { class bar {}; } }' > 
%T/include-fixer/include/bar.h
 // RUN: cd %T/include-fixer/build
-// RUN: clang-include-fixer -db=yaml -input=fake_yaml_db.yaml 
-minimize-paths=true -p=. %T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -output-dir=%T/include-fixer/symbols -p=. 
%T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -merge-dir=%T/include-fixer/symbols 
%T/include-fixer/build/find_all_symbols.yaml
+// RUN: FileCheck -input-file=%T/include-fixer/build/find_all_symbols.yaml 
-check-prefix=CHECK-YAML %s
+//
+// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: clang-include-fixer -db=yaml 
-input=%T/include-fixer/build/find_all_symbols.yaml -minimize-paths=true -p=. 
%T/include-fixer/src/bar.cpp
 // RUN: FileCheck -input-file=%T/include-fixer/src/bar.cpp %s
 
+// CHECK-YAML: ../include/bar.h
 // CHECK: #include "bar.h"
 // CHECK: b::a::bar f;
Index: 
clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
===
--- 
clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
+++ 
clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
@@ -105,7 +105,7 @@
   "#include \"internal/internal.h\"";
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 // Test path cleaning for both decls and macros.
-const std::string DirtyHeader = "./internal/../internal/./a/b.h";
+const std::string DirtyHeader = "./internal/./a/b.h";
 Content += "\n#include \"" + DirtyHeader + "\"";
 const std::string CleanHeader = "internal/a/b.h";
 const std::string DirtyHeaderContent =
Index: 
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
===
--- 
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
+++ 
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
@@ -158,6 +158,5 @@
   auto Factory =
   llvm::make_unique(
   , clang::find_all_symbols::getSTLPostfixHeaderMap());
-  Tool.run(Factory.get());
-  return 0;
+  return Tool.run(Factory.get());
 }
Index: clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp
===
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp
@@ -33,7 +33,7 @@
   if (Collector)
 FilePath = Collector->getMappedHeader(FilePath);
   SmallString<256> CleanedFilePath = FilePath;
-  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/true);
+  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/false);
 
   return CleanedFilePath.str();
 }


Index: clang-tools-extra/trunk/test/include-fixer/include_path.cpp
===
--- clang-tools-extra/trunk/test/include-fixer/include_path.cpp
+++ clang-tools-extra/trunk/test/include-fixer/include_path.cpp
@@ -1,14 +1,20 @@
 // REQUIRES: shell
 // RUN: mkdir -p %T/include-fixer/include
+// RUN: mkdir -p %T/include-fixer/symbols
 // RUN: mkdir -p %T/include-fixer/build
 // RUN: mkdir -p %T/include-fixer/src
 // RUN: sed 's|test_dir|%T/include-fixer|g' %S/Inputs/database_template.json > %T/include-fixer/build/compile_commands.json
-// 

[clang-tools-extra] r272152 - [include-fixer] Keep dot dot in SymbolInfo file paths.

2016-06-08 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Wed Jun  8 10:10:18 2016
New Revision: 272152

URL: http://llvm.org/viewvc/llvm-project?rev=272152=rev
Log:
[include-fixer] Keep dot dot in SymbolInfo file paths.

Summary:
Currently, removing dot dot in header's path doesn't make include-fixer
minimize path correctly in some cases, for example, specify a relative search
path based on the build directory("-I../include/").

Besides, removing dot dot can break symbolic link directories. So don't
removing it for now.

Reviewers: ioeric, bkramer

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D21132

Modified:
clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp

clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
clang-tools-extra/trunk/test/include-fixer/include_path.cpp

clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp

Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp?rev=272152=272151=272152=diff
==
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp 
(original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/PathConfig.cpp Wed 
Jun  8 10:10:18 2016
@@ -33,7 +33,7 @@ std::string getIncludePath(const SourceM
   if (Collector)
 FilePath = Collector->getMappedHeader(FilePath);
   SmallString<256> CleanedFilePath = FilePath;
-  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/true);
+  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/false);
 
   return CleanedFilePath.str();
 }

Modified: 
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp?rev=272152=272151=272152=diff
==
--- 
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
 (original)
+++ 
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
 Wed Jun  8 10:10:18 2016
@@ -158,6 +158,5 @@ int main(int argc, const char **argv) {
   auto Factory =
   llvm::make_unique(
   , clang::find_all_symbols::getSTLPostfixHeaderMap());
-  Tool.run(Factory.get());
-  return 0;
+  return Tool.run(Factory.get());
 }

Modified: clang-tools-extra/trunk/test/include-fixer/include_path.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/include-fixer/include_path.cpp?rev=272152=272151=272152=diff
==
--- clang-tools-extra/trunk/test/include-fixer/include_path.cpp (original)
+++ clang-tools-extra/trunk/test/include-fixer/include_path.cpp Wed Jun  8 
10:10:18 2016
@@ -1,14 +1,20 @@
 // REQUIRES: shell
 // RUN: mkdir -p %T/include-fixer/include
+// RUN: mkdir -p %T/include-fixer/symbols
 // RUN: mkdir -p %T/include-fixer/build
 // RUN: mkdir -p %T/include-fixer/src
 // RUN: sed 's|test_dir|%T/include-fixer|g' %S/Inputs/database_template.json > 
%T/include-fixer/build/compile_commands.json
-// RUN: cp %S/Inputs/fake_yaml_db.yaml %T/include-fixer/build/fake_yaml_db.yaml
-// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
-// RUN: touch %T/include-fixer/include/bar.h
+// RUN: echo -e '#include "bar.h"\nb::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: echo 'namespace b { namespace a { class bar {}; } }' > 
%T/include-fixer/include/bar.h
 // RUN: cd %T/include-fixer/build
-// RUN: clang-include-fixer -db=yaml -input=fake_yaml_db.yaml 
-minimize-paths=true -p=. %T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -output-dir=%T/include-fixer/symbols -p=. 
%T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -merge-dir=%T/include-fixer/symbols 
%T/include-fixer/build/find_all_symbols.yaml
+// RUN: FileCheck -input-file=%T/include-fixer/build/find_all_symbols.yaml 
-check-prefix=CHECK-YAML %s
+//
+// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: clang-include-fixer -db=yaml 
-input=%T/include-fixer/build/find_all_symbols.yaml -minimize-paths=true -p=. 
%T/include-fixer/src/bar.cpp
 // RUN: FileCheck -input-file=%T/include-fixer/src/bar.cpp %s
 
+// CHECK-YAML: ../include/bar.h
 // CHECK: #include "bar.h"
 // CHECK: b::a::bar f;

Modified: 
clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp?rev=272152=272151=272152=diff
==
--- 
clang-tools-extra/trunk/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
 (original)
+++ 

Re: [PATCH] D21132: [include-fixer] Keep dot dot in SymbolInfo file paths.

2016-06-08 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 60041.
hokein added a comment.

Rebase.


http://reviews.llvm.org/D21132

Files:
  include-fixer/find-all-symbols/PathConfig.cpp
  include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
  test/include-fixer/include_path.cpp
  unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp

Index: unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
===
--- unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
+++ unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
@@ -105,7 +105,7 @@
   "#include \"internal/internal.h\"";
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 // Test path cleaning for both decls and macros.
-const std::string DirtyHeader = "./internal/../internal/./a/b.h";
+const std::string DirtyHeader = "./internal/./a/b.h";
 Content += "\n#include \"" + DirtyHeader + "\"";
 const std::string CleanHeader = "internal/a/b.h";
 const std::string DirtyHeaderContent =
Index: test/include-fixer/include_path.cpp
===
--- test/include-fixer/include_path.cpp
+++ test/include-fixer/include_path.cpp
@@ -1,14 +1,20 @@
 // REQUIRES: shell
 // RUN: mkdir -p %T/include-fixer/include
+// RUN: mkdir -p %T/include-fixer/symbols
 // RUN: mkdir -p %T/include-fixer/build
 // RUN: mkdir -p %T/include-fixer/src
 // RUN: sed 's|test_dir|%T/include-fixer|g' %S/Inputs/database_template.json > 
%T/include-fixer/build/compile_commands.json
-// RUN: cp %S/Inputs/fake_yaml_db.yaml %T/include-fixer/build/fake_yaml_db.yaml
-// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
-// RUN: touch %T/include-fixer/include/bar.h
+// RUN: echo -e '#include "bar.h"\nb::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: echo 'namespace b { namespace a { class bar {}; } }' > 
%T/include-fixer/include/bar.h
 // RUN: cd %T/include-fixer/build
-// RUN: clang-include-fixer -db=yaml -input=fake_yaml_db.yaml 
-minimize-paths=true -p=. %T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -output-dir=%T/include-fixer/symbols -p=. 
%T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -merge-dir=%T/include-fixer/symbols 
%T/include-fixer/build/find_all_symbols.yaml
+// RUN: FileCheck -input-file=%T/include-fixer/build/find_all_symbols.yaml 
-check-prefix=CHECK-YAML %s
+//
+// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: clang-include-fixer -db=yaml 
-input=%T/include-fixer/build/find_all_symbols.yaml -minimize-paths=true -p=. 
%T/include-fixer/src/bar.cpp
 // RUN: FileCheck -input-file=%T/include-fixer/src/bar.cpp %s
 
+// CHECK-YAML: ../include/bar.h
 // CHECK: #include "bar.h"
 // CHECK: b::a::bar f;
Index: include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
===
--- include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
+++ include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
@@ -158,6 +158,5 @@
   auto Factory =
   llvm::make_unique(
   , clang::find_all_symbols::getSTLPostfixHeaderMap());
-  Tool.run(Factory.get());
-  return 0;
+  return Tool.run(Factory.get());
 }
Index: include-fixer/find-all-symbols/PathConfig.cpp
===
--- include-fixer/find-all-symbols/PathConfig.cpp
+++ include-fixer/find-all-symbols/PathConfig.cpp
@@ -33,7 +33,7 @@
   if (Collector)
 FilePath = Collector->getMappedHeader(FilePath);
   SmallString<256> CleanedFilePath = FilePath;
-  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/true);
+  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/false);
 
   return CleanedFilePath.str();
 }


Index: unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
===
--- unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
+++ unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
@@ -105,7 +105,7 @@
   "#include \"internal/internal.h\"";
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 // Test path cleaning for both decls and macros.
-const std::string DirtyHeader = "./internal/../internal/./a/b.h";
+const std::string DirtyHeader = "./internal/./a/b.h";
 Content += "\n#include \"" + DirtyHeader + "\"";
 const std::string CleanHeader = "internal/a/b.h";
 const std::string DirtyHeaderContent =
Index: test/include-fixer/include_path.cpp
===
--- test/include-fixer/include_path.cpp
+++ test/include-fixer/include_path.cpp
@@ -1,14 +1,20 @@
 // REQUIRES: shell
 // RUN: mkdir -p %T/include-fixer/include
+// RUN: mkdir -p %T/include-fixer/symbols
 // RUN: mkdir -p %T/include-fixer/build
 // RUN: mkdir -p %T/include-fixer/src
 // RUN: sed 's|test_dir|%T/include-fixer|g' 

Re: [PATCH] D20467: [include-fixer] Mention more details in the document.

2016-06-07 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59838.
hokein added a comment.

Fix typo.


http://reviews.llvm.org/D20467

Files:
  docs/ReleaseNotes.rst
  docs/include-fixer.rst

Index: docs/include-fixer.rst
===
--- docs/include-fixer.rst
+++ docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ 
path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database 
into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link 
compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` 
to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment 
variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let 
g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can customize the number of headers being shown by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---


Index: docs/include-fixer.rst
===
--- docs/include-fixer.rst
+++ docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can customize the number of headers being shown by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r271989 - [include-fixer] Mention more details in the document.

2016-06-07 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Tue Jun  7 02:50:48 2016
New Revision: 271989

URL: http://llvm.org/viewvc/llvm-project?rev=271989=rev
Log:
[include-fixer] Mention more details in the document.

Reviewers: bkramer

Subscribers: Eugene.Zelenko, cfe-commits, ioeric

Differential Revision: http://reviews.llvm.org/D20467

Modified:
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/include-fixer.rst

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=271989=271988=271989=diff
==
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Tue Jun  7 02:50:48 2016
@@ -44,7 +44,8 @@ infrastructure are described first, foll
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---

Modified: clang-tools-extra/trunk/docs/include-fixer.rst
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/include-fixer.rst?rev=271989=271988=271989=diff
==
--- clang-tools-extra/trunk/docs/include-fixer.rst (original)
+++ clang-tools-extra/trunk/docs/include-fixer.rst Tue Jun  7 02:50:48 2016
@@ -41,6 +41,8 @@ database for LLVM, any project built by
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ 
path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
@@ -48,7 +50,7 @@ database for LLVM, any project built by
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database 
into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link 
compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@ following key binding to your ``.vimrc``
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` 
to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment 
variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let 
g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can customize the number of headers being shown by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20467: [include-fixer] Mention more details in the document.

2016-06-07 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL271989: [include-fixer] Mention more details in the 
document. (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D20467?vs=59838=59839#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20467

Files:
  clang-tools-extra/trunk/docs/ReleaseNotes.rst
  clang-tools-extra/trunk/docs/include-fixer.rst

Index: clang-tools-extra/trunk/docs/include-fixer.rst
===
--- clang-tools-extra/trunk/docs/include-fixer.rst
+++ clang-tools-extra/trunk/docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ 
path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database 
into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link 
compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` 
to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment 
variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let 
g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can customize the number of headers being shown by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: clang-tools-extra/trunk/docs/ReleaseNotes.rst
===
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---


Index: clang-tools-extra/trunk/docs/include-fixer.rst
===
--- clang-tools-extra/trunk/docs/include-fixer.rst
+++ clang-tools-extra/trunk/docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can customize the number of headers being shown by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: clang-tools-extra/trunk/docs/ReleaseNotes.rst
===
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---
___

Re: [PATCH] D20966: [include-fixer] Add the missing header to the file where the unidentified symbol comes from.

2016-06-07 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: include-fixer/IncludeFixer.cpp:104
@@ -103,3 @@
-//   class Bar;
-//   Foo foo;
-//

bkramer wrote:
> Does this patch do the right thing for the test case in the comment? 
> Otherwise we'll try add includes to system headers all the time, which both 
> wrong and invisible to the user.
No, the patch doesn't handle this case :(. Currently it is hard to figure out 
this case. For the system headers, we can probably blacklist them? 


http://reviews.llvm.org/D20966



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21059: [clang-tidy] Ignore the deleted function in misc-definitions-in-headers.

2016-06-07 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL271991: [clang-tidy] Ignore the deleted function in 
misc-definitions-in-headers. (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D21059?vs=59843=59845#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D21059

Files:
  clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp

Index: clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
===
--- clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
+++ clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
@@ -103,6 +103,8 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header 
file;
 }
 
+int f8() = delete; // OK: the function being marked delete is not callable.
+
 int a = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header 
file; variable definitions in header files can lead to ODR violations 
[misc-definitions-in-headers]
 CA a1;
Index: clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -53,15 +53,17 @@
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),


Index: clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
===
--- clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
+++ clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
@@ -103,6 +103,8 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file;
 }
 
+int f8() = delete; // OK: the function being marked delete is not callable.
+
 int a = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header file; variable definitions in header files can lead to ODR violations [misc-definitions-in-headers]
 CA a1;
Index: clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -53,15 +53,17 @@
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r271991 - [clang-tidy] Ignore the deleted function in misc-definitions-in-headers.

2016-06-07 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Tue Jun  7 03:55:38 2016
New Revision: 271991

URL: http://llvm.org/viewvc/llvm-project?rev=271991=rev
Log:
[clang-tidy] Ignore the deleted function in misc-definitions-in-headers.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D21059

Modified:
clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp?rev=271991=271990=271991=diff
==
--- clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/misc/DefinitionsInHeadersCheck.cpp Tue 
Jun  7 03:55:38 2016
@@ -53,15 +53,17 @@ void DefinitionsInHeadersCheck::storeOpt
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),

Modified: 
clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp?rev=271991=271990=271991=diff
==
--- clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp 
(original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-definitions-in-headers.hpp Tue 
Jun  7 03:55:38 2016
@@ -103,6 +103,8 @@ namespace {
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header 
file;
 }
 
+int f8() = delete; // OK: the function being marked delete is not callable.
+
 int a = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header 
file; variable definitions in header files can lead to ODR violations 
[misc-definitions-in-headers]
 CA a1;


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21059: [clang-tidy] Ignore the deleted function in misc-definitions-in-headers.

2016-06-07 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59843.
hokein marked an inline comment as done.
hokein added a comment.

Use the existing test file.


http://reviews.llvm.org/D21059

Files:
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  test/clang-tidy/misc-definitions-in-headers.hpp

Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- test/clang-tidy/misc-definitions-in-headers.hpp
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -103,6 +103,8 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header 
file;
 }
 
+int f8() = delete; // OK: the function being marked delete is not callable.
+
 int a = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header 
file; variable definitions in header files can lead to ODR violations 
[misc-definitions-in-headers]
 CA a1;
Index: clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===
--- clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -53,15 +53,17 @@
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),


Index: test/clang-tidy/misc-definitions-in-headers.hpp
===
--- test/clang-tidy/misc-definitions-in-headers.hpp
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -103,6 +103,8 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file;
 }
 
+int f8() = delete; // OK: the function being marked delete is not callable.
+
 int a = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header file; variable definitions in header files can lead to ODR violations [misc-definitions-in-headers]
 CA a1;
Index: clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===
--- clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -53,15 +53,17 @@
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D21059: [clang-tidy] Ignore the deleted function in misc-definitions-in-headers.

2016-06-07 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: alexfh.
hokein added a subscriber: cfe-commits.

http://reviews.llvm.org/D21059

Files:
  clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  test/clang-tidy/misc-definitions-in-headers-cxx11.hpp

Index: test/clang-tidy/misc-definitions-in-headers-cxx11.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers-cxx11.hpp
@@ -0,0 +1,7 @@
+// RUN: clang-tidy %s -checks="-*,misc-definitions-in-headers" -- 
-extra-arg="-std=c++11" | FileCheck -allow-empty %s
+
+// Note: This test verifies that the checker does not emit any warning for the
+// function being marked delete.
+
+int f() = delete;
+// CHECK-NOT: [misc-definitions-in-headers]
Index: clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===
--- clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -53,15 +53,17 @@
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),


Index: test/clang-tidy/misc-definitions-in-headers-cxx11.hpp
===
--- /dev/null
+++ test/clang-tidy/misc-definitions-in-headers-cxx11.hpp
@@ -0,0 +1,7 @@
+// RUN: clang-tidy %s -checks="-*,misc-definitions-in-headers" -- -extra-arg="-std=c++11" | FileCheck -allow-empty %s
+
+// Note: This test verifies that the checker does not emit any warning for the
+// function being marked delete.
+
+int f() = delete;
+// CHECK-NOT: [misc-definitions-in-headers]
Index: clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===
--- clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -53,15 +53,17 @@
 void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  auto DefinitionMatcher =
+  anyOf(functionDecl(isDefinition(), unless(isDeleted())),
+varDecl(isDefinition()));
   if (UseHeaderFileExtension) {
-Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
-  usesHeaderFileExtension(HeaderFileExtensions))
-.bind("name-decl"),
-this);
+Finder->addMatcher(namedDecl(DefinitionMatcher,
+ usesHeaderFileExtension(HeaderFileExtensions))
+   .bind("name-decl"),
+   this);
   } else {
 Finder->addMatcher(
-namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())),
+namedDecl(DefinitionMatcher,
   anyOf(usesHeaderFileExtension(HeaderFileExtensions),
 unless(isExpansionInMainFile(
 .bind("name-decl"),
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21036: Misplaced const-qualification with typedef types

2016-06-07 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: test/clang-tidy/misc-misplaced-const.c:17
@@ +16,3 @@
+  const ip i3 = 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'i3' declared with a 
const-qualified typedef type; results in the type being 'int *const' instead of 
'const int *'
+

Will the check show the warning on `ip const i3 = 0;`? Technically, `const ip 
i3 = 0` is exactly the same with `ip const i3 = 0;`.


http://reviews.llvm.org/D21036



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21036: Misplaced const-qualification with typedef types

2016-06-07 Thread Haojian Wu via cfe-commits
hokein added a comment.

@aaron.ballman, you forgot to add the check in docs/ReleaseNotes.rst.


http://reviews.llvm.org/D21036



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20512: [PATCH] Bug 27475 - Request header guard check processes .hpp files as well as .h files

2016-06-07 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: clang-tidy/llvm/HeaderGuardCheck.h:19
@@ -18,3 +18,3 @@
 
 /// Finds and fixes header guards that do not adhere to LLVM style.
 class LLVMHeaderGuardCheck : public utils::HeaderGuardCheck {

hokein wrote:
> madsravn wrote:
> > hokein wrote:
> > > You should add a document for the option `HeaderFileExtensions` here.
> > I'm not sure what you mean here. What is "a document" in this context?
> Sorry for the unclear comment. You actually add an option 
> `HeaderFileExtensions` in `LLVMHeaderGuardCheck`, so you need to add a 
> document to describe it. See the file `DefinitionsInHeadersCheck.h` for 
> details.
You need to add a comment here, like:

```
/// Finds and fixes header guards that do not adhere to LLVM style.
///
///  The check supports these options:
///   - `HeaderFileExtensions`: a comma-separated list of filename extensions of
/// header files (The filename extension should not contain "." prefix).
/// ",h,hh,hpp,hxx" by default.
```


http://reviews.llvm.org/D20512



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21019: [include-fixer] try to make vim header selection more friendly.

2016-06-06 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: include-fixer/tool/clang-include-fixer.py:53
@@ +52,3 @@
+  except Exception:
+if res == '':
+  # choose the top ranked header by default

We can handle the ` ` , `a`, `q` cases after `res=vim.eval(to_eval)` instead of 
handling them in exception block. They don't look like errors in my opinion.


Comment at: include-fixer/tool/clang-include-fixer.py:132
@@ +131,3 @@
+  except Exception as error:
+print error.message
+  return

For error message, it might be better to print to stderr `print >> sys.stderr, 
error.message`?


http://reviews.llvm.org/D21019



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20467: [include-fixer] Mention more details in the document.

2016-06-06 Thread Haojian Wu via cfe-commits
hokein added a comment.

In http://reviews.llvm.org/D20467#435685, @Eugene.Zelenko wrote:

> Could you please mention include-fixer in  docs/ReleaseNotes.rst? This is 
> definitely major new feature in upcoming release.


Done.


http://reviews.llvm.org/D20467



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20467: [include-fixer] Mention more details in the document.

2016-06-06 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59721.
hokein marked an inline comment as done.
hokein added a comment.

Address review comments.


http://reviews.llvm.org/D20467

Files:
  docs/ReleaseNotes.rst
  docs/include-fixer.rst

Index: docs/include-fixer.rst
===
--- docs/include-fixer.rst
+++ docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ 
path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database 
into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link 
compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` 
to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add :program:`clang-include-fixer` path in the environment PATH variable.
+- Or set ``g:clang_include_fixer_path`` in vim configuration path: ``let 
g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can custonmize the number of headers being showed by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool provides an automated way of adding
+  ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---


Index: docs/include-fixer.rst
===
--- docs/include-fixer.rst
+++ docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add :program:`clang-include-fixer` path in the environment PATH variable.
+- Or set ``g:clang_include_fixer_path`` in vim configuration path: ``let g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can custonmize the number of headers being showed by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool provides an automated way of adding
+  ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20467: [include-fixer] Mention more details in the document.

2016-06-06 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59725.
hokein marked 3 inline comments as done.
hokein added a comment.

Add comments.


http://reviews.llvm.org/D20467

Files:
  docs/ReleaseNotes.rst
  docs/include-fixer.rst

Index: docs/include-fixer.rst
===
--- docs/include-fixer.rst
+++ docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ 
path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database 
into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link 
compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` 
to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment 
variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let 
g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can custonmize the number of headers being showed by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---


Index: docs/include-fixer.rst
===
--- docs/include-fixer.rst
+++ docs/include-fixer.rst
@@ -41,14 +41,16 @@
 .. code-block:: console
 
   $ cd path/to/llvm-build
+  $ ninja find-all-symbols // build find-all-symbols tool.
+  $ ninja clang-include-fixer // build clang-include-fixer tool.
   $ ls compile_commands.json # Make sure compile_commands.json exists.
 compile_commands.json
   $ path/to/llvm/source/tools/clang/tools/extra/include-fixer/find-all-symbols/tool/run-find-all-symbols.py
 ... wait as clang indexes the code base ...
   $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database into the source tree.
   $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link compilation database if it's not there already.
   $ cd path/to/llvm/source
-  $ clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
+  $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
 Added #include "foo.h"
 
 Integrate with Vim
@@ -63,6 +65,14 @@
 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change ``,cf`` to
 another binding if you need clang-include-fixer on a different key.
 
+Make sure vim can find :program:`clang-include-fixer`:
+
+- Add the path to :program:`clang-include-fixer` to the PATH environment variable.
+- Or set ``g:clang_include_fixer_path`` in vimrc: ``let g:clang_include_fixer_path=path/to/clang-include-fixer``
+
+You can custonmize the number of headers being showed by setting
+``let g:clang_include_fixer_maximum_suggested_headers=5``
+
 See ``clang-include-fixer.py`` for more details.
 
 How it Works
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -44,7 +44,8 @@
 Major New Features
 --
 
-- Feature1...
+- :program:`clang-include-fixer`, a tool that provides an automated way of
+  adding ``#include`` directives for missing symbols in one translation unit.
 
 Improvements to clang-query
 ---
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20909: [clang-tidy] Ignore function context in misc-unused-using-decls.

2016-06-03 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL271632: [clang-tidy] Ignore function context in 
misc-unused-using-decls. (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D20909?vs=59492=59494#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20909

Files:
  clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h
  clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp

Index: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
===
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
@@ -17,6 +17,8 @@
   static int ii;
 };
 template  class J {};
+class G;
+class H;
 
 class Base {
  public:
@@ -99,6 +101,24 @@
 USING_FUNC
 #undef USING_FUNC
 
+namespace N1 {
+// n::G is used in namespace N2.
+// Currently, the check doesn't support multiple scopes. All the relevant
+// using-decls will be marked as used once we see an usage even the usage is in
+// other scope.
+using n::G;
+}
+
+namespace N2 {
+using n::G;
+void f(G g);
+}
+
+void IgnoreFunctionScope() {
+// Using-decls defined in function scope will be ignored.
+using n::H;
+}
+
 // - Usages -
 void f(B b);
 void g() {
@@ -112,4 +132,3 @@
   UsedTemplateFunc();
   cout << endl;
 }
-
Index: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h
===
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -36,9 +36,15 @@
   struct UsingDeclContext {
 explicit UsingDeclContext(const UsingDecl *FoundUsingDecl)
 : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {}
+// A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl
+// can introduce multiple UsingShadowDecls in some cases (such as
+// overloaded functions).
 llvm::SmallPtrSet UsingTargetDecls;
+// The original UsingDecl.
 const UsingDecl *FoundUsingDecl;
+// The source range of the UsingDecl.
 CharSourceRange UsingDeclRange;
+// Whether the UsingDecl is used.
 bool IsUsed;
   };
 
Index: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -41,11 +41,17 @@
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
 // Ignores using-declarations defined in macros.
 if (Using->getLocation().isMacroID())
-  return ;
+  return;
 
 // Ignores using-declarations defined in class definition.
 if (isa(Using->getDeclContext()))
-  return ;
+  return;
+
+// FIXME: We ignore using-decls defined in function definitions at the
+// moment because of false positives caused by ADL and different function
+// scopes.
+if (isa(Using->getDeclContext()))
+  return;
 
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
@@ -97,11 +103,14 @@
 }
 
 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
+  // FIXME: Currently, we don't handle the using-decls being used in different
+  // scopes (such as different namespaces, different functions). Instead of
+  // giving an incorrect message, we mark all of them as used.
+  //
+  // FIXME: Use a more efficient way to find a matching context.
   for (auto  : Contexts) {
-if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0) {
+if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
   Context.IsUsed = true;
-  break;
-}
   }
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r271632 - [clang-tidy] Ignore function context in misc-unused-using-decls.

2016-06-03 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Fri Jun  3 03:05:11 2016
New Revision: 271632

URL: http://llvm.org/viewvc/llvm-project?rev=271632=rev
Log:
[clang-tidy] Ignore function context in misc-unused-using-decls.

Summary: Make the check's behavior more correct when handling using-decls in 
multiple scopes.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20909

Modified:
clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h
clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp?rev=271632=271631=271632=diff
==
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Fri Jun  
3 03:05:11 2016
@@ -41,11 +41,17 @@ void UnusedUsingDeclsCheck::check(const
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
 // Ignores using-declarations defined in macros.
 if (Using->getLocation().isMacroID())
-  return ;
+  return;
 
 // Ignores using-declarations defined in class definition.
 if (isa(Using->getDeclContext()))
-  return ;
+  return;
+
+// FIXME: We ignore using-decls defined in function definitions at the
+// moment because of false positives caused by ADL and different function
+// scopes.
+if (isa(Using->getDeclContext()))
+  return;
 
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
@@ -97,11 +103,14 @@ void UnusedUsingDeclsCheck::check(const
 }
 
 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
+  // FIXME: Currently, we don't handle the using-decls being used in different
+  // scopes (such as different namespaces, different functions). Instead of
+  // giving an incorrect message, we mark all of them as used.
+  //
+  // FIXME: Use a more efficient way to find a matching context.
   for (auto  : Contexts) {
-if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0) {
+if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
   Context.IsUsed = true;
-  break;
-}
   }
 }
 

Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h?rev=271632=271631=271632=diff
==
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.h Fri Jun  3 
03:05:11 2016
@@ -36,9 +36,15 @@ private:
   struct UsingDeclContext {
 explicit UsingDeclContext(const UsingDecl *FoundUsingDecl)
 : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {}
+// A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl
+// can introduce multiple UsingShadowDecls in some cases (such as
+// overloaded functions).
 llvm::SmallPtrSet UsingTargetDecls;
+// The original UsingDecl.
 const UsingDecl *FoundUsingDecl;
+// The source range of the UsingDecl.
 CharSourceRange UsingDeclRange;
+// Whether the UsingDecl is used.
 bool IsUsed;
   };
 

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp?rev=271632=271631=271632=diff
==
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp 
(original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp Fri Jun 
 3 03:05:11 2016
@@ -17,6 +17,8 @@ class I {
   static int ii;
 };
 template  class J {};
+class G;
+class H;
 
 class Base {
  public:
@@ -99,6 +101,24 @@ DEFINE_INT(test);
 USING_FUNC
 #undef USING_FUNC
 
+namespace N1 {
+// n::G is used in namespace N2.
+// Currently, the check doesn't support multiple scopes. All the relevant
+// using-decls will be marked as used once we see an usage even the usage is in
+// other scope.
+using n::G;
+}
+
+namespace N2 {
+using n::G;
+void f(G g);
+}
+
+void IgnoreFunctionScope() {
+// Using-decls defined in function scope will be ignored.
+using n::H;
+}
+
 // - Usages -
 void f(B b);
 void g() {
@@ -112,4 +132,3 @@ void g() {
   UsedTemplateFunc();
   cout << endl;
 }
-


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D20950: [include-fixer] Don't add missing header if the unindentified symbol isn't from the main file.

2016-06-03 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: bkramer.
hokein added a subscriber: cfe-commits.

The further solution is to add the missing header to the file where the
symbol comes from.

http://reviews.llvm.org/D20950

Files:
  include-fixer/IncludeFixer.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -44,6 +44,8 @@
   llvm::MemoryBuffer::getMemBuffer("\n"));
   InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
   llvm::MemoryBuffer::getMemBuffer("\n"));
+  InMemoryFileSystem->addFile("header.h", 0,
+  llvm::MemoryBuffer::getMemBuffer("bar b;"));
   return Invocation.run();
 }
 
@@ -186,6 +188,11 @@
 runIncludeFixer("int test = a::b::Green;\n"));
 }
 
+TEST(IncludeFixer, IgnoreSymbolFromHeader) {
+  std::string Code = "#include \"header.h\"";
+  EXPECT_EQ(Code, runIncludeFixer(Code));
+}
+
 // FIXME: add test cases for inserting and sorting multiple headers when
 // include-fixer supports multiple headers insertion.
 TEST(IncludeFixer, InsertAndSortSingleHeader) {
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -86,6 +86,15 @@
 if (getCompilerInstance().getSema().isSFINAEContext())
   return clang::TypoCorrection();
 
+// We currently ignore the unidentified symbol which is not from the
+// main file.
+//
+// FIXME: Add the missing header to the header file where the symbol comes
+// from.
+if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
+Typo.getLoc()))
+  return clang::TypoCorrection();
+
 std::string TypoScopeString;
 if (S) {
   // FIXME: Currently we only use namespace contexts. Use other context


Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -44,6 +44,8 @@
   llvm::MemoryBuffer::getMemBuffer("\n"));
   InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
   llvm::MemoryBuffer::getMemBuffer("\n"));
+  InMemoryFileSystem->addFile("header.h", 0,
+  llvm::MemoryBuffer::getMemBuffer("bar b;"));
   return Invocation.run();
 }
 
@@ -186,6 +188,11 @@
 runIncludeFixer("int test = a::b::Green;\n"));
 }
 
+TEST(IncludeFixer, IgnoreSymbolFromHeader) {
+  std::string Code = "#include \"header.h\"";
+  EXPECT_EQ(Code, runIncludeFixer(Code));
+}
+
 // FIXME: add test cases for inserting and sorting multiple headers when
 // include-fixer supports multiple headers insertion.
 TEST(IncludeFixer, InsertAndSortSingleHeader) {
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -86,6 +86,15 @@
 if (getCompilerInstance().getSema().isSFINAEContext())
   return clang::TypoCorrection();
 
+// We currently ignore the unidentified symbol which is not from the
+// main file.
+//
+// FIXME: Add the missing header to the header file where the symbol comes
+// from.
+if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
+Typo.getLoc()))
+  return clang::TypoCorrection();
+
 std::string TypoScopeString;
 if (S) {
   // FIXME: Currently we only use namespace contexts. Use other context
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20909: [clang-tidy] Ignore function context in misc-unused-using-decls.

2016-06-03 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59492.
hokein added a comment.

Address code comments.


http://reviews.llvm.org/D20909

Files:
  clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  clang-tidy/misc/UnusedUsingDeclsCheck.h
  test/clang-tidy/misc-unused-using-decls.cpp

Index: test/clang-tidy/misc-unused-using-decls.cpp
===
--- test/clang-tidy/misc-unused-using-decls.cpp
+++ test/clang-tidy/misc-unused-using-decls.cpp
@@ -17,6 +17,8 @@
   static int ii;
 };
 template  class J {};
+class G;
+class H;
 
 class Base {
  public:
@@ -99,6 +101,24 @@
 USING_FUNC
 #undef USING_FUNC
 
+namespace N1 {
+// n::G is used in namespace N2.
+// Currently, the check doesn't support multiple scopes. All the relevant
+// using-decls will be marked as used once we see an usage even the usage is in
+// other scope.
+using n::G;
+}
+
+namespace N2 {
+using n::G;
+void f(G g);
+}
+
+void IgnoreFunctionScope() {
+// Using-decls defined in function scope will be ignored.
+using n::H;
+}
+
 // - Usages -
 void f(B b);
 void g() {
@@ -112,4 +132,3 @@
   UsedTemplateFunc();
   cout << endl;
 }
-
Index: clang-tidy/misc/UnusedUsingDeclsCheck.h
===
--- clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -36,9 +36,15 @@
   struct UsingDeclContext {
 explicit UsingDeclContext(const UsingDecl *FoundUsingDecl)
 : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {}
+// A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl
+// can introduce multiple UsingShadowDecls in some cases (such as
+// overloaded functions).
 llvm::SmallPtrSet UsingTargetDecls;
+// The original UsingDecl.
 const UsingDecl *FoundUsingDecl;
+// The source range of the UsingDecl.
 CharSourceRange UsingDeclRange;
+// Whether the UsingDecl is used.
 bool IsUsed;
   };
 
Index: clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -41,11 +41,17 @@
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
 // Ignores using-declarations defined in macros.
 if (Using->getLocation().isMacroID())
-  return ;
+  return;
 
 // Ignores using-declarations defined in class definition.
 if (isa(Using->getDeclContext()))
-  return ;
+  return;
+
+// FIXME: We ignore using-decls defined in function definitions at the
+// moment because of false positives caused by ADL and different function
+// scopes.
+if (isa(Using->getDeclContext()))
+  return;
 
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
@@ -97,11 +103,14 @@
 }
 
 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
+  // FIXME: Currently, we don't handle the using-decls being used in different
+  // scopes (such as different namespaces, different functions). Instead of
+  // giving an incorrect message, we mark all of them as used.
+  //
+  // FIXME: Use a more efficient way to find a matching context.
   for (auto  : Contexts) {
-if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0) {
+if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
   Context.IsUsed = true;
-  break;
-}
   }
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r271660 - [include-fixer] Don't add missing header if the unindentified symbol isn't from the main file.

2016-06-03 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Fri Jun  3 06:26:02 2016
New Revision: 271660

URL: http://llvm.org/viewvc/llvm-project?rev=271660=rev
Log:
[include-fixer] Don't add missing header if the unindentified symbol isn't from 
the main file.

Summary:
The further solution is to add the missing header to the file where the
symbol comes from.

Reviewers: bkramer

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20950

Modified:
clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp?rev=271660=271659=271660=diff
==
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Fri Jun  3 06:26:02 
2016
@@ -86,6 +86,29 @@ public:
 if (getCompilerInstance().getSema().isSFINAEContext())
   return clang::TypoCorrection();
 
+// We currently ignore the unidentified symbol which is not from the
+// main file.
+//
+// However, this is not always true due to templates in a non-self 
contained
+// header, consider the case:
+//
+//   // header.h
+//   template 
+//   class Foo {
+// T t;
+//   };
+//
+//   // test.cc
+//   // We need to add  in test.cc instead of header.h.
+//   class Bar;
+//   Foo foo;
+//
+// FIXME: Add the missing header to the header file where the symbol comes
+// from.
+if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
+Typo.getLoc()))
+  return clang::TypoCorrection();
+
 std::string TypoScopeString;
 if (S) {
   // FIXME: Currently we only use namespace contexts. Use other context

Modified: clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp?rev=271660=271659=271660=diff
==
--- clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp 
(original)
+++ clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp Fri 
Jun  3 06:26:02 2016
@@ -44,6 +44,8 @@ static bool runOnCode(tooling::ToolActio
   llvm::MemoryBuffer::getMemBuffer("\n"));
   InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
   llvm::MemoryBuffer::getMemBuffer("\n"));
+  InMemoryFileSystem->addFile("header.h", 0,
+  llvm::MemoryBuffer::getMemBuffer("bar b;"));
   return Invocation.run();
 }
 
@@ -186,6 +188,11 @@ TEST(IncludeFixer, EnumConstantSymbols)
 runIncludeFixer("int test = a::b::Green;\n"));
 }
 
+TEST(IncludeFixer, IgnoreSymbolFromHeader) {
+  std::string Code = "#include \"header.h\"";
+  EXPECT_EQ(Code, runIncludeFixer(Code));
+}
+
 // FIXME: add test cases for inserting and sorting multiple headers when
 // include-fixer supports multiple headers insertion.
 TEST(IncludeFixer, InsertAndSortSingleHeader) {


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20950: [include-fixer] Don't add missing header if the unindentified symbol isn't from the main file.

2016-06-03 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL271660: [include-fixer] Don't add missing header if the 
unindentified symbol isn't… (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D20950?vs=59496=59529#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20950

Files:
  clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
  clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp

Index: clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
===
--- clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
+++ clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
@@ -44,6 +44,8 @@
   llvm::MemoryBuffer::getMemBuffer("\n"));
   InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
   llvm::MemoryBuffer::getMemBuffer("\n"));
+  InMemoryFileSystem->addFile("header.h", 0,
+  llvm::MemoryBuffer::getMemBuffer("bar b;"));
   return Invocation.run();
 }
 
@@ -186,6 +188,11 @@
 runIncludeFixer("int test = a::b::Green;\n"));
 }
 
+TEST(IncludeFixer, IgnoreSymbolFromHeader) {
+  std::string Code = "#include \"header.h\"";
+  EXPECT_EQ(Code, runIncludeFixer(Code));
+}
+
 // FIXME: add test cases for inserting and sorting multiple headers when
 // include-fixer supports multiple headers insertion.
 TEST(IncludeFixer, InsertAndSortSingleHeader) {
Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
@@ -86,6 +86,29 @@
 if (getCompilerInstance().getSema().isSFINAEContext())
   return clang::TypoCorrection();
 
+// We currently ignore the unidentified symbol which is not from the
+// main file.
+//
+// However, this is not always true due to templates in a non-self 
contained
+// header, consider the case:
+//
+//   // header.h
+//   template 
+//   class Foo {
+// T t;
+//   };
+//
+//   // test.cc
+//   // We need to add  in test.cc instead of header.h.
+//   class Bar;
+//   Foo foo;
+//
+// FIXME: Add the missing header to the header file where the symbol comes
+// from.
+if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
+Typo.getLoc()))
+  return clang::TypoCorrection();
+
 std::string TypoScopeString;
 if (S) {
   // FIXME: Currently we only use namespace contexts. Use other context


Index: clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
===
--- clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
+++ clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
@@ -44,6 +44,8 @@
   llvm::MemoryBuffer::getMemBuffer("\n"));
   InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
   llvm::MemoryBuffer::getMemBuffer("\n"));
+  InMemoryFileSystem->addFile("header.h", 0,
+  llvm::MemoryBuffer::getMemBuffer("bar b;"));
   return Invocation.run();
 }
 
@@ -186,6 +188,11 @@
 runIncludeFixer("int test = a::b::Green;\n"));
 }
 
+TEST(IncludeFixer, IgnoreSymbolFromHeader) {
+  std::string Code = "#include \"header.h\"";
+  EXPECT_EQ(Code, runIncludeFixer(Code));
+}
+
 // FIXME: add test cases for inserting and sorting multiple headers when
 // include-fixer supports multiple headers insertion.
 TEST(IncludeFixer, InsertAndSortSingleHeader) {
Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
@@ -86,6 +86,29 @@
 if (getCompilerInstance().getSema().isSFINAEContext())
   return clang::TypoCorrection();
 
+// We currently ignore the unidentified symbol which is not from the
+// main file.
+//
+// However, this is not always true due to templates in a non-self contained
+// header, consider the case:
+//
+//   // header.h
+//   template 
+//   class Foo {
+// T t;
+//   };
+//
+//   // test.cc
+//   // We need to add  in test.cc instead of header.h.
+//   class Bar;
+//   Foo foo;
+//
+// FIXME: Add the missing header to the header file where the symbol comes
+// from.
+if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
+Typo.getLoc()))
+  return clang::TypoCorrection();
+
 std::string TypoScopeString;
 if (S) {
   // FIXME: Currently we only use namespace contexts. Use other context

Re: [PATCH] D20581: [include-fixer] Simplify the code since we won't handle multiple includes at once.

2016-05-25 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL270700: [include-fixer] Simplify the code since we won't 
handle multiple includes at… (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D20581?vs=58272=58412#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20581

Files:
  clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp

Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
@@ -282,64 +282,46 @@
clang::HeaderSearch ,
std::set ,
std::vector ) {
-if (Untried.empty())
+if (SymbolQueryResults.empty())
   return false;
 
-const auto  = UntriedList.front();
+// FIXME: Rank the results and pick the best one instead of the first one.
+const auto  = SymbolQueryResults.front();
 Headers.insert(minimizeInclude(ToTry, SourceManager, HeaderSearch));
 
 StringRef Code = 
SourceManager.getBufferData(SourceManager.getMainFileID());
 Replacements = CreateReplacementsForHeaders(Code, Headers);
 
 // We currently abort after the first inserted include. The more
 // includes we have the less safe this becomes due to error recovery
 // changing the results.
-// FIXME: Handle multiple includes at once.
 return true;
   }
 
   /// Sets the location at the very top of the file.
   void setFileBegin(clang::SourceLocation Location) { FileBegin = Location; }
 
-  /// Add an include to the set of includes to try.
-  /// \param include_path The include path to try.
-  void TryInclude(const std::string , const std::string _path) {
-if (Untried.insert(include_path).second)
-  UntriedList.push_back(include_path);
-  }
-
 private:
   /// Query the database for a given identifier.
   bool query(StringRef Query, SourceLocation Loc) {
 assert(!Query.empty() && "Empty query!");
 
-// Save database lookups by not looking up identifiers multiple times.
-if (!SeenQueries.insert(Query).second)
-  return true;
+// Skip other identifers once we have discovered an identfier successfully.
+if (!SymbolQueryResults.empty())
+  return false;
 
 DEBUG(llvm::dbgs() << "Looking up '" << Query << "' at ");
 DEBUG(Loc.print(llvm::dbgs(), getCompilerInstance().getSourceManager()));
 DEBUG(llvm::dbgs() << " ...");
 
-std::string error_text;
-auto SearchReply = SymbolIndexMgr.search(Query);
-DEBUG(llvm::dbgs() << SearchReply.size() << " replies\n");
-if (SearchReply.empty())
-  return false;
-
-// Add those files to the set of includes to try out.
-// FIXME: Rank the results and pick the best one instead of the first one.
-TryInclude(Query, SearchReply[0]);
-
-return true;
+SymbolQueryResults = SymbolIndexMgr.search(Query);
+DEBUG(llvm::dbgs() << SymbolQueryResults.size() << " replies\n");
+return !SymbolQueryResults.empty();
   }
 
   /// The client to use to find cross-references.
   SymbolIndexManager 
 
-  // Remeber things we looked up to avoid querying things twice.
-  llvm::StringSet<> SeenQueries;
-
   /// The absolute path to the file being processed.
   std::string Filename;
 
@@ -354,11 +336,9 @@
   /// clang-format config file found.
   std::string FallbackStyle;
 
-  /// Includes we have left to try. A set to unique them and a list to keep
-  /// track of the order. We prefer includes that were discovered early to 
avoid
-  /// getting caught in results from error recovery.
-  std::set Untried;
-  std::vector UntriedList;
+  /// The query results of an identifier. We only include the first discovered
+  /// identifier to avoid getting caught in results from error recovery.
+  std::vector SymbolQueryResults;
 
   /// Whether we should use the smallest possible include path.
   bool MinimizeIncludePaths = true;


Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
@@ -282,64 +282,46 @@
clang::HeaderSearch ,
std::set ,
std::vector ) {
-if (Untried.empty())
+if (SymbolQueryResults.empty())
   return false;
 
-const auto  = UntriedList.front();
+// FIXME: Rank the results and pick the best one instead of the first one.
+const auto  = SymbolQueryResults.front();
 Headers.insert(minimizeInclude(ToTry, SourceManager, HeaderSearch));
 
 StringRef Code = SourceManager.getBufferData(SourceManager.getMainFileID());
 Replacements = CreateReplacementsForHeaders(Code, Headers);
 
 // We currently abort after the first inserted include. The more
 // includes we have the less safe this becomes due to error recovery

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-25 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 58420.
hokein added a comment.

Fix a nit.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::CreateReplacementsForHeader(
+Code, "input.cc", "llvm", FixerContext.FirstIncludeOffset,
+FixerContext.Headers.front());
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -28,6 +28,22 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+choosing_mode = False;
+if vim.eval('exists("g:clang_include_fixer_choosing_mode")') == "1":
+  choosing_mode = vim.eval('g:clang_include_fixer_choosing_mode')
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message, choices, default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,13 +57,34 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
+  if choosing_mode:
+# Run command to get all headers.
+command = [binary, "-output-headers", "-db="+args.db, "-input="+args.input,
+   vim.current.buffer.name]
+stdout, stderr = execute(command, text)
+lines = stdout.splitlines()
+if len(lines) == 0:
+  return
+symbol = lines[0]
+choices_message = ""
+index = 1;
+for line in lines[1:]:
+  choices_message += "&" + str(index) + line + "\n"
+  index += 1
+select = ShowDialog("choose a header file for {0}.".format(symbol), choices_message)
+# Insert a selected header.
+command = [binary, "-insert-header="+lines[select], vim.current.buffer.name]
+stdout, stderr = execute(command, text)
+vim.current.buffer[:] = None;
+vim.current.buffer.append(stdout.splitlines())
+print "Added #include {0}.".format(lines[select])
+return;
+
+
   # Call clang-include-fixer.
   command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
 
   # If successful, replace buffer contents.
   if stderr:
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -9,13 +9,16 @@
 
 #include "InMemorySymbolIndex.h"
 #include "IncludeFixer.h"
+#include "IncludeFixerContext.h"
 #include "SymbolIndexManager.h"
 #include "YamlSymbolIndex.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "clang/Tooling/CommonOptionsParser.h"
+#include "clang/Tooling/ReplacementsYaml.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
@@ -56,6 +59,23 @@
"used for editor integration."),
   cl::init(false), cl::cat(IncludeFixerCategory));
 
+cl::opt OuputHeaders(
+"output-headers",
+cl::desc("Output the queried symbol and its relevant headers.\n"
+ "The first line is the symbol name. The other lines\n"
+ "are the headers: \n"
+ "   b::foo\n"
+ "   

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-25 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 58415.
hokein added a comment.

Rebase


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::CreateReplacementsForHeader(
+Code, "input.cc", "llvm", FixerContext.FirstIncludeOffset,
+FixerContext.Headers.front());
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -28,6 +28,22 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+choosing_mode = False;
+if vim.eval('exists("g:clang_include_fixer_choosing_mode")') == "1":
+  choosing_mode = vim.eval('g:clang_include_fixer_choosing_mode')
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message, choices, default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,13 +57,34 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
+  if choosing_mode:
+# Run command to get all headers.
+command = [binary, "-output-headers", "-db="+args.db, "-input="+args.input,
+   vim.current.buffer.name]
+stdout, stderr = execute(command, text)
+lines = stdout.splitlines()
+if len(lines) == 0:
+  return
+symbol = lines[0]
+choices_message = ""
+index = 1;
+for line in lines[1:]:
+  choices_message += "&" + str(index) + line + "\n"
+  index += 1
+select = ShowDialog("choose a header file for {0}.".format(symbol), choices_message)
+# Insert a selected header.
+command = [binary, "-insert-header="+lines[select], vim.current.buffer.name]
+stdout, stderr = execute(command, text)
+vim.current.buffer[:] = None;
+vim.current.buffer.append(stdout.splitlines())
+print "Added #include {0}.".format(lines[select])
+return;
+
+
   # Call clang-include-fixer.
   command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
 
   # If successful, replace buffer contents.
   if stderr:
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -9,13 +9,16 @@
 
 #include "InMemorySymbolIndex.h"
 #include "IncludeFixer.h"
+#include "IncludeFixerContext.h"
 #include "SymbolIndexManager.h"
 #include "YamlSymbolIndex.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "clang/Tooling/CommonOptionsParser.h"
+#include "clang/Tooling/ReplacementsYaml.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
@@ -56,6 +59,23 @@
"used for editor integration."),
   cl::init(false), cl::cat(IncludeFixerCategory));
 
+cl::opt OuputHeaders(
+"output-headers",
+cl::desc("Output the queried symbol and its relevant headers.\n"
+ "The first line is the symbol name. The other lines\n"
+ "are the headers: \n"
+ "   b::foo\n"
+ "   

[PATCH] D21278: Fix an enum-compare compliation warning message.

2016-06-13 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: klimek.
hokein added subscribers: cfe-commits, spatel.

A follow-up fix on D21235.

http://reviews.llvm.org/D21278

Files:
  lib/Target/X86/X86ISelLowering.cpp

Index: lib/Target/X86/X86ISelLowering.cpp
===
--- lib/Target/X86/X86ISelLowering.cpp
+++ lib/Target/X86/X86ISelLowering.cpp
@@ -15185,12 +15185,14 @@
   if (SetCCOpcode == ISD::SETUEQ) {
 CC0 = 3; // UNORD
 CC1 = 0; // EQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FOR : ISD::OR;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FOR) :
+   static_cast(ISD::OR);
   } else {
 assert(SetCCOpcode == ISD::SETONE);
 CC0 = 7; // ORD
 CC1 = 4; // NEQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FAND : ISD::AND;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FAND) 
:
+   static_cast(ISD::AND);
   }
 
   SDValue Cmp0 = DAG.getNode(Opc, dl, VT, Op0, Op1,


Index: lib/Target/X86/X86ISelLowering.cpp
===
--- lib/Target/X86/X86ISelLowering.cpp
+++ lib/Target/X86/X86ISelLowering.cpp
@@ -15185,12 +15185,14 @@
   if (SetCCOpcode == ISD::SETUEQ) {
 CC0 = 3; // UNORD
 CC1 = 0; // EQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FOR : ISD::OR;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FOR) :
+   static_cast(ISD::OR);
   } else {
 assert(SetCCOpcode == ISD::SETONE);
 CC0 = 7; // ORD
 CC1 = 4; // NEQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FAND : ISD::AND;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FAND) :
+   static_cast(ISD::AND);
   }
 
   SDValue Cmp0 = DAG.getNode(Opc, dl, VT, Op0, Op1,
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21278: Fix an enumeral mismatch warning.

2016-06-13 Thread Haojian Wu via cfe-commits
hokein added a comment.

In http://reviews.llvm.org/D21278#455850, @klimek wrote:

> LG. Fix the change description when submitting, though - this is not about an 
> enum comparison, right?


Done. This is enumeral mismatch indeed.


http://reviews.llvm.org/D21278



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21278: Fix an enumeral mismatch warning.

2016-06-13 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL272539: Fix an enumeral mismatch warning. (authored by 
hokein).

Changed prior to commit:
  http://reviews.llvm.org/D21278?vs=60494=60498#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D21278

Files:
  llvm/trunk/lib/Target/X86/X86ISelLowering.cpp

Index: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
===
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
@@ -15185,12 +15185,14 @@
   if (SetCCOpcode == ISD::SETUEQ) {
 CC0 = 3; // UNORD
 CC1 = 0; // EQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FOR : ISD::OR;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FOR) :
+   static_cast(ISD::OR);
   } else {
 assert(SetCCOpcode == ISD::SETONE);
 CC0 = 7; // ORD
 CC1 = 4; // NEQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FAND : ISD::AND;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FAND) 
:
+   static_cast(ISD::AND);
   }
 
   SDValue Cmp0 = DAG.getNode(Opc, dl, VT, Op0, Op1,


Index: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
===
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
@@ -15185,12 +15185,14 @@
   if (SetCCOpcode == ISD::SETUEQ) {
 CC0 = 3; // UNORD
 CC1 = 0; // EQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FOR : ISD::OR;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FOR) :
+   static_cast(ISD::OR);
   } else {
 assert(SetCCOpcode == ISD::SETONE);
 CC0 = 7; // ORD
 CC1 = 4; // NEQ
-CombineOpc = Opc == X86ISD::CMPP ? X86ISD::FAND : ISD::AND;
+CombineOpc = Opc == X86ISD::CMPP ? static_cast(X86ISD::FAND) :
+   static_cast(ISD::AND);
   }
 
   SDValue Cmp0 = DAG.getNode(Opc, dl, VT, Op0, Op1,
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D21371: [include-fixer] Correct two wrong header mappings.

2016-06-15 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL272773: [include-fixer] Correct two wrong header mappings. 
(authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D21371?vs=60812=60814#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D21371

Files:
  clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp

Index: 
clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
===
--- 
clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
+++ 
clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
@@ -111,7 +111,7 @@
   {"bits/hashtable.h", ""},
   {"bits/hashtable_policy.h", ""},
   {"bits/indirect_array.h", ""},
-  {"bits/ios_base.h", ""},
+  {"bits/ios_base.h", ""},
   {"bits/istream.tcc", ""},
   {"bits/list.tcc", ""},
   {"bits/locale_classes.h", ""},
@@ -171,7 +171,7 @@
   {"bits/stl_vector.h", ""},
   {"bits/stream_iterator.h", ""},
   {"bits/streambuf.tcc", ""},
-  {"bits/streambuf_iterator.h", ""},
+  {"bits/streambuf_iterator.h", ""},
   {"bits/stringfwd.h", ""},
   {"bits/unique_ptr.h", ""},
   {"bits/unordered_map.h", ""},


Index: clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
===
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
@@ -111,7 +111,7 @@
   {"bits/hashtable.h", ""},
   {"bits/hashtable_policy.h", ""},
   {"bits/indirect_array.h", ""},
-  {"bits/ios_base.h", ""},
+  {"bits/ios_base.h", ""},
   {"bits/istream.tcc", ""},
   {"bits/list.tcc", ""},
   {"bits/locale_classes.h", ""},
@@ -171,7 +171,7 @@
   {"bits/stl_vector.h", ""},
   {"bits/stream_iterator.h", ""},
   {"bits/streambuf.tcc", ""},
-  {"bits/streambuf_iterator.h", ""},
+  {"bits/streambuf_iterator.h", ""},
   {"bits/stringfwd.h", ""},
   {"bits/unique_ptr.h", ""},
   {"bits/unordered_map.h", ""},
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D21371: [include-fixer] Correct two wrong header mappings.

2016-06-15 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: bkramer.
hokein added subscribers: cfe-commits, ioeric.

http://reviews.llvm.org/D21371

Files:
  include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp

Index: include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
===
--- include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
+++ include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
@@ -111,7 +111,7 @@
   {"bits/hashtable.h", ""},
   {"bits/hashtable_policy.h", ""},
   {"bits/indirect_array.h", ""},
-  {"bits/ios_base.h", ""},
+  {"bits/ios_base.h", ""},
   {"bits/istream.tcc", ""},
   {"bits/list.tcc", ""},
   {"bits/locale_classes.h", ""},
@@ -171,7 +171,7 @@
   {"bits/stl_vector.h", ""},
   {"bits/stream_iterator.h", ""},
   {"bits/streambuf.tcc", ""},
-  {"bits/streambuf_iterator.h", ""},
+  {"bits/streambuf_iterator.h", ""},
   {"bits/stringfwd.h", ""},
   {"bits/unique_ptr.h", ""},
   {"bits/unordered_map.h", ""},


Index: include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
===
--- include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
+++ include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
@@ -111,7 +111,7 @@
   {"bits/hashtable.h", ""},
   {"bits/hashtable_policy.h", ""},
   {"bits/indirect_array.h", ""},
-  {"bits/ios_base.h", ""},
+  {"bits/ios_base.h", ""},
   {"bits/istream.tcc", ""},
   {"bits/list.tcc", ""},
   {"bits/locale_classes.h", ""},
@@ -171,7 +171,7 @@
   {"bits/stl_vector.h", ""},
   {"bits/stream_iterator.h", ""},
   {"bits/streambuf.tcc", ""},
-  {"bits/streambuf_iterator.h", ""},
+  {"bits/streambuf_iterator.h", ""},
   {"bits/stringfwd.h", ""},
   {"bits/unique_ptr.h", ""},
   {"bits/unordered_map.h", ""},
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r272773 - [include-fixer] Correct two wrong header mappings.

2016-06-15 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Wed Jun 15 06:15:12 2016
New Revision: 272773

URL: http://llvm.org/viewvc/llvm-project?rev=272773=rev
Log:
[include-fixer] Correct two wrong header mappings.

Reviewers: bkramer

Subscribers: ioeric, cfe-commits

Differential Revision: http://reviews.llvm.org/D21371

Modified:

clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp

Modified: 
clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp?rev=272773=272772=272773=diff
==
--- 
clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp 
(original)
+++ 
clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp 
Wed Jun 15 06:15:12 2016
@@ -111,7 +111,7 @@ const HeaderMapCollector::HeaderMap *get
   {"bits/hashtable.h", ""},
   {"bits/hashtable_policy.h", ""},
   {"bits/indirect_array.h", ""},
-  {"bits/ios_base.h", ""},
+  {"bits/ios_base.h", ""},
   {"bits/istream.tcc", ""},
   {"bits/list.tcc", ""},
   {"bits/locale_classes.h", ""},
@@ -171,7 +171,7 @@ const HeaderMapCollector::HeaderMap *get
   {"bits/stl_vector.h", ""},
   {"bits/stream_iterator.h", ""},
   {"bits/streambuf.tcc", ""},
-  {"bits/streambuf_iterator.h", ""},
+  {"bits/streambuf_iterator.h", ""},
   {"bits/stringfwd.h", ""},
   {"bits/unique_ptr.h", ""},
   {"bits/unordered_map.h", ""},


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D21132: [include-fixer] Keep dot dot in SymbolInfo file paths.

2016-06-08 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added reviewers: bkramer, ioeric.
hokein added a subscriber: cfe-commits.

Currently, removing dot dot in header's path doesn't make include-fixer
minimize path correctly in some cases, for example, specify a relative search
path based on the build directory("-I../include/").

Besides, removing dot dot can break symbolic link directories. So don't
removing it for now.

http://reviews.llvm.org/D21132

Files:
  include-fixer/find-all-symbols/PathConfig.cpp
  include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
  test/include-fixer/include_path.cpp
  unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp

Index: unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
===
--- unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
+++ unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
@@ -105,7 +105,7 @@
   "#include \"internal/internal.h\"";
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 // Test path cleaning for both decls and macros.
-const std::string DirtyHeader = "./internal/../internal/./a/b.h";
+const std::string DirtyHeader = "./internal/./a/b.h";
 Content += "\n#include \"" + DirtyHeader + "\"";
 const std::string CleanHeader = "internal/a/b.h";
 const std::string DirtyHeaderContent =
Index: test/include-fixer/include_path.cpp
===
--- test/include-fixer/include_path.cpp
+++ test/include-fixer/include_path.cpp
@@ -1,14 +1,20 @@
 // REQUIRES: shell
 // RUN: mkdir -p %T/include-fixer/include
+// RUN: mkdir -p %T/include-fixer/symbols
 // RUN: mkdir -p %T/include-fixer/build
 // RUN: mkdir -p %T/include-fixer/src
 // RUN: sed 's|test_dir|%T/include-fixer|g' %S/Inputs/database_template.json > 
%T/include-fixer/build/compile_commands.json
-// RUN: cp %S/Inputs/fake_yaml_db.yaml %T/include-fixer/build/fake_yaml_db.yaml
-// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
-// RUN: touch %T/include-fixer/include/bar.h
+// RUN: echo -e '#include "bar.h"\nb::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: echo 'namespace b { namespace a { class bar {}; } }' > 
%T/include-fixer/include/bar.h
 // RUN: cd %T/include-fixer/build
-// RUN: clang-include-fixer -db=yaml -input=fake_yaml_db.yaml -p=. 
%T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -output-dir=%T/include-fixer/symbols -p=. 
%T/include-fixer/src/bar.cpp
+// RUN: find-all-symbols -merge-dir=%T/include-fixer/symbols 
%T/include-fixer/build/find_all_symbols.yaml
+// RUN: FileCheck -input-file=%T/include-fixer/build/find_all_symbols.yaml 
-check-prefix=CHECK-YAML %s
+//
+// RUN: echo 'b::a::bar f;' > %T/include-fixer/src/bar.cpp
+// RUN: clang-include-fixer -db=yaml 
-input=%T/include-fixer/build/find_all_symbols.yaml -p=. 
%T/include-fixer/src/bar.cpp
 // RUN: FileCheck -input-file=%T/include-fixer/src/bar.cpp %s
 
+// CHECK-YAML: ../include/bar.h
 // CHECK: #include "bar.h"
 // CHECK: b::a::bar f;
Index: include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
===
--- include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
+++ include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
@@ -158,6 +158,5 @@
   auto Factory =
   llvm::make_unique(
   , clang::find_all_symbols::getSTLPostfixHeaderMap());
-  Tool.run(Factory.get());
-  return 0;
+  return Tool.run(Factory.get());
 }
Index: include-fixer/find-all-symbols/PathConfig.cpp
===
--- include-fixer/find-all-symbols/PathConfig.cpp
+++ include-fixer/find-all-symbols/PathConfig.cpp
@@ -33,7 +33,7 @@
   if (Collector)
 FilePath = Collector->getMappedHeader(FilePath);
   SmallString<256> CleanedFilePath = FilePath;
-  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/true);
+  llvm::sys::path::remove_dots(CleanedFilePath, /*remove_dot_dot=*/false);
 
   return CleanedFilePath.str();
 }


Index: unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
===
--- unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
+++ unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
@@ -105,7 +105,7 @@
   "#include \"internal/internal.h\"";
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 // Test path cleaning for both decls and macros.
-const std::string DirtyHeader = "./internal/../internal/./a/b.h";
+const std::string DirtyHeader = "./internal/./a/b.h";
 Content += "\n#include \"" + DirtyHeader + "\"";
 const std::string CleanHeader = "internal/a/b.h";
 const std::string DirtyHeaderContent =
Index: test/include-fixer/include_path.cpp
===
--- test/include-fixer/include_path.cpp
+++ 

Re: [PATCH] D20666: Fix a wrong check in misc-unused-using-decls

2016-05-30 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL271199: Fix a wrong check in misc-unused-using-decls 
(authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D20666?vs=58576=58937#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20666

Files:
  clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp

Index: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -18,20 +18,10 @@
 namespace tidy {
 namespace misc {
 
-// A function that helps to tell whether a TargetDecl will be checked.
-// We only check a TargetDecl if :
-//   * The corresponding UsingDecl is not defined in macros or in class
-// definitions.
-//   * Only variable, function and class types are considered.
+// A function that helps to tell whether a TargetDecl in a UsingDecl will be
+// checked. Only variable, function, function template, class template and 
class
+// are considered.
 static bool ShouldCheckDecl(const Decl *TargetDecl) {
-  // Ignores using-declarations defined in macros.
-  if (TargetDecl->getLocation().isMacroID())
-return false;
-
-  // Ignores using-declarations defined in class definition.
-  if (isa(TargetDecl->getDeclContext()))
-return false;
-
   return isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl);
@@ -49,6 +39,14 @@
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult ) {
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
+// Ignores using-declarations defined in macros.
+if (Using->getLocation().isMacroID())
+  return ;
+
+// Ignores using-declarations defined in class definition.
+if (isa(Using->getDeclContext()))
+  return ;
+
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
 Using->getLocStart(),
Index: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
===
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
@@ -33,6 +33,7 @@
 template  int UsedInTemplateFunc() { return 1; }
 void OverloadFunc(int);
 void OverloadFunc(double);
+int FuncUsedByUsingDeclInMacro() { return 1; }
 
 class ostream {
 public:
@@ -93,6 +94,11 @@
 DEFINE_INT(test);
 #undef DEFIND_INT
 
+#define USING_FUNC \
+  using n::FuncUsedByUsingDeclInMacro;
+USING_FUNC
+#undef USING_FUNC
+
 // - Usages -
 void f(B b);
 void g() {


Index: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -18,20 +18,10 @@
 namespace tidy {
 namespace misc {
 
-// A function that helps to tell whether a TargetDecl will be checked.
-// We only check a TargetDecl if :
-//   * The corresponding UsingDecl is not defined in macros or in class
-// definitions.
-//   * Only variable, function and class types are considered.
+// A function that helps to tell whether a TargetDecl in a UsingDecl will be
+// checked. Only variable, function, function template, class template and class
+// are considered.
 static bool ShouldCheckDecl(const Decl *TargetDecl) {
-  // Ignores using-declarations defined in macros.
-  if (TargetDecl->getLocation().isMacroID())
-return false;
-
-  // Ignores using-declarations defined in class definition.
-  if (isa(TargetDecl->getDeclContext()))
-return false;
-
   return isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl);
@@ -49,6 +39,14 @@
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult ) {
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
+// Ignores using-declarations defined in macros.
+if (Using->getLocation().isMacroID())
+  return ;
+
+// Ignores using-declarations defined in class definition.
+if (isa(Using->getDeclContext()))
+  return ;
+
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
 Using->getLocStart(),
Index: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
===
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
@@ -33,6 +33,7 @@
 template  int UsedInTemplateFunc() { return 1; }
 void OverloadFunc(int);
 void OverloadFunc(double);

[clang-tools-extra] r271199 - Fix a wrong check in misc-unused-using-decls

2016-05-30 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Mon May 30 02:42:22 2016
New Revision: 271199

URL: http://llvm.org/viewvc/llvm-project?rev=271199=rev
Log:
Fix a wrong check in misc-unused-using-decls

Summary:
We should check whether a UsingDecl is defined in macros or in class
definition, not TargetDecls of the UsingDecl.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20666

Modified:
clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp?rev=271199=271198=271199=diff
==
--- clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/UnusedUsingDeclsCheck.cpp Mon May 
30 02:42:22 2016
@@ -18,20 +18,10 @@ namespace clang {
 namespace tidy {
 namespace misc {
 
-// A function that helps to tell whether a TargetDecl will be checked.
-// We only check a TargetDecl if :
-//   * The corresponding UsingDecl is not defined in macros or in class
-// definitions.
-//   * Only variable, function and class types are considered.
+// A function that helps to tell whether a TargetDecl in a UsingDecl will be
+// checked. Only variable, function, function template, class template and 
class
+// are considered.
 static bool ShouldCheckDecl(const Decl *TargetDecl) {
-  // Ignores using-declarations defined in macros.
-  if (TargetDecl->getLocation().isMacroID())
-return false;
-
-  // Ignores using-declarations defined in class definition.
-  if (isa(TargetDecl->getDeclContext()))
-return false;
-
   return isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl);
@@ -49,6 +39,14 @@ void UnusedUsingDeclsCheck::registerMatc
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult ) {
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
+// Ignores using-declarations defined in macros.
+if (Using->getLocation().isMacroID())
+  return ;
+
+// Ignores using-declarations defined in class definition.
+if (isa(Using->getDeclContext()))
+  return ;
+
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
 Using->getLocStart(),

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp?rev=271199=271198=271199=diff
==
--- clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp 
(original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-unused-using-decls.cpp Mon May 
30 02:42:22 2016
@@ -33,6 +33,7 @@ template  int UnusedTemplate
 template  int UsedInTemplateFunc() { return 1; }
 void OverloadFunc(int);
 void OverloadFunc(double);
+int FuncUsedByUsingDeclInMacro() { return 1; }
 
 class ostream {
 public:
@@ -93,6 +94,11 @@ using n::OverloadFunc; // OverloadFunc
 DEFINE_INT(test);
 #undef DEFIND_INT
 
+#define USING_FUNC \
+  using n::FuncUsedByUsingDeclInMacro;
+USING_FUNC
+#undef USING_FUNC
+
 // - Usages -
 void f(B b);
 void g() {


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-30 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 58942.
hokein added a comment.

Remove unneeded headers.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createReplacementsForHeader(
+Code, "input.cc", "llvm", FixerContext.FirstIncludeOffset,
+FixerContext.Headers.front());
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], text)
+print "Added #include {0} for 

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-30 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 58941.
hokein marked 9 inline comments as done.
hokein added a comment.

Address comments.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createReplacementsForHeader(
+Code, "input.cc", "llvm", FixerContext.FirstIncludeOffset,
+FixerContext.Headers.front());
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], text)
+print "Added 

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-30 Thread Haojian Wu via cfe-commits
hokein added a comment.

In http://reviews.llvm.org/D20621#439447, @bkramer wrote:

> Can you add some lit tests for the various command line modes 
> clang-include-fixer has now. We can't reasonably test the vim integration but 
> we can tests the bits it's composed of.


Done.



Comment at: include-fixer/tool/clang-include-fixer.py:49
@@ +48,3 @@
+  default_choice_index)
+  return int(vim.eval(to_eval));
+

ioeric wrote:
> Maybe handle cases where `to_eval` is not a valid index?
vim can handle this for us. 


http://reviews.llvm.org/D20621



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-30 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 58955.
hokein marked an inline comment as done.
hokein added a comment.

Refactor createReplacementsForHeaders.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createInsertHeaderReplacements(
+Code, "input.cc", FixerContext.Headers.front(),
+FixerContext.FirstIncludeOffset);
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], text)
+  

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-30 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 58964.
hokein marked an inline comment as done.
hokein added a comment.

Use format::getStyle to get clang-format style.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createInsertHeaderReplacements(
+Code, "input.cc", FixerContext.Headers.front(),
+FixerContext.FirstIncludeOffset);
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], 

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-30 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: include-fixer/IncludeFixer.h:80
@@ +79,3 @@
+unsigned FirstIncludeOffset=-1U,
+const clang::format::FormatStyle =clang::format::getLLVMStyle());
+

Using a default argument in `Style` can simplify the code in some cases, for 
example in the Unittest. @klimek might have idea? 


http://reviews.llvm.org/D20621



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59018.
hokein marked 2 inline comments as done.
hokein added a comment.

Fix code style.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createInsertHeaderReplacements(
+Code, "input.cc", FixerContext.Headers.front(),
+FixerContext.FirstIncludeOffset);
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], text)
+print "Added 

[PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: bkramer.
hokein added a subscriber: cfe-commits.

And some improvements:
* Show better error messages on unfound symbols.
* Fix a typo.

http://reviews.llvm.org/D20827

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/ranking.cpp

Index: test/include-fixer/ranking.cpp
===
--- test/include-fixer/ranking.cpp
+++ test/include-fixer/ranking.cpp
@@ -1,6 +1,5 @@
-// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s -implicit-check-not=.h
+// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s
 
-// CHECK: "../include/bar.h"
-// CHECK-NEXT: "../include/zbar.h"
+// CHECK: Headers: [ '"../include/bar.h"', '"../include/zbar.h"' ]
 
 bar b;
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
 // CHECK-HEADERS: "foo.h"
 // CHECK-HEADERS: "bar.h"
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -19,6 +19,7 @@
 import difflib
 import subprocess
 import vim
+import yaml
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
 # on the path.
@@ -49,7 +50,7 @@
 
 
 def InsertHeaderToVimBuffer(header, text):
-  command = [binary, "-stdin", "-insert-header="+header,
+  command = [binary, "-stdin", "-insert-header="+yaml.dump(header),
  vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   if stdout:
@@ -77,30 +78,41 @@
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
+  include_fixer_context = yaml.load(stdout)
+
   lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+
+  symbol = include_fixer_context["SymbolIdentifier"]
+  headers = include_fixer_context["Headers"]
+
+  if not symbol:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if not headers:
+print "Couldn't find a header for {0}.\n".format(symbol)
 return
 
   # The first line is the symbol name.
-  symbol = lines[0]
   # If there is only one suggested header, insert it directly.
-  if len(lines) == 2 or maximum_suggested_headers == 1:
-InsertHeaderToVimBuffer(lines[1], text)
-print "Added #include {0} for {1}.\n".format(lines[1], symbol)
+  if len(headers) == 1 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+ "Headers":[headers[0]]}, text)
+print "Added #include {0} for {1}.\n".format(header[0], symbol)
 return
 
   choices_message = ""
   index = 1;
-  for header in lines[1:1+maximum_suggested_headers]:
+  for header in headers[0:maximum_suggested_headers]:
 choices_message += "&{0} {1}\n".format(index, header)
 index += 1
 
   select = ShowDialog("choose a header file for {0}.".format(symbol),
   choices_message)
   # Insert a selected header.
-  InsertHeaderToVimBuffer(lines[select], text)
-  print "Added #include {0} for {1}.\n".format(lines[select], symbol)
+  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+   "Headers":[headers[select-1]]}, text)
+  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
   return;
 
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using clang::include_fixer::IncludeFixerContext;
+
+LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(IncludeFixerContext)
+LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
+
+namespace llvm {
+namespace yaml {
+template <> 

Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59100.
hokein added a comment.

Remove unused code.


http://reviews.llvm.org/D20827

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/ranking.cpp

Index: test/include-fixer/ranking.cpp
===
--- test/include-fixer/ranking.cpp
+++ test/include-fixer/ranking.cpp
@@ -1,6 +1,5 @@
-// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s -implicit-check-not=.h
+// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s
 
-// CHECK: "../include/bar.h"
-// CHECK-NEXT: "../include/zbar.h"
+// CHECK: Headers: [ '"../include/bar.h"', '"../include/zbar.h"' ]
 
 bar b;
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
 // CHECK-HEADERS: "foo.h"
 // CHECK-HEADERS: "bar.h"
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -19,6 +19,7 @@
 import difflib
 import subprocess
 import vim
+import yaml
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
 # on the path.
@@ -49,7 +50,7 @@
 
 
 def InsertHeaderToVimBuffer(header, text):
-  command = [binary, "-stdin", "-insert-header="+header,
+  command = [binary, "-stdin", "-insert-header="+yaml.dump(header),
  vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   if stdout:
@@ -77,30 +78,38 @@
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
-  lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+  include_fixer_context = yaml.load(stdout)
+  symbol = include_fixer_context["SymbolIdentifier"]
+  headers = include_fixer_context["Headers"]
+
+  if not symbol:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if not headers:
+print "Couldn't find a header for {0}.\n".format(symbol)
 return
 
   # The first line is the symbol name.
-  symbol = lines[0]
   # If there is only one suggested header, insert it directly.
-  if len(lines) == 2 or maximum_suggested_headers == 1:
-InsertHeaderToVimBuffer(lines[1], text)
-print "Added #include {0} for {1}.\n".format(lines[1], symbol)
+  if len(headers) == 1 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+ "Headers":[headers[0]]}, text)
+print "Added #include {0} for {1}.\n".format(headers[0], symbol)
 return
 
   choices_message = ""
   index = 1;
-  for header in lines[1:1+maximum_suggested_headers]:
+  for header in headers[0:maximum_suggested_headers]:
 choices_message += "&{0} {1}\n".format(index, header)
 index += 1
 
   select = ShowDialog("choose a header file for {0}.".format(symbol),
   choices_message)
   # Insert a selected header.
-  InsertHeaderToVimBuffer(lines[select], text)
-  print "Added #include {0} for {1}.\n".format(lines[select], symbol)
+  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+   "Headers":[headers[select-1]]}, text)
+  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
   return;
 
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using clang::include_fixer::IncludeFixerContext;
+
+LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(IncludeFixerContext)
+LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
+
+namespace llvm {
+namespace yaml {
+template <> struct MappingTraits {
+  static void mapping(IO , IncludeFixerContext ) {
+

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein added a comment.

In http://reviews.llvm.org/D20621#444050, @bkramer wrote:

> LG. Can't wait to use it myself :)


Currently, the header is only inserted at the first line of the file because we 
don't output the FirstIncludeOffset to py script. A follow-up patch will come 
soon.


Repository:
  rL LLVM

http://reviews.llvm.org/D20621



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL271258: [include-fixer] Create a mode in vim integration to 
show multiple potential… (authored by hokein).

Changed prior to commit:
  http://reviews.llvm.org/D20621?vs=59028=59030#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20621

Files:
  clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
  clang-tools-extra/trunk/include-fixer/IncludeFixer.h
  clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h
  clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
  clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py
  clang-tools-extra/trunk/test/include-fixer/commandline_options.cpp
  clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp

Index: clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
===
--- clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
+++ clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createInsertHeaderReplacements(
+Code, "input.cc", FixerContext.Headers.front(),
+FixerContext.FirstIncludeOffset);
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.h
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.h
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.h
@@ -10,7 +10,9 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
 #define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXER_H
 
+#include "IncludeFixerContext.h"
 #include "SymbolIndexManager.h"
+#include "clang/Format/Format.h"
 #include "clang/Tooling/Core/Replacement.h"
 #include "clang/Tooling/Tooling.h"
 #include 
@@ -28,13 +30,12 @@
 class IncludeFixerActionFactory : public clang::tooling::ToolAction {
 public:
   /// \param SymbolIndexMgr A source for matching symbols to header files.
-  /// \param Replacements Storage for the output of the fixer.
+  /// \param Context A context for the symbol being queried.
   /// \param StyleName Fallback style for reformatting.
   /// \param MinimizeIncludePaths whether inserted include paths are optimized.
-  IncludeFixerActionFactory(
-  SymbolIndexManager , std::set ,
-  std::vector ,
-  StringRef StyleName, bool MinimizeIncludePaths = true);
+  IncludeFixerActionFactory(SymbolIndexManager ,
+IncludeFixerContext , StringRef StyleName,
+bool MinimizeIncludePaths = true);
 
   ~IncludeFixerActionFactory() override;
 
@@ -48,11 +49,8 @@
   /// The client to use to find cross-references.
   SymbolIndexManager 
 
-  /// Headers to be added.
-  std::set 
-
-  /// Replacements are written here.
-  std::vector 
+  /// The context that contains all information about the symbol being queried.
+  IncludeFixerContext 
 
   /// Whether inserted include paths should be optimized.
   bool MinimizeIncludePaths;
@@ -62,6 +60,25 @@
   std::string FallbackStyle;
 };
 
+/// Create replacements for the header being inserted. The replacements will
+/// insert a header before the first #include in \p Code, and sort all headers
+/// with the given clang-format style.
+///
+/// \param Code The source code.
+/// \param FilePath The source file path.
+/// \param Header The header being inserted.
+/// \param FirstIncludeOffset The insertion point for new include directives.
+/// The default value -1U means inserting the header at the first line, and if
+/// there is no #include block, it will create a header block by inserting a
+/// newline.
+/// \param Style clang-format style being used.
+///
+/// \return Replacements for inserting and sorting headers.
+std::vector createInsertHeaderReplacements(
+StringRef Code, StringRef FilePath, StringRef Header,
+unsigned FirstIncludeOffset = -1U,
+const clang::format::FormatStyle  = clang::format::getLLVMStyle());
+
 } // namespace include_fixer
 } // namespace clang
 
Index: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
===
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
@@ 

[clang-tools-extra] r271258 - [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Tue May 31 04:31:51 2016
New Revision: 271258

URL: http://llvm.org/viewvc/llvm-project?rev=271258=rev
Log:
[include-fixer] Create a mode in vim integration to show multiple potential 
headers.

Summary:
Some changes in the patch:

* Add two commandline flags in clang-include-fixer.
* Introduce a IncludeFixerContext for the queried symbol.
* Pull out CreateReplacementsForHeader.

Reviewers: bkramer

Subscribers: klimek, cfe-commits, ioeric

Differential Revision: http://reviews.llvm.org/D20621

Added:
clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h
clang-tools-extra/trunk/test/include-fixer/commandline_options.cpp
Modified:
clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
clang-tools-extra/trunk/include-fixer/IncludeFixer.h
clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py
clang-tools-extra/trunk/unittests/include-fixer/IncludeFixerTest.cpp

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp?rev=271258=271257=271258=diff
==
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Tue May 31 04:31:51 
2016
@@ -214,7 +214,7 @@ public:
 
   /// Get the minimal include for a given path.
   std::string minimizeInclude(StringRef Include,
-  clang::SourceManager ,
+  const clang::SourceManager ,
   clang::HeaderSearch ) {
 if (!MinimizeIncludePaths)
   return Include;
@@ -236,66 +236,21 @@ public:
 return IsSystem ? '<' + Suggestion + '>' : '"' + Suggestion + '"';
   }
 
-  /// Insert all headers before the first #include in \p Code and run
-  /// clang-format to sort all headers.
-  /// \return Replacements for inserting and sorting headers.
-  std::vector
-  CreateReplacementsForHeaders(StringRef Code,
-   const std::set ) {
-// Create replacements for new headers.
-clang::tooling::Replacements Insertions;
-if (FirstIncludeOffset == -1U) {
-  // FIXME: skip header guards.
-  FirstIncludeOffset = 0;
-  // If there is no existing #include, then insert an empty line after new
-  // header block.
-  if (Code.front() != '\n')
-Insertions.insert(
-clang::tooling::Replacement(Filename, FirstIncludeOffset, 0, 
"\n"));
-}
-// Keep inserting new headers before the first header.
-for (StringRef Header : Headers) {
-  std::string Text = "#include " + Header.str() + "\n";
-  Insertions.insert(
-  clang::tooling::Replacement(Filename, FirstIncludeOffset, 0, Text));
-}
-DEBUG({
-  llvm::dbgs() << "Header insertions:\n";
-  for (const auto  : Insertions)
-llvm::dbgs() << R.toString() << '\n';
-});
-
-clang::format::FormatStyle Style =
-clang::format::getStyle("file", Filename, FallbackStyle);
-clang::tooling::Replacements Replaces =
-formatReplacements(Code, Insertions, Style);
-// FIXME: remove this when `clang::tooling::Replacements` is implemented as
-// `std::vector`.
-std::vector Results;
-std::copy(Replaces.begin(), Replaces.end(), std::back_inserter(Results));
-return Results;
-  }
-
-  /// Generate replacements for the suggested includes.
-  /// \return true if changes will be made, false otherwise.
-  bool Rewrite(clang::SourceManager ,
-   clang::HeaderSearch ,
-   std::set ,
-   std::vector ) {
+  /// Get the include fixer context for the queried symbol.
+  IncludeFixerContext
+  getIncludeFixerContext(const clang::SourceManager ,
+ clang::HeaderSearch ) {
+IncludeFixerContext FixerContext;
 if (SymbolQueryResults.empty())
-  return false;
+  return FixerContext;
 
-// FIXME: Rank the results and pick the best one instead of the first one.
-const auto  = SymbolQueryResults.front();
-Headers.insert(minimizeInclude(ToTry, SourceManager, HeaderSearch));
-
-StringRef Code = 
SourceManager.getBufferData(SourceManager.getMainFileID());
-Replacements = CreateReplacementsForHeaders(Code, Headers);
-
-// We currently abort after the first inserted include. The more
-// includes we have the less safe this becomes due to error recovery
-// changing the results.
-return true;
+FixerContext.SymbolIdentifer = QuerySymbol;
+FixerContext.FirstIncludeOffset = FirstIncludeOffset;
+for (const auto  : SymbolQueryResults)
+  FixerContext.Headers.push_back(
+  minimizeInclude(Header, SourceManager, HeaderSearch));
+
+return FixerContext;
   }
 
   /// Sets the location at the very top of the file.
@@ -314,6 +269,7 @@ private:

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59026.
hokein added a comment.

Update a out-of-date comment.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createInsertHeaderReplacements(
+Code, "input.cc", FixerContext.Headers.front(),
+FixerContext.FirstIncludeOffset);
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], text)
+print "Added #include {0} for 

Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.
hokein added a comment.

http://reviews.llvm.org/D20621



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20621: [include-fixer] Create a mode in vim integration to show multiple potential headers.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59028.
hokein added a comment.

Add -1U comment back.


http://reviews.llvm.org/D20621

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixer.h
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  unittests/include-fixer/IncludeFixerTest.cpp

Index: unittests/include-fixer/IncludeFixerTest.cpp
===
--- unittests/include-fixer/IncludeFixerTest.cpp
+++ unittests/include-fixer/IncludeFixerTest.cpp
@@ -70,11 +70,16 @@
   SymbolIndexMgr->addSymbolIndex(
   llvm::make_unique(Symbols));
 
-  std::set Headers;
-  std::vector Replacements;
-  IncludeFixerActionFactory Factory(*SymbolIndexMgr, Headers, Replacements,
-"llvm");
+  IncludeFixerContext FixerContext;
+  IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm");
+
   runOnCode(, Code, "input.cc", ExtraArgs);
+  std::vector Replacements;
+  if (!FixerContext.Headers.empty()) {
+Replacements = clang::include_fixer::createInsertHeaderReplacements(
+Code, "input.cc", FixerContext.Headers.front(),
+FixerContext.FirstIncludeOffset);
+  }
   clang::RewriterTestContext Context;
   clang::FileID ID = Context.createInMemoryFile("input.cc", Code);
   clang::tooling::applyAllReplacements(Replacements, Context.Rewrite);
Index: test/include-fixer/commandline_options.cpp
===
--- /dev/null
+++ test/include-fixer/commandline_options.cpp
@@ -0,0 +1,12 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+//
+// CHECK-HEADERS: "foo.h"
+// CHECK-HEADERS: "bar.h"
+//
+// CHECK: #include "foo.h"
+// CHECK: foo f;
+
+foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -18,7 +18,6 @@
 import argparse
 import difflib
 import subprocess
-import sys
 import vim
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
@@ -28,6 +27,39 @@
 if vim.eval('exists("g:clang_include_fixer_path")') == "1":
   binary = vim.eval('g:clang_include_fixer_path')
 
+maximum_suggested_headers=3
+if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
+  maximum_suggested_headers = max(
+  1,
+  vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
+
+
+def ShowDialog(message, choices, default_choice_index=0):
+  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
+  choices,
+  default_choice_index)
+  return int(vim.eval(to_eval));
+
+
+def execute(command, text):
+  p = subprocess.Popen(command,
+   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+   stdin=subprocess.PIPE)
+  return p.communicate(input=text)
+
+
+def InsertHeaderToVimBuffer(header, text):
+  command = [binary, "-stdin", "-insert-header="+header,
+ vim.current.buffer.name]
+  stdout, stderr = execute(command, text)
+  if stdout:
+lines = stdout.splitlines()
+sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+for op in reversed(sequence.get_opcodes()):
+  if op[0] is not 'equal':
+vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+
+
 def main():
   parser = argparse.ArgumentParser(
   description='Vim integration for clang-include-fixer')
@@ -41,24 +73,36 @@
   buf = vim.current.buffer
   text = '\n'.join(buf)
 
-  # Call clang-include-fixer.
-  command = [binary, "-stdin", "-db="+args.db, "-input="+args.input,
+  # Run command to get all headers.
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
  vim.current.buffer.name]
-  p = subprocess.Popen(command,
-   stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-   stdin=subprocess.PIPE)
-  stdout, stderr = p.communicate(input=text)
+  stdout, stderr = execute(command, text)
+  lines = stdout.splitlines()
+  if len(lines) < 2:
+print "No header is included.\n"
+return
 
-  # If successful, replace buffer contents.
-  if stderr:
-print stderr
+  # The first line is the symbol name.
+  symbol = lines[0]
+  # If there is only one suggested header, insert it directly.
+  if len(lines) == 2 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer(lines[1], text)
+print "Added #include {0} for {1}.\n".format(lines[1], 

[clang-tools-extra] r271261 - [include-fixer] Add missing dependency.

2016-05-31 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Tue May 31 05:06:12 2016
New Revision: 271261

URL: http://llvm.org/viewvc/llvm-project?rev=271261=rev
Log:
[include-fixer] Add missing dependency.

Modified:
clang-tools-extra/trunk/include-fixer/tool/CMakeLists.txt

Modified: clang-tools-extra/trunk/include-fixer/tool/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/CMakeLists.txt?rev=271261=271260=271261=diff
==
--- clang-tools-extra/trunk/include-fixer/tool/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/include-fixer/tool/CMakeLists.txt Tue May 31 
05:06:12 2016
@@ -3,6 +3,7 @@ include_directories(${CMAKE_CURRENT_SOUR
 add_clang_executable(clang-include-fixer ClangIncludeFixer.cpp)
 target_link_libraries(clang-include-fixer
   clangBasic
+  clangFormat
   clangFrontend
   clangIncludeFixer
   clangRewrite


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D20909: [clang-tidy] Ignore function context in misc-unused-using-decls.

2016-06-02 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: alexfh.
hokein added a subscriber: cfe-commits.

Make the check's behavior more correct when handling using-decls in multiple 
scopes.

http://reviews.llvm.org/D20909

Files:
  clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  clang-tidy/misc/UnusedUsingDeclsCheck.h
  test/clang-tidy/misc-unused-using-decls.cpp

Index: test/clang-tidy/misc-unused-using-decls.cpp
===
--- test/clang-tidy/misc-unused-using-decls.cpp
+++ test/clang-tidy/misc-unused-using-decls.cpp
@@ -17,6 +17,8 @@
   static int ii;
 };
 template  class J {};
+class G;
+class H;
 
 class Base {
  public:
@@ -99,6 +101,24 @@
 USING_FUNC
 #undef USING_FUNC
 
+namespace N1 {
+// n::G is used in namespace N2.
+// Currently, the check doesn't support multiple scopes. All the relevant
+// using-decls will be marked as used once we see an usage even the usage is in
+// other scope.
+using n::G;
+}
+
+namespace N2 {
+using n::G;
+void f(G g);
+}
+
+void IgnoreFunctionScope() {
+// Using-decls defined in function scope will be ignored.
+using n::H;
+}
+
 // - Usages -
 void f(B b);
 void g() {
@@ -112,4 +132,3 @@
   UsedTemplateFunc();
   cout << endl;
 }
-
Index: clang-tidy/misc/UnusedUsingDeclsCheck.h
===
--- clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -36,9 +36,15 @@
   struct UsingDeclContext {
 explicit UsingDeclContext(const UsingDecl *FoundUsingDecl)
 : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {}
+// A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl
+// can introduce multiple UsingShadowDecls in some cases (such as
+// overloaded functions).
 llvm::SmallPtrSet UsingTargetDecls;
+// The original UsingDecl.
 const UsingDecl *FoundUsingDecl;
+// The source range of the UsingDecl.
 CharSourceRange UsingDeclRange;
+// Whether the UsingDecl is used.
 bool IsUsed;
   };
 
Index: clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -41,11 +41,17 @@
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
 // Ignores using-declarations defined in macros.
 if (Using->getLocation().isMacroID())
-  return ;
+  return;
 
 // Ignores using-declarations defined in class definition.
 if (isa(Using->getDeclContext()))
-  return ;
+  return;
+
+// FIXME: We ignore using-decls defined in function definitions at the
+// moment because of false positives caused by ADL and different function
+// scopes.
+if (isa(Using->getDeclContext()))
+  return;
 
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
@@ -97,11 +103,12 @@
 }
 
 void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
+  // FIXME: Currently, we don't handle the using-decls being used in different
+  // scopes (such as different namespaces, different functions). Instead of
+  // giving an incorrect message, we mark all of them as used.
   for (auto  : Contexts) {
-if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0) {
+if (Context.UsingTargetDecls.count(D->getCanonicalDecl()) > 0)
   Context.IsUsed = true;
-  break;
-}
   }
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20519: [clang-tidy] Ignore ADL-style using decls in unused-using-decls check.

2016-05-26 Thread Haojian Wu via cfe-commits
hokein added inline comments.


Comment at: clang-tidy/misc/UnusedUsingDeclsCheck.cpp:47
@@ +46,3 @@
+  return;
+// Ignores using-declarations defined in function definitions to avoid
+// arguement-dependent lookup.

alexfh wrote:
> hokein wrote:
> > alexfh wrote:
> > > I don't think using declarations in functions are inherently related to 
> > > ADL. This check doesn't seem like a useful heuristic.
> > Yeah, the current work around is only to ignore all using-decls in function 
> > context which is not perfect. I didn't find out a better way.
> If we're talking about this pattern:
> 
>   using std::swap;
>   ...
>   ::ns::T x, y;
>   swap(x, y); // ::ns::swap() is used
> 
> then for each function name resolved using ADL, we can find all using 
> declarations for the same name in different namespaces and mark them used. 
> WDYT?
Great, this idea looks good to me. Will make the change.


http://reviews.llvm.org/D20519



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D20666: Fix a wrong check in misc-unused-using-decls

2016-05-26 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: alexfh.
hokein added a subscriber: cfe-commits.

We should check whether a UsingDecl is defined in macros or in class
definition, not TargetDecls of the UsingDecl.

http://reviews.llvm.org/D20666

Files:
  clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  test/clang-tidy/misc-unused-using-decls.cpp

Index: test/clang-tidy/misc-unused-using-decls.cpp
===
--- test/clang-tidy/misc-unused-using-decls.cpp
+++ test/clang-tidy/misc-unused-using-decls.cpp
@@ -33,6 +33,7 @@
 template  int UsedInTemplateFunc() { return 1; }
 void OverloadFunc(int);
 void OverloadFunc(double);
+int FuncUsedByUsingDeclInMacro() { return 1; }
 
 class ostream {
 public:
@@ -93,6 +94,11 @@
 DEFINE_INT(test);
 #undef DEFIND_INT
 
+#define USING_FUNC \
+  using n::FuncUsedByUsingDeclInMacro;
+USING_FUNC
+#undef USING_FUNC
+
 // - Usages -
 void f(B b);
 void g() {
Index: clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -18,20 +18,10 @@
 namespace tidy {
 namespace misc {
 
-// A function that helps to tell whether a TargetDecl will be checked.
-// We only check a TargetDecl if :
-//   * The corresponding UsingDecl is not defined in macros or in class
-// definitions.
-//   * Only variable, function and class types are considered.
+// A function that helps to tell whether a TargetDecl in a UsingDecl will be
+// checked. Only variable, function, function template, class template and 
class
+// are considered.
 static bool ShouldCheckDecl(const Decl *TargetDecl) {
-  // Ignores using-declarations defined in macros.
-  if (TargetDecl->getLocation().isMacroID())
-return false;
-
-  // Ignores using-declarations defined in class definition.
-  if (isa(TargetDecl->getDeclContext()))
-return false;
-
   return isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl);
@@ -49,6 +39,14 @@
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult ) {
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
+// Ignores using-declarations defined in macros.
+if (Using->getLocation().isMacroID())
+  return ;
+
+// Ignores using-declarations defined in class definition.
+if (isa(Using->getDeclContext()))
+  return ;
+
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
 Using->getLocStart(),


Index: test/clang-tidy/misc-unused-using-decls.cpp
===
--- test/clang-tidy/misc-unused-using-decls.cpp
+++ test/clang-tidy/misc-unused-using-decls.cpp
@@ -33,6 +33,7 @@
 template  int UsedInTemplateFunc() { return 1; }
 void OverloadFunc(int);
 void OverloadFunc(double);
+int FuncUsedByUsingDeclInMacro() { return 1; }
 
 class ostream {
 public:
@@ -93,6 +94,11 @@
 DEFINE_INT(test);
 #undef DEFIND_INT
 
+#define USING_FUNC \
+  using n::FuncUsedByUsingDeclInMacro;
+USING_FUNC
+#undef USING_FUNC
+
 // - Usages -
 void f(B b);
 void g() {
Index: clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===
--- clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -18,20 +18,10 @@
 namespace tidy {
 namespace misc {
 
-// A function that helps to tell whether a TargetDecl will be checked.
-// We only check a TargetDecl if :
-//   * The corresponding UsingDecl is not defined in macros or in class
-// definitions.
-//   * Only variable, function and class types are considered.
+// A function that helps to tell whether a TargetDecl in a UsingDecl will be
+// checked. Only variable, function, function template, class template and class
+// are considered.
 static bool ShouldCheckDecl(const Decl *TargetDecl) {
-  // Ignores using-declarations defined in macros.
-  if (TargetDecl->getLocation().isMacroID())
-return false;
-
-  // Ignores using-declarations defined in class definition.
-  if (isa(TargetDecl->getDeclContext()))
-return false;
-
   return isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl) || isa(TargetDecl) ||
  isa(TargetDecl);
@@ -49,6 +39,14 @@
 
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult ) {
   if (const auto *Using = Result.Nodes.getNodeAs("using")) {
+// Ignores using-declarations defined in macros.
+if (Using->getLocation().isMacroID())
+  return ;
+
+// Ignores using-declarations defined in class definition.
+if (isa(Using->getDeclContext()))
+  return ;
+
 UsingDeclContext Context(Using);
 Context.UsingDeclRange = CharSourceRange::getCharRange(
 Using->getLocStart(),
___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[clang-tools-extra] r271382 - [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
Author: hokein
Date: Wed Jun  1 06:43:10 2016
New Revision: 271382

URL: http://llvm.org/viewvc/llvm-project?rev=271382=rev
Log:
[include-fixer] Use YAML format in -output-headers and -insert-header mode.

Summary:
And some improvements:
* Show better error messages on unfound symbols.
* Fix a typo.

Reviewers: bkramer

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20827

Modified:
clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h
clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py
clang-tools-extra/trunk/test/include-fixer/commandline_options.cpp
clang-tools-extra/trunk/test/include-fixer/ranking.cpp

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp?rev=271382=271381=271382=diff
==
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Wed Jun  1 06:43:10 
2016
@@ -193,10 +193,7 @@ public:
   getIncludeFixerContext(const clang::SourceManager ,
  clang::HeaderSearch ) {
 IncludeFixerContext FixerContext;
-if (SymbolQueryResults.empty())
-  return FixerContext;
-
-FixerContext.SymbolIdentifer = QuerySymbol;
+FixerContext.SymbolIdentifier = QuerySymbol;
 for (const auto  : SymbolQueryResults)
   FixerContext.Headers.push_back(
   minimizeInclude(Header, SourceManager, HeaderSearch));

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h?rev=271382=271381=271382=diff
==
--- clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixerContext.h Wed Jun  1 
06:43:10 2016
@@ -19,7 +19,7 @@ namespace include_fixer {
 /// \brief A context for the symbol being queried.
 struct IncludeFixerContext {
   /// \brief The symbol name.
-  std::string SymbolIdentifer;
+  std::string SymbolIdentifier;
   /// \brief The headers which have SymbolIdentifier definitions.
   std::vector Headers;
 };

Modified: clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp?rev=271382=271381=271382=diff
==
--- clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp Wed Jun  1 
06:43:10 2016
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using clang::include_fixer::IncludeFixerContext;
+
+LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(IncludeFixerContext)
+LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
+
+namespace llvm {
+namespace yaml {
+template <> struct MappingTraits {
+  static void mapping(IO , IncludeFixerContext ) {
+io.mapRequired("SymbolIdentifier", Context.SymbolIdentifier);
+io.mapRequired("Headers", Context.Headers);
+  }
+};
+} // namespace yaml
+} // namespace llvm
 
 namespace {
 cl::OptionCategory IncludeFixerCategory("Tool options");
@@ -60,19 +76,21 @@ cl::opt
 
 cl::opt OutputHeaders(
 "output-headers",
-cl::desc("Output the symbol being quired and all its relevant headers.\n"
- "The first line is the symbol name; The other lines\n"
- "are the headers: \n"
- "   b::foo\n"
- "   path/to/foo_a.h\n"
- "   path/to/foo_b.h\n"),
+cl::desc("Print the symbol being queried and all its relevant headers in\n"
+ "JSON format to stdout:\n"
+ "  {\n"
+ "\"SymbolIdentifier\": \"foo\",\n"
+ "\"Headers\": [\"\\\"foo_a.h\\\"\"]\n"
+ "  }"),
 cl::init(false), cl::cat(IncludeFixerCategory));
 
 cl::opt InsertHeader(
 "insert-header",
 cl::desc("Insert a specific header. This should run with STDIN mode.\n"
  "The result is written to stdout. It is currently used for\n"
- "editor integration."),
+ "editor integration. Support YAML/JSON format:\n"
+ "  -insert-header=\"{SymbolIdentifier: foo,\n"
+ "   Headers: ['\\\"foo_a.h\\\"']}\""),
 cl::init(""), cl::cat(IncludeFixerCategory));
 
 cl::opt
@@ -134,6 +152,19 @@ createSymbolIndexManager(StringRef FileP
   return SymbolIndexMgr;
 }
 
+void 

Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59183.
hokein added a comment.

Get rid of yaml dependency, using json module.


http://reviews.llvm.org/D20827

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/ranking.cpp

Index: test/include-fixer/ranking.cpp
===
--- test/include-fixer/ranking.cpp
+++ test/include-fixer/ranking.cpp
@@ -1,6 +1,5 @@
-// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s -implicit-check-not=.h
+// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s
 
-// CHECK: "../include/bar.h"
-// CHECK-NEXT: "../include/zbar.h"
+// CHECK: "Headers": [ "\"../include/bar.h\"", "\"../include/zbar.h\"" ]
 
 bar b;
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,10 +1,9 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
-// CHECK-HEADERS: "foo.h"
-// CHECK-HEADERS: "bar.h"
+// CHECK-HEADERS: "Headers": [ "\"foo.h\"", "\"bar.h\"" ]
 //
 // CHECK: #include "foo.h"
 // CHECK: foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -19,6 +19,7 @@
 import difflib
 import subprocess
 import vim
+import json
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
 # on the path.
@@ -49,7 +50,7 @@
 
 
 def InsertHeaderToVimBuffer(header, text):
-  command = [binary, "-stdin", "-insert-header="+header,
+  command = [binary, "-stdin", "-insert-header="+json.dumps(header),
  vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   if stdout:
@@ -77,30 +78,38 @@
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
-  lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+  include_fixer_context = json.loads(stdout)
+  symbol = include_fixer_context["SymbolIdentifier"]
+  headers = include_fixer_context["Headers"]
+
+  if not symbol:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if not headers:
+print "Couldn't find a header for {0}.\n".format(symbol)
 return
 
   # The first line is the symbol name.
-  symbol = lines[0]
   # If there is only one suggested header, insert it directly.
-  if len(lines) == 2 or maximum_suggested_headers == 1:
-InsertHeaderToVimBuffer(lines[1], text)
-print "Added #include {0} for {1}.\n".format(lines[1], symbol)
+  if len(headers) == 1 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+ "Headers":[headers[0]]}, text)
+print "Added #include {0} for {1}.\n".format(headers[0], symbol)
 return
 
   choices_message = ""
   index = 1;
-  for header in lines[1:1+maximum_suggested_headers]:
+  for header in headers[0:maximum_suggested_headers]:
 choices_message += "&{0} {1}\n".format(index, header)
 index += 1
 
   select = ShowDialog("choose a header file for {0}.".format(symbol),
   choices_message)
   # Insert a selected header.
-  InsertHeaderToVimBuffer(lines[select], text)
-  print "Added #include {0} for {1}.\n".format(lines[select], symbol)
+  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+   "Headers":[headers[select-1]]}, text)
+  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
   return;
 
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using clang::include_fixer::IncludeFixerContext;
+
+LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(IncludeFixerContext)
+LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
+
+namespace 

Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59184.
hokein added a comment.

Show error message when clang-include-fixer died with a fatal error.


http://reviews.llvm.org/D20827

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/ranking.cpp

Index: test/include-fixer/ranking.cpp
===
--- test/include-fixer/ranking.cpp
+++ test/include-fixer/ranking.cpp
@@ -1,6 +1,5 @@
-// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s -implicit-check-not=.h
+// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s
 
-// CHECK: "../include/bar.h"
-// CHECK-NEXT: "../include/zbar.h"
+// CHECK: "Headers": [ "\"../include/bar.h\"", "\"../include/zbar.h\"" ]
 
 bar b;
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,10 +1,9 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
-// CHECK-HEADERS: "foo.h"
-// CHECK-HEADERS: "bar.h"
+// CHECK-HEADERS: "Headers": [ "\"foo.h\"", "\"bar.h\"" ]
 //
 // CHECK: #include "foo.h"
 // CHECK: foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -19,6 +19,7 @@
 import difflib
 import subprocess
 import vim
+import json
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
 # on the path.
@@ -49,7 +50,7 @@
 
 
 def InsertHeaderToVimBuffer(header, text):
-  command = [binary, "-stdin", "-insert-header="+header,
+  command = [binary, "-stdin", "-insert-header="+json.dumps(header),
  vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   if stdout:
@@ -77,30 +78,42 @@
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
-  lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+  if stderr:
+print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
+return
+
+  include_fixer_context = json.loads(stdout)
+  symbol = include_fixer_context["SymbolIdentifier"]
+  headers = include_fixer_context["Headers"]
+
+  if not symbol:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if not headers:
+print "Couldn't find a header for {0}.\n".format(symbol)
 return
 
   # The first line is the symbol name.
-  symbol = lines[0]
   # If there is only one suggested header, insert it directly.
-  if len(lines) == 2 or maximum_suggested_headers == 1:
-InsertHeaderToVimBuffer(lines[1], text)
-print "Added #include {0} for {1}.\n".format(lines[1], symbol)
+  if len(headers) == 1 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+ "Headers":[headers[0]]}, text)
+print "Added #include {0} for {1}.\n".format(headers[0], symbol)
 return
 
   choices_message = ""
   index = 1;
-  for header in lines[1:1+maximum_suggested_headers]:
+  for header in headers[0:maximum_suggested_headers]:
 choices_message += "&{0} {1}\n".format(index, header)
 index += 1
 
   select = ShowDialog("choose a header file for {0}.".format(symbol),
   choices_message)
   # Insert a selected header.
-  InsertHeaderToVimBuffer(lines[select], text)
-  print "Added #include {0} for {1}.\n".format(lines[select], symbol)
+  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+   "Headers":[headers[select-1]]}, text)
+  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
   return;
 
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using 

Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.
hokein added a comment.

http://reviews.llvm.org/D20827



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59187.
hokein added a comment.

Always escape headers.


http://reviews.llvm.org/D20827

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/ranking.cpp

Index: test/include-fixer/ranking.cpp
===
--- test/include-fixer/ranking.cpp
+++ test/include-fixer/ranking.cpp
@@ -1,6 +1,5 @@
-// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s -implicit-check-not=.h
+// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s
 
-// CHECK: "../include/bar.h"
-// CHECK-NEXT: "../include/zbar.h"
+// CHECK: "Headers": [ "\"../include/bar.h\"", "\"../include/zbar.h\"" ]
 
 bar b;
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,10 +1,9 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
-// CHECK-HEADERS: "foo.h"
-// CHECK-HEADERS: "bar.h"
+// CHECK-HEADERS: "Headers": [ "\"foo.h\"", "\"bar.h\"" ]
 //
 // CHECK: #include "foo.h"
 // CHECK: foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -19,6 +19,7 @@
 import difflib
 import subprocess
 import vim
+import json
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
 # on the path.
@@ -49,7 +50,7 @@
 
 
 def InsertHeaderToVimBuffer(header, text):
-  command = [binary, "-stdin", "-insert-header="+header,
+  command = [binary, "-stdin", "-insert-header="+json.dumps(header),
  vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   if stdout:
@@ -77,30 +78,42 @@
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
-  lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+  if stderr:
+print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
+return
+
+  include_fixer_context = json.loads(stdout)
+  symbol = include_fixer_context["SymbolIdentifier"]
+  headers = include_fixer_context["Headers"]
+
+  if not symbol:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if not headers:
+print "Couldn't find a header for {0}.\n".format(symbol)
 return
 
   # The first line is the symbol name.
-  symbol = lines[0]
   # If there is only one suggested header, insert it directly.
-  if len(lines) == 2 or maximum_suggested_headers == 1:
-InsertHeaderToVimBuffer(lines[1], text)
-print "Added #include {0} for {1}.\n".format(lines[1], symbol)
+  if len(headers) == 1 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+ "Headers":[headers[0]]}, text)
+print "Added #include {0} for {1}.\n".format(headers[0], symbol)
 return
 
   choices_message = ""
   index = 1;
-  for header in lines[1:1+maximum_suggested_headers]:
+  for header in headers[0:maximum_suggested_headers]:
 choices_message += "&{0} {1}\n".format(index, header)
 index += 1
 
   select = ShowDialog("choose a header file for {0}.".format(symbol),
   choices_message)
   # Insert a selected header.
-  InsertHeaderToVimBuffer(lines[select], text)
-  print "Added #include {0} for {1}.\n".format(lines[select], symbol)
+  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+   "Headers":[headers[select-1]]}, text)
+  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
   return;
 
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using clang::include_fixer::IncludeFixerContext;
+

Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59185.
hokein added a comment.

Use llvm::yaml::escape to escape double quote.


http://reviews.llvm.org/D20827

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py
  test/include-fixer/commandline_options.cpp
  test/include-fixer/ranking.cpp

Index: test/include-fixer/ranking.cpp
===
--- test/include-fixer/ranking.cpp
+++ test/include-fixer/ranking.cpp
@@ -1,6 +1,5 @@
-// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s -implicit-check-not=.h
+// RUN: clang-include-fixer -db=yaml -input=%S/Inputs/fake_yaml_db.yaml -output-headers %s -- | FileCheck %s
 
-// CHECK: "../include/bar.h"
-// CHECK-NEXT: "../include/zbar.h"
+// CHECK: "Headers": [ "\"../include/bar.h\"", "\"../include/zbar.h\"" ]
 
 bar b;
Index: test/include-fixer/commandline_options.cpp
===
--- test/include-fixer/commandline_options.cpp
+++ test/include-fixer/commandline_options.cpp
@@ -1,10 +1,9 @@
 // REQUIRES: shell
 // RUN: sed -e 's#//.*$##' %s > %t.cpp
 // RUN: clang-include-fixer -db=fixed -input='foo= "foo.h","bar.h"' -output-headers %t.cpp -- | FileCheck %s -check-prefix=CHECK-HEADERS
-// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='"foo.h"' %t.cpp | FileCheck %s -check-prefix=CHECK
+// RUN: cat %t.cpp | clang-include-fixer -stdin -insert-header='{SymbolIdentifier: foo, Headers: ["\"foo.h\""]}' %t.cpp | FileCheck %s -check-prefix=CHECK
 //
-// CHECK-HEADERS: "foo.h"
-// CHECK-HEADERS: "bar.h"
+// CHECK-HEADERS: "Headers": [ "\"foo.h\"", "\"bar.h\"" ]
 //
 // CHECK: #include "foo.h"
 // CHECK: foo f;
Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -19,6 +19,7 @@
 import difflib
 import subprocess
 import vim
+import json
 
 # set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
 # on the path.
@@ -49,7 +50,7 @@
 
 
 def InsertHeaderToVimBuffer(header, text):
-  command = [binary, "-stdin", "-insert-header="+header,
+  command = [binary, "-stdin", "-insert-header="+json.dumps(header),
  vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   if stdout:
@@ -77,30 +78,42 @@
   command = [binary, "-stdin", "-output-headers", "-db="+args.db,
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
-  lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+  if stderr:
+print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
+return
+
+  include_fixer_context = json.loads(stdout)
+  symbol = include_fixer_context["SymbolIdentifier"]
+  headers = include_fixer_context["Headers"]
+
+  if not symbol:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if not headers:
+print "Couldn't find a header for {0}.\n".format(symbol)
 return
 
   # The first line is the symbol name.
-  symbol = lines[0]
   # If there is only one suggested header, insert it directly.
-  if len(lines) == 2 or maximum_suggested_headers == 1:
-InsertHeaderToVimBuffer(lines[1], text)
-print "Added #include {0} for {1}.\n".format(lines[1], symbol)
+  if len(headers) == 1 or maximum_suggested_headers == 1:
+InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+ "Headers":[headers[0]]}, text)
+print "Added #include {0} for {1}.\n".format(headers[0], symbol)
 return
 
   choices_message = ""
   index = 1;
-  for header in lines[1:1+maximum_suggested_headers]:
+  for header in headers[0:maximum_suggested_headers]:
 choices_message += "&{0} {1}\n".format(index, header)
 index += 1
 
   select = ShowDialog("choose a header file for {0}.".format(symbol),
   choices_message)
   # Insert a selected header.
-  InsertHeaderToVimBuffer(lines[select], text)
-  print "Added #include {0} for {1}.\n".format(lines[select], symbol)
+  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+   "Headers":[headers[select-1]]}, text)
+  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
   return;
 
 
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -18,9 +18,25 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/YAMLTraits.h"
 
 using namespace clang;
 using namespace llvm;
+using clang::include_fixer::IncludeFixerContext;
+

Re: [PATCH] D20827: [include-fixer] Use YAML format in -output-headers and -insert-header mode.

2016-06-01 Thread Haojian Wu via cfe-commits
hokein marked 2 inline comments as done.
hokein added a comment.

http://reviews.llvm.org/D20827



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D20809: [include-fixer] Show better messages on unfound symbols.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 59042.
hokein added a comment.

Rebase


http://reviews.llvm.org/D20809

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py

Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -78,8 +78,13 @@
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+
+  if len(lines) == 0:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if len(lines) == 1:
+print "Couldn't find a header for {0}.\n".format(lines[0])
 return
 
   # The first line is the symbol name.
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -190,8 +190,10 @@
   }
 
   if (OutputHeaders) {
+if (Context.SymbolIdentifier.empty())
+  return 0;
 // FIXME: Output IncludeFixerContext as YAML.
-llvm::outs() << Context.SymbolIdentifer << "\n";
+llvm::outs() << Context.SymbolIdentifier << "\n";
 for (const auto  : Context.Headers)
   llvm::outs() << Header << "\n";
 return 0;
Index: include-fixer/IncludeFixerContext.h
===
--- include-fixer/IncludeFixerContext.h
+++ include-fixer/IncludeFixerContext.h
@@ -19,7 +19,7 @@
 /// \brief A context for the symbol being queried.
 struct IncludeFixerContext {
   /// \brief The symbol name.
-  std::string SymbolIdentifer;
+  std::string SymbolIdentifier;
   /// \brief The headers which have SymbolIdentifier definitions.
   std::vector Headers;
   /// \brief The insertion point for new include header.
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -241,10 +241,7 @@
   getIncludeFixerContext(const clang::SourceManager ,
  clang::HeaderSearch ) {
 IncludeFixerContext FixerContext;
-if (SymbolQueryResults.empty())
-  return FixerContext;
-
-FixerContext.SymbolIdentifer = QuerySymbol;
+FixerContext.SymbolIdentifier = QuerySymbol;
 FixerContext.FirstIncludeOffset = FirstIncludeOffset;
 for (const auto  : SymbolQueryResults)
   FixerContext.Headers.push_back(


Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -78,8 +78,13 @@
  "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+
+  if len(lines) == 0:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if len(lines) == 1:
+print "Couldn't find a header for {0}.\n".format(lines[0])
 return
 
   # The first line is the symbol name.
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -190,8 +190,10 @@
   }
 
   if (OutputHeaders) {
+if (Context.SymbolIdentifier.empty())
+  return 0;
 // FIXME: Output IncludeFixerContext as YAML.
-llvm::outs() << Context.SymbolIdentifer << "\n";
+llvm::outs() << Context.SymbolIdentifier << "\n";
 for (const auto  : Context.Headers)
   llvm::outs() << Header << "\n";
 return 0;
Index: include-fixer/IncludeFixerContext.h
===
--- include-fixer/IncludeFixerContext.h
+++ include-fixer/IncludeFixerContext.h
@@ -19,7 +19,7 @@
 /// \brief A context for the symbol being queried.
 struct IncludeFixerContext {
   /// \brief The symbol name.
-  std::string SymbolIdentifer;
+  std::string SymbolIdentifier;
   /// \brief The headers which have SymbolIdentifier definitions.
   std::vector Headers;
   /// \brief The insertion point for new include header.
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -241,10 +241,7 @@
   getIncludeFixerContext(const clang::SourceManager ,
  clang::HeaderSearch ) {
 IncludeFixerContext FixerContext;
-if (SymbolQueryResults.empty())
-  return FixerContext;
-
-FixerContext.SymbolIdentifer = QuerySymbol;
+FixerContext.SymbolIdentifier = QuerySymbol;
 

[PATCH] D20809: [include-fixer] Show better messages on unfound symbols.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: bkramer.
hokein added a subscriber: cfe-commits.

Also some misc tweaks.

http://reviews.llvm.org/D20809

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/IncludeFixerContext.h
  include-fixer/tool/ClangIncludeFixer.cpp
  include-fixer/tool/clang-include-fixer.py

Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -74,12 +74,17 @@
   text = '\n'.join(buf)
 
   # Run command to get all headers.
-  command = [binary, "-stdin", "-output-headers", "-db="+args.db, 
"-input="+args.input, "-debug",
- vim.current.buffer.name]
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db,
+ "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+
+  if len(lines) == 0:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if len(lines) == 1:
+print "Couldn't find a header for {0}.\n".format(lines[0])
 return
 
   # The first line is the symbol name.
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -190,8 +190,10 @@
   }
 
   if (OutputHeaders) {
+if (Context.SymbolIdentifier.empty())
+  return 0;
 // FIXME: Output IncludeFixerContext as YAML.
-llvm::outs() << Context.SymbolIdentifer << "\n";
+llvm::outs() << Context.SymbolIdentifier << "\n";
 for (const auto  : Context.Headers)
   llvm::outs() << Header << "\n";
 return 0;
Index: include-fixer/IncludeFixerContext.h
===
--- include-fixer/IncludeFixerContext.h
+++ include-fixer/IncludeFixerContext.h
@@ -19,7 +19,7 @@
 /// \brief A context for the symbol being queried.
 struct IncludeFixerContext {
   /// \brief The symbol name.
-  std::string SymbolIdentifer;
+  std::string SymbolIdentifier;
   /// \brief The headers which have SymbolIdentifier definitions.
   std::vector Headers;
   /// \brief The insertion point for new include header.
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -241,10 +241,7 @@
   getIncludeFixerContext(const clang::SourceManager ,
  clang::HeaderSearch ) {
 IncludeFixerContext FixerContext;
-if (SymbolQueryResults.empty())
-  return FixerContext;
-
-FixerContext.SymbolIdentifer = QuerySymbol;
+FixerContext.SymbolIdentifier = QuerySymbol;
 FixerContext.FirstIncludeOffset = FirstIncludeOffset;
 for (const auto  : SymbolQueryResults)
   FixerContext.Headers.push_back(


Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -74,12 +74,17 @@
   text = '\n'.join(buf)
 
   # Run command to get all headers.
-  command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
- vim.current.buffer.name]
+  command = [binary, "-stdin", "-output-headers", "-db="+args.db,
+ "-input="+args.input, vim.current.buffer.name]
   stdout, stderr = execute(command, text)
   lines = stdout.splitlines()
-  if len(lines) < 2:
-print "No header is included.\n"
+
+  if len(lines) == 0:
+print "The file is fine, no need to add a header.\n"
+return;
+
+  if len(lines) == 1:
+print "Couldn't find a header for {0}.\n".format(lines[0])
 return
 
   # The first line is the symbol name.
Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -190,8 +190,10 @@
   }
 
   if (OutputHeaders) {
+if (Context.SymbolIdentifier.empty())
+  return 0;
 // FIXME: Output IncludeFixerContext as YAML.
-llvm::outs() << Context.SymbolIdentifer << "\n";
+llvm::outs() << Context.SymbolIdentifier << "\n";
 for (const auto  : Context.Headers)
   llvm::outs() << Header << "\n";
 return 0;
Index: include-fixer/IncludeFixerContext.h
===
--- include-fixer/IncludeFixerContext.h
+++ include-fixer/IncludeFixerContext.h
@@ -19,7 +19,7 @@
 /// \brief A context for the symbol being queried.
 struct IncludeFixerContext {
   /// \brief The symbol name.
-  std::string SymbolIdentifer;
+  std::string SymbolIdentifier;
   /// \brief The headers which have SymbolIdentifier definitions.
   std::vector 

[PATCH] D20808: [include-fixer] Code cleanup.

2016-05-31 Thread Haojian Wu via cfe-commits
hokein created this revision.
hokein added a reviewer: bkramer.
hokein added a subscriber: cfe-commits.

* Abstract the DB setting code to a function.
* Remove the unused FallbackStyle.

http://reviews.llvm.org/D20808

Files:
  include-fixer/IncludeFixer.cpp
  include-fixer/tool/ClangIncludeFixer.cpp

Index: include-fixer/tool/ClangIncludeFixer.cpp
===
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -81,6 +81,59 @@
"headers if there is no clang-format config file found."),
   cl::init("llvm"), cl::cat(IncludeFixerCategory));
 
+std::unique_ptr
+createSymbolIndexManager(StringRef FilePath) {
+  auto SymbolIndexMgr = llvm::make_unique();
+  switch (DatabaseFormat) {
+  case fixed: {
+// Parse input and fill the database with it.
+// =<, header...>
+// Multiple symbols can be given, separated by semicolons.
+std::map SymbolsMap;
+SmallVector SemicolonSplits;
+StringRef(Input).split(SemicolonSplits, ";");
+std::vector Symbols;
+for (StringRef Pair : SemicolonSplits) {
+  auto Split = Pair.split('=');
+  std::vector Headers;
+  SmallVector CommaSplits;
+  Split.second.split(CommaSplits, ",");
+  for (StringRef Header : CommaSplits)
+Symbols.push_back(find_all_symbols::SymbolInfo(
+Split.first.trim(),
+find_all_symbols::SymbolInfo::SymbolKind::Unknown, Header.trim(), 1,
+{}));
+}
+SymbolIndexMgr->addSymbolIndex(
+llvm::make_unique(Symbols));
+break;
+  }
+  case yaml: {
+llvm::ErrorOr DB(nullptr);
+if (!Input.empty()) {
+  DB = include_fixer::YamlSymbolIndex::createFromFile(Input);
+} else {
+  // If we don't have any input file, look in the directory of the first
+  // file and its parents.
+  SmallString<128> AbsolutePath(tooling::getAbsolutePath(FilePath));
+  StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
+  DB = include_fixer::YamlSymbolIndex::createFromDirectory(
+  Directory, "find_all_symbols_db.yaml");
+}
+
+if (!DB) {
+  llvm::errs() << "Couldn't find YAML db: " << DB.getError().message()
+   << '\n';
+  return nullptr;
+}
+
+SymbolIndexMgr->addSymbolIndex(std::move(*DB));
+break;
+  }
+  }
+  return SymbolIndexMgr;
+}
+
 int includeFixerMain(int argc, const char **argv) {
   tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory);
   tooling::ClangTool tool(options.getCompilations(),
@@ -128,55 +181,10 @@
   }
 
   // Set up data source.
-  auto SymbolIndexMgr = llvm::make_unique();
-  switch (DatabaseFormat) {
-  case fixed: {
-// Parse input and fill the database with it.
-// =<, header...>
-// Multiple symbols can be given, separated by semicolons.
-std::map SymbolsMap;
-SmallVector SemicolonSplits;
-StringRef(Input).split(SemicolonSplits, ";");
-std::vector Symbols;
-for (StringRef Pair : SemicolonSplits) {
-  auto Split = Pair.split('=');
-  std::vector Headers;
-  SmallVector CommaSplits;
-  Split.second.split(CommaSplits, ",");
-  for (StringRef Header : CommaSplits)
-Symbols.push_back(find_all_symbols::SymbolInfo(
-Split.first.trim(),
-find_all_symbols::SymbolInfo::SymbolKind::Unknown, Header.trim(), 1,
-{}));
-}
-SymbolIndexMgr->addSymbolIndex(
-llvm::make_unique(Symbols));
-break;
-  }
-  case yaml: {
-llvm::ErrorOr DB(nullptr);
-if (!Input.empty()) {
-  DB = include_fixer::YamlSymbolIndex::createFromFile(Input);
-} else {
-  // If we don't have any input file, look in the directory of the first
-  // file and its parents.
-  SmallString<128> AbsolutePath(
-  tooling::getAbsolutePath(options.getSourcePathList().front()));
-  StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
-  DB = include_fixer::YamlSymbolIndex::createFromDirectory(
-  Directory, "find_all_symbols_db.yaml");
-}
-
-if (!DB) {
-  llvm::errs() << "Couldn't find YAML db: " << DB.getError().message()
-   << '\n';
-  return 1;
-}
-
-SymbolIndexMgr->addSymbolIndex(std::move(*DB));
-break;
-  }
-  }
+  std::unique_ptr SymbolIndexMgr =
+  createSymbolIndexManager(options.getSourcePathList().front());
+  if (!SymbolIndexMgr)
+return 1;
 
   // Now run our tool.
   include_fixer::IncludeFixerContext Context;
Index: include-fixer/IncludeFixer.cpp
===
--- include-fixer/IncludeFixer.cpp
+++ include-fixer/IncludeFixer.cpp
@@ -59,9 +59,8 @@
 class Action : public clang::ASTFrontendAction,
public clang::ExternalSemaSource 

  1   2   3   4   5   6   7   8   9   10   >