2014-12-09 18:02:51 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
#===- add_new_check.py - clang-tidy check generator ----------*- python -*--===#
|
|
|
|
#
|
2019-01-19 16:50:56 +08:00
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-12-09 18:02:51 +08:00
|
|
|
#
|
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
|
2018-03-24 18:49:17 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-02-01 05:52:39 +08:00
|
|
|
import argparse
|
2014-12-09 18:02:51 +08:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# Adapts the module's CMakelist file. Returns 'True' if it could add a new entry
|
|
|
|
# and 'False' if the entry already existed.
|
|
|
|
def adapt_cmake(module_path, check_name_camel):
|
|
|
|
filename = os.path.join(module_path, 'CMakeLists.txt')
|
|
|
|
with open(filename, 'r') as f:
|
2015-09-10 21:56:39 +08:00
|
|
|
lines = f.readlines()
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
cpp_file = check_name_camel + '.cpp'
|
|
|
|
|
|
|
|
# Figure out whether this check already exists.
|
|
|
|
for line in lines:
|
|
|
|
if line.strip() == cpp_file:
|
|
|
|
return False
|
|
|
|
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Updating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2014-12-09 18:02:51 +08:00
|
|
|
cpp_found = False
|
|
|
|
file_added = False
|
|
|
|
for line in lines:
|
2015-09-10 21:56:39 +08:00
|
|
|
cpp_line = line.strip().endswith('.cpp')
|
2015-09-04 22:56:57 +08:00
|
|
|
if (not file_added) and (cpp_line or cpp_found):
|
2014-12-09 18:02:51 +08:00
|
|
|
cpp_found = True
|
2015-09-04 22:56:57 +08:00
|
|
|
if (line.strip() > cpp_file) or (not cpp_line):
|
2014-12-09 18:02:51 +08:00
|
|
|
f.write(' ' + cpp_file + '\n')
|
|
|
|
file_added = True
|
2015-09-10 21:56:39 +08:00
|
|
|
f.write(line)
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# Adds a header for the new check.
|
2019-05-11 02:27:09 +08:00
|
|
|
def write_header(module_path, module, namespace, check_name, check_name_camel):
|
2015-09-10 21:56:39 +08:00
|
|
|
check_name_dashes = module + '-' + check_name
|
2014-12-09 18:02:51 +08:00
|
|
|
filename = os.path.join(module_path, check_name_camel) + '.h'
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Creating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2016-04-13 16:46:32 +08:00
|
|
|
header_guard = ('LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_' + module.upper() + '_'
|
2017-11-25 16:49:04 +08:00
|
|
|
+ check_name_camel.upper() + '_H')
|
2014-12-09 18:02:51 +08:00
|
|
|
f.write('//===--- ')
|
|
|
|
f.write(os.path.basename(filename))
|
2018-09-20 08:02:55 +08:00
|
|
|
f.write(' - clang-tidy ')
|
|
|
|
f.write('-' * max(0, 42 - len(os.path.basename(filename))))
|
2014-12-09 18:02:51 +08:00
|
|
|
f.write('*- C++ -*-===//')
|
|
|
|
f.write("""
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-12-09 18:02:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef %(header_guard)s
|
|
|
|
#define %(header_guard)s
|
|
|
|
|
2019-03-25 22:46:51 +08:00
|
|
|
#include "../ClangTidyCheck.h"
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2019-05-11 02:27:09 +08:00
|
|
|
namespace %(namespace)s {
|
2014-12-09 18:02:51 +08:00
|
|
|
|
2015-09-10 21:56:39 +08:00
|
|
|
/// FIXME: Write a short description.
|
|
|
|
///
|
|
|
|
/// For the user-facing documentation see:
|
|
|
|
/// http://clang.llvm.org/extra/clang-tidy/checks/%(check_name_dashes)s.html
|
2014-12-09 18:02:51 +08:00
|
|
|
class %(check_name)s : public ClangTidyCheck {
|
|
|
|
public:
|
|
|
|
%(check_name)s(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context) {}
|
|
|
|
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
|
|
|
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
|
|
|
};
|
|
|
|
|
2019-05-11 02:27:09 +08:00
|
|
|
} // namespace %(namespace)s
|
2014-12-09 18:02:51 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // %(header_guard)s
|
|
|
|
""" % {'header_guard': header_guard,
|
2015-09-10 21:56:39 +08:00
|
|
|
'check_name': check_name_camel,
|
2015-12-16 23:05:27 +08:00
|
|
|
'check_name_dashes': check_name_dashes,
|
2019-05-11 02:27:09 +08:00
|
|
|
'module': module,
|
|
|
|
'namespace': namespace})
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Adds the implementation of the new check.
|
2019-05-11 02:27:09 +08:00
|
|
|
def write_implementation(module_path, module, namespace, check_name_camel):
|
2014-12-09 18:02:51 +08:00
|
|
|
filename = os.path.join(module_path, check_name_camel) + '.cpp'
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Creating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2014-12-09 18:02:51 +08:00
|
|
|
f.write('//===--- ')
|
|
|
|
f.write(os.path.basename(filename))
|
2018-09-20 08:02:55 +08:00
|
|
|
f.write(' - clang-tidy ')
|
|
|
|
f.write('-' * max(0, 51 - len(os.path.basename(filename))))
|
2014-12-09 18:02:51 +08:00
|
|
|
f.write('-===//')
|
|
|
|
f.write("""
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-12-09 18:02:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "%(check_name)s.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2019-05-11 02:27:09 +08:00
|
|
|
namespace %(namespace)s {
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
void %(check_name)s::registerMatchers(MatchFinder *Finder) {
|
|
|
|
// FIXME: Add matchers.
|
|
|
|
Finder->addMatcher(functionDecl().bind("x"), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void %(check_name)s::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
// FIXME: Add callback implementation.
|
2014-12-09 20:43:09 +08:00
|
|
|
const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
|
|
|
|
if (MatchedDecl->getName().startswith("awesome_"))
|
|
|
|
return;
|
2016-05-30 23:42:08 +08:00
|
|
|
diag(MatchedDecl->getLocation(), "function %%0 is insufficiently awesome")
|
[clang-tidy] Add fix descriptions to clang-tidy checks.
Summary:
Motivation/Context: in the code review system integrating with clang-tidy,
clang-tidy doesn't provide a human-readable description of the fix. Usually
developers have to preview a code diff (before vs after apply the fix) to
understand what the fix does before applying a fix.
This patch proposes that each clang-tidy check provides a short and
actional fix description that can be shown in the UI, so that users can know
what the fix does without previewing diff.
This patch extends clang-tidy framework to support fix descriptions (will add implementations for
existing checks in the future). Fix descriptions and fixes are emitted via diagnostic::Note (rather than
attaching the main warning diagnostic).
Before this patch:
```
void MyCheck::check(...) {
...
diag(loc, "my check warning") << FixtItHint::CreateReplacement(...);
}
```
After:
```
void MyCheck::check(...) {
...
diag(loc, "my check warning"); // Emit a check warning
diag(loc, "fix description", DiagnosticIDs::Note) << FixtItHint::CreateReplacement(...); // Emit a diagnostic note and a fix
}
```
Reviewers: sammccall, alexfh
Reviewed By: alexfh
Subscribers: MyDeveloperDay, Eugene.Zelenko, aaron.ballman, JonasToth, xazax.hun, jdoerfert, cfe-commits
Tags: #clang-tools-extra, #clang
Differential Revision: https://reviews.llvm.org/D59932
llvm-svn: 358576
2019-04-17 20:53:59 +08:00
|
|
|
<< MatchedDecl;
|
|
|
|
diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note)
|
2014-12-09 20:43:09 +08:00
|
|
|
<< FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
|
2014-12-09 18:02:51 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 02:27:09 +08:00
|
|
|
} // namespace %(namespace)s
|
2014-12-09 18:02:51 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
2015-12-16 23:05:27 +08:00
|
|
|
""" % {'check_name': check_name_camel,
|
2019-05-11 02:27:09 +08:00
|
|
|
'module': module,
|
|
|
|
'namespace': namespace})
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Modifies the module to include the new check.
|
|
|
|
def adapt_module(module_path, module, check_name, check_name_camel):
|
2018-03-24 18:49:17 +08:00
|
|
|
modulecpp = list(filter(
|
|
|
|
lambda p: p.lower() == module.lower() + 'tidymodule.cpp',
|
|
|
|
os.listdir(module_path)))[0]
|
2015-10-06 21:31:00 +08:00
|
|
|
filename = os.path.join(module_path, modulecpp)
|
2014-12-09 18:02:51 +08:00
|
|
|
with open(filename, 'r') as f:
|
2015-09-10 21:56:39 +08:00
|
|
|
lines = f.readlines()
|
2014-12-09 18:02:51 +08:00
|
|
|
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Updating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2014-12-09 18:02:51 +08:00
|
|
|
header_added = False
|
|
|
|
header_found = False
|
|
|
|
check_added = False
|
2019-08-31 04:47:02 +08:00
|
|
|
check_fq_name = module + '-' + check_name
|
2014-12-09 18:02:51 +08:00
|
|
|
check_decl = (' CheckFactories.registerCheck<' + check_name_camel +
|
2019-08-31 04:47:02 +08:00
|
|
|
'>(\n "' + check_fq_name + '");\n')
|
|
|
|
|
|
|
|
lines = iter(lines)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
line = lines.next()
|
|
|
|
if not header_added:
|
|
|
|
match = re.search('#include "(.*)"', line)
|
|
|
|
if match:
|
|
|
|
header_found = True
|
|
|
|
if match.group(1) > check_name_camel:
|
|
|
|
header_added = True
|
|
|
|
f.write('#include "' + check_name_camel + '.h"\n')
|
|
|
|
elif header_found:
|
2014-12-09 18:02:51 +08:00
|
|
|
header_added = True
|
|
|
|
f.write('#include "' + check_name_camel + '.h"\n')
|
2019-08-31 04:47:02 +08:00
|
|
|
|
|
|
|
if not check_added:
|
|
|
|
if line.strip() == '}':
|
2014-12-09 18:02:51 +08:00
|
|
|
check_added = True
|
|
|
|
f.write(check_decl)
|
2019-08-31 04:47:02 +08:00
|
|
|
else:
|
|
|
|
match = re.search('registerCheck<(.*)> *\( *(?:"([^"]*)")?', line)
|
|
|
|
prev_line = None
|
|
|
|
if match:
|
|
|
|
current_check_name = match.group(2)
|
|
|
|
if current_check_name is None:
|
|
|
|
# If we didn't find the check name on this line, look on the
|
|
|
|
# next one.
|
|
|
|
prev_line = line
|
|
|
|
line = lines.next()
|
|
|
|
match = re.search(' *"([^"]*)"', line)
|
|
|
|
if match:
|
|
|
|
current_check_name = match.group(1)
|
|
|
|
if current_check_name > check_fq_name:
|
|
|
|
check_added = True
|
|
|
|
f.write(check_decl)
|
|
|
|
if prev_line:
|
|
|
|
f.write(prev_line)
|
|
|
|
f.write(line)
|
|
|
|
except StopIteration:
|
|
|
|
pass
|
2014-12-09 18:02:51 +08:00
|
|
|
|
|
|
|
|
2017-07-12 21:13:41 +08:00
|
|
|
# Adds a release notes entry.
|
|
|
|
def add_release_notes(module_path, module, check_name):
|
|
|
|
check_name_dashes = module + '-' + check_name
|
|
|
|
filename = os.path.normpath(os.path.join(module_path,
|
|
|
|
'../../docs/ReleaseNotes.rst'))
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
2018-12-11 03:41:53 +08:00
|
|
|
lineMatcher = re.compile('Improvements to clang-tidy')
|
2019-03-25 22:09:10 +08:00
|
|
|
nextSectionMatcher = re.compile('Improvements to clang-include-fixer')
|
2018-12-11 03:41:53 +08:00
|
|
|
checkerMatcher = re.compile('- New :doc:`(.*)')
|
|
|
|
|
2017-07-12 21:13:41 +08:00
|
|
|
print('Updating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2017-07-12 21:13:41 +08:00
|
|
|
note_added = False
|
|
|
|
header_found = False
|
2018-12-11 03:41:53 +08:00
|
|
|
next_header_found = False
|
|
|
|
add_note_here = False
|
2017-07-12 21:13:41 +08:00
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if not note_added:
|
2018-12-11 03:41:53 +08:00
|
|
|
match = lineMatcher.match(line)
|
|
|
|
match_next = nextSectionMatcher.match(line)
|
|
|
|
match_checker = checkerMatcher.match(line)
|
|
|
|
if match_checker:
|
|
|
|
last_checker = match_checker.group(1)
|
|
|
|
if last_checker > check_name_dashes:
|
|
|
|
add_note_here = True
|
|
|
|
|
|
|
|
if match_next:
|
|
|
|
next_header_found = True
|
|
|
|
add_note_here = True
|
|
|
|
|
2017-07-12 21:13:41 +08:00
|
|
|
if match:
|
|
|
|
header_found = True
|
2018-12-11 03:41:53 +08:00
|
|
|
f.write(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if line.startswith('----'):
|
|
|
|
f.write(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if header_found and add_note_here:
|
2017-07-12 21:13:41 +08:00
|
|
|
if not line.startswith('----'):
|
2018-12-11 03:41:53 +08:00
|
|
|
f.write("""- New :doc:`%s
|
2018-05-03 23:59:39 +08:00
|
|
|
<clang-tidy/checks/%s>` check.
|
2017-07-12 21:13:41 +08:00
|
|
|
|
|
|
|
FIXME: add release notes.
|
2018-12-11 03:41:53 +08:00
|
|
|
|
2017-07-12 21:13:41 +08:00
|
|
|
""" % (check_name_dashes, check_name_dashes))
|
|
|
|
note_added = True
|
|
|
|
|
|
|
|
f.write(line)
|
|
|
|
|
|
|
|
|
2014-12-09 18:02:51 +08:00
|
|
|
# Adds a test for the check.
|
2018-02-01 05:52:39 +08:00
|
|
|
def write_test(module_path, module, check_name, test_extension):
|
2014-12-09 20:43:09 +08:00
|
|
|
check_name_dashes = module + '-' + check_name
|
2019-10-11 21:46:55 +08:00
|
|
|
filename = os.path.normpath(os.path.join(module_path, '../../test/clang-tidy/checkers',
|
2018-02-01 05:52:39 +08:00
|
|
|
check_name_dashes + '.' + test_extension))
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Creating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2016-04-13 16:46:32 +08:00
|
|
|
f.write("""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
|
2014-12-09 18:02:51 +08:00
|
|
|
|
2014-12-09 20:43:09 +08:00
|
|
|
// FIXME: Add something that triggers the check here.
|
2014-12-09 18:02:51 +08:00
|
|
|
void f();
|
2014-12-09 20:43:09 +08:00
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [%(check_name_dashes)s]
|
2014-12-09 18:02:51 +08:00
|
|
|
|
2014-12-09 20:43:09 +08:00
|
|
|
// FIXME: Verify the applied fix.
|
|
|
|
// * Make the CHECK patterns specific enough and try to make verified lines
|
|
|
|
// unique to avoid incorrect matches.
|
|
|
|
// * Use {{}} for regular expressions.
|
|
|
|
// CHECK-FIXES: {{^}}void awesome_f();{{$}}
|
|
|
|
|
|
|
|
// FIXME: Add something that doesn't trigger the check here.
|
|
|
|
void awesome_f2();
|
2016-04-13 16:46:32 +08:00
|
|
|
""" % {'check_name_dashes': check_name_dashes})
|
|
|
|
|
2014-12-09 18:02:51 +08:00
|
|
|
|
2015-09-10 21:56:39 +08:00
|
|
|
# Recreates the list of checks in the docs/clang-tidy/checks directory.
|
2016-01-29 23:54:26 +08:00
|
|
|
def update_checks_list(clang_tidy_path):
|
|
|
|
docs_dir = os.path.join(clang_tidy_path, '../docs/clang-tidy/checks')
|
2016-01-12 00:48:26 +08:00
|
|
|
filename = os.path.normpath(os.path.join(docs_dir, 'list.rst'))
|
2015-09-10 21:56:39 +08:00
|
|
|
with open(filename, 'r') as f:
|
|
|
|
lines = f.readlines()
|
2018-03-24 18:49:17 +08:00
|
|
|
doc_files = list(filter(lambda s: s.endswith('.rst') and s != 'list.rst',
|
|
|
|
os.listdir(docs_dir)))
|
2016-01-12 00:48:26 +08:00
|
|
|
doc_files.sort()
|
|
|
|
|
|
|
|
def format_link(doc_file):
|
|
|
|
check_name = doc_file.replace('.rst', '')
|
|
|
|
with open(os.path.join(docs_dir, doc_file), 'r') as doc:
|
2016-12-02 01:24:42 +08:00
|
|
|
content = doc.read()
|
|
|
|
match = re.search('.*:orphan:.*', content)
|
|
|
|
if match:
|
|
|
|
return ''
|
|
|
|
|
2016-04-13 16:46:32 +08:00
|
|
|
match = re.search('.*:http-equiv=refresh: \d+;URL=(.*).html.*',
|
2016-12-02 01:24:42 +08:00
|
|
|
content)
|
2016-01-12 00:48:26 +08:00
|
|
|
if match:
|
|
|
|
return ' %(check)s (redirects to %(target)s) <%(check)s>\n' % {
|
2016-04-13 16:46:32 +08:00
|
|
|
'check': check_name,
|
|
|
|
'target': match.group(1)
|
|
|
|
}
|
2016-01-12 00:48:26 +08:00
|
|
|
return ' %s\n' % check_name
|
|
|
|
|
|
|
|
checks = map(format_link, doc_files)
|
2015-09-10 21:56:39 +08:00
|
|
|
|
|
|
|
print('Updating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2015-09-10 21:56:39 +08:00
|
|
|
for line in lines:
|
|
|
|
f.write(line)
|
|
|
|
if line.startswith('.. toctree::'):
|
|
|
|
f.writelines(checks)
|
|
|
|
break
|
|
|
|
|
2016-04-13 16:46:32 +08:00
|
|
|
|
2015-09-10 21:56:39 +08:00
|
|
|
# Adds a documentation for the check.
|
|
|
|
def write_docs(module_path, module, check_name):
|
|
|
|
check_name_dashes = module + '-' + check_name
|
2016-04-13 16:46:32 +08:00
|
|
|
filename = os.path.normpath(os.path.join(
|
|
|
|
module_path, '../../docs/clang-tidy/checks/', check_name_dashes + '.rst'))
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Creating %s...' % filename)
|
2018-03-24 18:49:17 +08:00
|
|
|
with open(filename, 'w') as f:
|
2016-04-13 16:46:32 +08:00
|
|
|
f.write(""".. title:: clang-tidy - %(check_name_dashes)s
|
2015-12-23 01:36:49 +08:00
|
|
|
|
|
|
|
%(check_name_dashes)s
|
2015-09-10 21:56:39 +08:00
|
|
|
%(underline)s
|
|
|
|
|
|
|
|
FIXME: Describe what patterns does the check detect and why. Give examples.
|
2016-04-13 16:46:32 +08:00
|
|
|
""" % {'check_name_dashes': check_name_dashes,
|
|
|
|
'underline': '=' * len(check_name_dashes)})
|
|
|
|
|
2015-09-10 21:56:39 +08:00
|
|
|
|
2014-12-09 18:02:51 +08:00
|
|
|
def main():
|
2018-02-01 05:52:39 +08:00
|
|
|
language_to_extension = {
|
|
|
|
'c': 'c',
|
|
|
|
'c++': 'cpp',
|
|
|
|
'objc': 'm',
|
|
|
|
'objc++': 'mm',
|
|
|
|
}
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'--update-docs',
|
|
|
|
action='store_true',
|
|
|
|
help='just update the list of documentation files, then exit')
|
|
|
|
parser.add_argument(
|
|
|
|
'--language',
|
|
|
|
help='language to use for new check (defaults to c++)',
|
|
|
|
choices=language_to_extension.keys(),
|
|
|
|
default='c++',
|
|
|
|
metavar='LANG')
|
|
|
|
parser.add_argument(
|
|
|
|
'module',
|
2018-02-28 20:21:38 +08:00
|
|
|
nargs='?',
|
2018-02-01 05:52:39 +08:00
|
|
|
help='module directory under which to place the new tidy check (e.g., misc)')
|
|
|
|
parser.add_argument(
|
|
|
|
'check',
|
2018-02-28 20:21:38 +08:00
|
|
|
nargs='?',
|
2018-02-01 05:52:39 +08:00
|
|
|
help='name of new tidy check to add (e.g. foo-do-the-stuff)')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.update_docs:
|
2016-01-29 23:54:26 +08:00
|
|
|
update_checks_list(os.path.dirname(sys.argv[0]))
|
|
|
|
return
|
|
|
|
|
2018-02-28 20:21:38 +08:00
|
|
|
if not args.module or not args.check:
|
2018-03-24 18:49:17 +08:00
|
|
|
print('Module and check must be specified.')
|
2018-02-28 20:21:38 +08:00
|
|
|
parser.print_usage()
|
|
|
|
return
|
|
|
|
|
2018-02-01 05:52:39 +08:00
|
|
|
module = args.module
|
|
|
|
check_name = args.check
|
2016-04-13 16:46:32 +08:00
|
|
|
|
|
|
|
if check_name.startswith(module):
|
2018-03-24 18:49:17 +08:00
|
|
|
print('Check name "%s" must not start with the module "%s". Exiting.' % (
|
|
|
|
check_name, module))
|
2016-04-13 16:46:32 +08:00
|
|
|
return
|
2014-12-09 18:02:51 +08:00
|
|
|
check_name_camel = ''.join(map(lambda elem: elem.capitalize(),
|
|
|
|
check_name.split('-'))) + 'Check'
|
|
|
|
clang_tidy_path = os.path.dirname(sys.argv[0])
|
|
|
|
module_path = os.path.join(clang_tidy_path, module)
|
|
|
|
|
|
|
|
if not adapt_cmake(module_path, check_name_camel):
|
|
|
|
return
|
2019-05-11 02:27:09 +08:00
|
|
|
|
|
|
|
# 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)
|
2014-12-09 18:02:51 +08:00
|
|
|
adapt_module(module_path, module, check_name, check_name_camel)
|
2017-07-12 21:13:41 +08:00
|
|
|
add_release_notes(module_path, module, check_name)
|
2018-02-01 05:52:39 +08:00
|
|
|
test_extension = language_to_extension.get(args.language)
|
|
|
|
write_test(module_path, module, check_name, test_extension)
|
2015-09-10 21:56:39 +08:00
|
|
|
write_docs(module_path, module, check_name)
|
2016-01-29 23:54:26 +08:00
|
|
|
update_checks_list(clang_tidy_path)
|
2015-09-10 21:56:39 +08:00
|
|
|
print('Done. Now it\'s your turn!')
|
2014-12-09 18:02:51 +08:00
|
|
|
|
2016-04-13 16:46:32 +08:00
|
|
|
|
2014-12-09 18:02:51 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|