[clang-tools-extra] r360450 - [clang-tidy] Change the namespace for llvm checkers from 'llvm' to 'llvm_check'

Don Hinton via cfe-commits cfe-commits at lists.llvm.org
Fri May 10 11:27:10 PDT 2019


Author: dhinton
Date: Fri May 10 11:27:09 2019
New Revision: 360450

URL: http://llvm.org/viewvc/llvm-project?rev=360450&view=rev
Log:
[clang-tidy] Change the namespace for llvm checkers from 'llvm' to 'llvm_check'

Summary:
Change the namespace for llvm checkers from 'llvm' to
'llvm_check', and modify add_new_check.py and rename_check.py to
support the new namespace. Checker, file, and directory names remain
unchanged.

Used new version of rename_check.py to make the change in existing
llvm checkers, but had to fix LLVMTidyModule.cpp and
LLVMModuleTest.cpp by hand.

The changes made by rename_check.py are idempotent, so if accidentally
run multiple times, it won't do anything.

Reviewed By: aaron.ballman

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D60629

Modified:
    clang-tools-extra/trunk/clang-tidy/add_new_check.py
    clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp
    clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h
    clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.cpp
    clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.h
    clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp
    clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp
    clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h
    clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.cpp
    clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.h
    clang-tools-extra/trunk/clang-tidy/rename_check.py
    clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp

Modified: clang-tools-extra/trunk/clang-tidy/add_new_check.py
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/add_new_check.py?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/add_new_check.py (original)
+++ clang-tools-extra/trunk/clang-tidy/add_new_check.py Fri May 10 11:27:09 2019
@@ -46,7 +46,7 @@ def adapt_cmake(module_path, check_name_
 
 
 # Adds a header for the new check.
-def write_header(module_path, module, check_name, check_name_camel):
+def write_header(module_path, module, namespace, check_name, check_name_camel):
   check_name_dashes = module + '-' + check_name
   filename = os.path.join(module_path, check_name_camel) + '.h'
   print('Creating %s...' % filename)
@@ -73,7 +73,7 @@ def write_header(module_path, module, ch
 
 namespace clang {
 namespace tidy {
-namespace %(module)s {
+namespace %(namespace)s {
 
 /// FIXME: Write a short description.
 ///
@@ -87,7 +87,7 @@ public:
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 };
 
-} // namespace %(module)s
+} // namespace %(namespace)s
 } // namespace tidy
 } // namespace clang
 
@@ -95,11 +95,12 @@ public:
 """ % {'header_guard': header_guard,
        'check_name': check_name_camel,
        'check_name_dashes': check_name_dashes,
-       'module': module})
+       'module': module,
+       'namespace': namespace})
 
 
 # Adds the implementation of the new check.
-def write_implementation(module_path, module, check_name_camel):
+def write_implementation(module_path, module, namespace, check_name_camel):
   filename = os.path.join(module_path, check_name_camel) + '.cpp'
   print('Creating %s...' % filename)
   with open(filename, 'w') as f:
@@ -124,7 +125,7 @@ using namespace clang::ast_matchers;
 
 namespace clang {
 namespace tidy {
-namespace %(module)s {
+namespace %(namespace)s {
 
 void %(check_name)s::registerMatchers(MatchFinder *Finder) {
   // FIXME: Add matchers.
@@ -142,11 +143,12 @@ void %(check_name)s::check(const MatchFi
       << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
 }
 
-} // namespace %(module)s
+} // namespace %(namespace)s
 } // namespace tidy
 } // namespace clang
 """ % {'check_name': check_name_camel,
-       'module': module})
+       'module': module,
+       'namespace': namespace})
 
 
 # Modifies the module to include the new check.
@@ -375,8 +377,15 @@ def main():
 
   if not adapt_cmake(module_path, check_name_camel):
     return
-  write_header(module_path, module, check_name, check_name_camel)
-  write_implementation(module_path, module, check_name_camel)
+
+  # Map module names to namespace names that don't conflict with widely used top-level namespaces.
+  if module == 'llvm':
+    namespace = module + '_check'
+  else:
+    namespace = module
+
+  write_header(module_path, module, namespace, check_name, check_name_camel)
+  write_implementation(module_path, module, namespace, check_name_camel)
   adapt_module(module_path, module, check_name, check_name_camel)
   add_release_notes(module_path, module, check_name)
   test_extension = language_to_extension.get(args.language)

Modified: clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp Fri May 10 11:27:09 2019
@@ -10,7 +10,7 @@
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 LLVMHeaderGuardCheck::LLVMHeaderGuardCheck(StringRef Name,
                                            ClangTidyContext *Context)
@@ -49,6 +49,6 @@ std::string LLVMHeaderGuardCheck::getHea
   return StringRef(Guard).upper();
 }
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h Fri May 10 11:27:09 2019
@@ -6,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_HEADER_GUARD_CHECK_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_HEADER_GUARD_CHECK_H
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_HEADERGUARDCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_HEADERGUARDCHECK_H
 
 #include "../utils/HeaderGuard.h"
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 /// Finds and fixes header guards that do not adhere to LLVM style.
 /// For the user-facing documentation see:
@@ -32,8 +32,8 @@ public:
   std::string getHeaderGuard(StringRef Filename, StringRef OldGuard) override;
 };
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang
 
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_HEADER_GUARD_CHECK_H
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_HEADERGUARDCHECK_H

Modified: clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.cpp?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.cpp Fri May 10 11:27:09 2019
@@ -15,7 +15,7 @@
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 namespace {
 class IncludeOrderPPCallbacks : public PPCallbacks {
@@ -176,6 +176,6 @@ void IncludeOrderPPCallbacks::EndOfMainF
   IncludeDirectives.clear();
 }
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.h?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/IncludeOrderCheck.h Fri May 10 11:27:09 2019
@@ -6,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_INCLUDE_ORDER_CHECK_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_INCLUDE_ORDER_CHECK_H
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_INCLUDEORDERCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_INCLUDEORDERCHECK_H
 
 #include "../ClangTidyCheck.h"
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 /// Checks the correct order of `#includes`.
 ///
@@ -26,8 +26,8 @@ public:
                            Preprocessor *ModuleExpanderPP) override;
 };
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang
 
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_INCLUDE_ORDER_CHECK_H
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_INCLUDEORDERCHECK_H

Modified: clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp Fri May 10 11:27:09 2019
@@ -17,7 +17,7 @@
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 class LLVMModule : public ClangTidyModule {
 public:
@@ -36,7 +36,7 @@ public:
 static ClangTidyModuleRegistry::Add<LLVMModule> X("llvm-module",
                                                   "Adds LLVM lint checks.");
 
-} // namespace llvm
+} // namespace llvm_check
 
 // This anchor is used to force the linker to link in the generated object file
 // and thus register the LLVMModule.

Modified: clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp Fri May 10 11:27:09 2019
@@ -19,7 +19,7 @@ AST_MATCHER(Expr, isMacroID) { return No
 } // namespace ast_matchers
 
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 void PreferIsaOrDynCastInConditionalsCheck::registerMatchers(
     MatchFinder *Finder) {
@@ -130,6 +130,6 @@ void PreferIsaOrDynCastInConditionalsChe
   }
 }
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.h Fri May 10 11:27:09 2019
@@ -6,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_AVOIDCASTINCONDITIONALCHECK_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_AVOIDCASTINCONDITIONALCHECK_H
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
 
 #include "../ClangTidyCheck.h"
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 /// \brief Looks at conditionals and finds and replaces cases of ``cast<>``, which will
 /// assert rather than return a null pointer, and ``dyn_cast<>`` where
@@ -57,8 +57,8 @@ public:
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 };
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang
 
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_AVOIDCASTINCONDITIONALCHECK_H
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H

Modified: clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.cpp?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.cpp Fri May 10 11:27:09 2019
@@ -15,7 +15,7 @@ using namespace clang::ast_matchers;
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 void TwineLocalCheck::registerMatchers(MatchFinder *Finder) {
   auto TwineType =
@@ -60,6 +60,6 @@ void TwineLocalCheck::check(const MatchF
   }
 }
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.h?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/TwineLocalCheck.h Fri May 10 11:27:09 2019
@@ -6,14 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINE_LOCAL_CHECK_H
-#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINE_LOCAL_CHECK_H
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINELOCALCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINELOCALCHECK_H
 
 #include "../ClangTidyCheck.h"
 
 namespace clang {
 namespace tidy {
-namespace llvm {
+namespace llvm_check {
 
 /// Looks for local `Twine` variables which are prone to use after frees and
 /// should be generally avoided.
@@ -25,8 +25,8 @@ public:
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 };
 
-} // namespace llvm
+} // namespace llvm_check
 } // namespace tidy
 } // namespace clang
 
-#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINE_LOCAL_CHECK_H
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_TWINELOCALCHECK_H

Modified: clang-tools-extra/trunk/clang-tidy/rename_check.py
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/rename_check.py?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/rename_check.py (original)
+++ clang-tools-extra/trunk/clang-tidy/rename_check.py Fri May 10 11:27:09 2019
@@ -14,6 +14,18 @@ import os
 import re
 
 
+def replaceInFileRegex(fileName, sFrom, sTo):
+  if sFrom == sTo:
+    return
+  txt = None
+  with open(fileName, "r") as f:
+    txt = f.read()
+
+  txt = re.sub(sFrom, sTo, txt)
+  print("Replacing '%s' -> '%s' in '%s'..." % (sFrom, sTo, fileName))
+  with open(fileName, "w") as f:
+    f.write(txt)
+
 def replaceInFile(fileName, sFrom, sTo):
   if sFrom == sTo:
     return
@@ -203,6 +215,8 @@ def main():
   clang_tidy_path = os.path.dirname(__file__)
 
   header_guard_variants = [
+      (args.old_check_name.replace('-', '_')).upper() + '_CHECK',
+      (old_module + '_' + check_name_camel).upper(),
       (old_module + '_' + new_check_name_camel).upper(),
       args.old_check_name.replace('-', '_').upper()]
   header_guard_new = (new_module + '_' + new_check_name_camel).upper()
@@ -210,18 +224,19 @@ def main():
   old_module_path = os.path.join(clang_tidy_path, old_module)
   new_module_path = os.path.join(clang_tidy_path, new_module)
 
-  # Remove the check from the old module.
-  cmake_lists = os.path.join(old_module_path, 'CMakeLists.txt')
-  check_found = deleteMatchingLines(cmake_lists, '\\b' + check_name_camel)
-  if not check_found:
-    print("Check name '%s' not found in %s. Exiting." %
+  if (args.old_check_name != args.new_check_name):
+    # Remove the check from the old module.
+    cmake_lists = os.path.join(old_module_path, 'CMakeLists.txt')
+    check_found = deleteMatchingLines(cmake_lists, '\\b' + check_name_camel)
+    if not check_found:
+      print("Check name '%s' not found in %s. Exiting." %
             (check_name_camel, cmake_lists))
-    return 1
+      return 1
 
-  modulecpp = filter(
-      lambda p: p.lower() == old_module.lower() + 'tidymodule.cpp',
-      os.listdir(old_module_path))[0]
-  deleteMatchingLines(os.path.join(old_module_path, modulecpp),
+    modulecpp = filter(
+        lambda p: p.lower() == old_module.lower() + 'tidymodule.cpp',
+        os.listdir(old_module_path))[0]
+    deleteMatchingLines(os.path.join(old_module_path, modulecpp),
                       '\\b' + check_name_camel + '|\\b' + args.old_check_name)
 
   for filename in getListOfFiles(clang_tidy_path):
@@ -249,14 +264,21 @@ def main():
                   new_module + '/' + new_check_name_camel)
     replaceInFile(filename, check_name_camel, new_check_name_camel)
 
-  if old_module != new_module:
+  if old_module != new_module or new_module == 'llvm':
+    if new_module == 'llvm':
+      new_namespace = new_module + '_check'
+    else:
+      new_namespace = new_module
     check_implementation_files = glob.glob(
         os.path.join(old_module_path, new_check_name_camel + '*'))
     for filename in check_implementation_files:
       # Move check implementation to the directory of the new module.
       filename = fileRename(filename, old_module_path, new_module_path)
-      replaceInFile(filename, 'namespace ' + old_module,
-                    'namespace ' + new_module)
+      replaceInFileRegex(filename, 'namespace ' + old_module + '[^ \n]*',
+                         'namespace ' + new_namespace)
+
+  if (args.old_check_name == args.new_check_name):
+    return
 
   # Add check to the new module.
   adapt_cmake(new_module_path, new_check_name_camel)

Modified: clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp?rev=360450&r1=360449&r2=360450&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp Fri May 10 11:27:09 2019
@@ -3,7 +3,7 @@
 #include "llvm/IncludeOrderCheck.h"
 #include "gtest/gtest.h"
 
-using namespace clang::tidy::llvm;
+using namespace clang::tidy::llvm_check;
 
 namespace clang {
 namespace tidy {




More information about the cfe-commits mailing list