blob: 3d566ef2b5d6ed9ea34dcd0a00ba0eac27ea7e5c [file] [log] [blame]
Daniel Jasper4ecc8b32014-12-09 10:02:51 +00001#!/usr/bin/env python
2#
3#===- add_new_check.py - clang-tidy check generator ----------*- python -*--===#
4#
5# The LLVM Compiler Infrastructure
6#
7# This file is distributed under the University of Illinois Open Source
8# License. See LICENSE.TXT for details.
9#
10#===------------------------------------------------------------------------===#
11
12import os
13import re
14import sys
15
16
17# Adapts the module's CMakelist file. Returns 'True' if it could add a new entry
18# and 'False' if the entry already existed.
19def adapt_cmake(module_path, check_name_camel):
20 filename = os.path.join(module_path, 'CMakeLists.txt')
21 with open(filename, 'r') as f:
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000022 lines = f.readlines()
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000023
24 cpp_file = check_name_camel + '.cpp'
25
26 # Figure out whether this check already exists.
27 for line in lines:
28 if line.strip() == cpp_file:
29 return False
30
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000031 print('Updating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +000032 with open(filename, 'wb') as f:
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000033 cpp_found = False
34 file_added = False
35 for line in lines:
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000036 cpp_line = line.strip().endswith('.cpp')
Alexander Kornienkoc0ebfbe52015-09-04 14:56:57 +000037 if (not file_added) and (cpp_line or cpp_found):
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000038 cpp_found = True
Alexander Kornienkoc0ebfbe52015-09-04 14:56:57 +000039 if (line.strip() > cpp_file) or (not cpp_line):
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000040 f.write(' ' + cpp_file + '\n')
41 file_added = True
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000042 f.write(line)
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000043
44 return True
45
46
47# Adds a header for the new check.
48def write_header(module_path, module, check_name, check_name_camel):
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000049 check_name_dashes = module + '-' + check_name
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000050 filename = os.path.join(module_path, check_name_camel) + '.h'
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000051 print('Creating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +000052 with open(filename, 'wb') as f:
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000053 header_guard = ('LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_' + module.upper() +
54 '_' + check_name.upper().replace('-', '_') + '_H')
55 f.write('//===--- ')
56 f.write(os.path.basename(filename))
57 f.write(' - clang-tidy')
58 f.write('-' * max(0, 43 - len(os.path.basename(filename))))
59 f.write('*- C++ -*-===//')
60 f.write("""
61//
62// The LLVM Compiler Infrastructure
63//
64// This file is distributed under the University of Illinois Open Source
65// License. See LICENSE.TXT for details.
66//
67//===----------------------------------------------------------------------===//
68
69#ifndef %(header_guard)s
70#define %(header_guard)s
71
72#include "../ClangTidy.h"
73
74namespace clang {
75namespace tidy {
76
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000077/// FIXME: Write a short description.
78///
79/// For the user-facing documentation see:
80/// http://clang.llvm.org/extra/clang-tidy/checks/%(check_name_dashes)s.html
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000081class %(check_name)s : public ClangTidyCheck {
82public:
83 %(check_name)s(StringRef Name, ClangTidyContext *Context)
84 : ClangTidyCheck(Name, Context) {}
85 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
86 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
87};
88
89} // namespace tidy
90} // namespace clang
91
92#endif // %(header_guard)s
93
94""" % {'header_guard': header_guard,
Alexander Kornienko3285f1b2015-09-10 13:56:39 +000095 'check_name': check_name_camel,
96 'check_name_dashes': check_name_dashes})
Daniel Jasper4ecc8b32014-12-09 10:02:51 +000097
98
99# Adds the implementation of the new check.
100def write_implementation(module_path, check_name_camel):
101 filename = os.path.join(module_path, check_name_camel) + '.cpp'
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000102 print('Creating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +0000103 with open(filename, 'wb') as f:
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000104 f.write('//===--- ')
105 f.write(os.path.basename(filename))
106 f.write(' - clang-tidy')
107 f.write('-' * max(0, 52 - len(os.path.basename(filename))))
108 f.write('-===//')
109 f.write("""
110//
111// The LLVM Compiler Infrastructure
112//
113// This file is distributed under the University of Illinois Open Source
114// License. See LICENSE.TXT for details.
115//
116//===----------------------------------------------------------------------===//
117
118#include "%(check_name)s.h"
119#include "clang/AST/ASTContext.h"
120#include "clang/ASTMatchers/ASTMatchFinder.h"
121
122using namespace clang::ast_matchers;
123
124namespace clang {
125namespace tidy {
126
127void %(check_name)s::registerMatchers(MatchFinder *Finder) {
128 // FIXME: Add matchers.
129 Finder->addMatcher(functionDecl().bind("x"), this);
130}
131
132void %(check_name)s::check(const MatchFinder::MatchResult &Result) {
133 // FIXME: Add callback implementation.
Alexander Kornienko423236b2014-12-09 12:43:09 +0000134 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
135 if (MatchedDecl->getName().startswith("awesome_"))
136 return;
137 diag(MatchedDecl->getLocation(), "function '%%0' is insufficiently awesome")
138 << MatchedDecl->getName()
139 << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000140}
141
142} // namespace tidy
143} // namespace clang
144
145""" % {'check_name': check_name_camel})
146
147
148# Modifies the module to include the new check.
149def adapt_module(module_path, module, check_name, check_name_camel):
Aaron Ballmanaaa40802015-10-06 13:31:00 +0000150 modulecpp = filter(lambda p: p.lower() == module.lower() + "tidymodule.cpp", os.listdir(module_path))[0]
151 filename = os.path.join(module_path, modulecpp)
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000152 with open(filename, 'r') as f:
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000153 lines = f.readlines()
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000154
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000155 print('Updating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +0000156 with open(filename, 'wb') as f:
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000157 header_added = False
158 header_found = False
159 check_added = False
160 check_decl = (' CheckFactories.registerCheck<' + check_name_camel +
161 '>(\n "' + module + '-' + check_name + '");\n')
162
163 for line in lines:
164 if not header_added:
165 match = re.search('#include "(.*)"', line)
166 if match:
167 header_found = True
168 if match.group(1) > check_name_camel:
169 header_added = True
170 f.write('#include "' + check_name_camel + '.h"\n')
171 elif header_found:
172 header_added = True
173 f.write('#include "' + check_name_camel + '.h"\n')
174
175 if not check_added:
176 if line.strip() == '}':
177 check_added = True
178 f.write(check_decl)
179 else:
180 match = re.search('registerCheck<(.*)>', line)
181 if match and match.group(1) > check_name_camel:
182 check_added = True
183 f.write(check_decl)
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000184 f.write(line)
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000185
186
187# Adds a test for the check.
188def write_test(module_path, module, check_name):
Alexander Kornienko423236b2014-12-09 12:43:09 +0000189 check_name_dashes = module + '-' + check_name
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000190 filename = os.path.normpath(
191 os.path.join(module_path, '../../test/clang-tidy',
192 check_name_dashes + '.cpp'))
193 print('Creating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +0000194 with open(filename, 'wb') as f:
Alexander Kornienko423236b2014-12-09 12:43:09 +0000195 f.write(
Matthias Gehre9ec20032015-10-26 21:48:08 +0000196"""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000197
Alexander Kornienko423236b2014-12-09 12:43:09 +0000198// FIXME: Add something that triggers the check here.
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000199void f();
Alexander Kornienko423236b2014-12-09 12:43:09 +0000200// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [%(check_name_dashes)s]
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000201
Alexander Kornienko423236b2014-12-09 12:43:09 +0000202// FIXME: Verify the applied fix.
203// * Make the CHECK patterns specific enough and try to make verified lines
204// unique to avoid incorrect matches.
205// * Use {{}} for regular expressions.
206// CHECK-FIXES: {{^}}void awesome_f();{{$}}
207
208// FIXME: Add something that doesn't trigger the check here.
209void awesome_f2();
210""" % {"check_name_dashes" : check_name_dashes})
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000211
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000212# Recreates the list of checks in the docs/clang-tidy/checks directory.
213def update_checks_list(module_path):
214 filename = os.path.normpath(
215 os.path.join(module_path, '../../docs/clang-tidy/checks/list.rst'))
216 with open(filename, 'r') as f:
217 lines = f.readlines()
218
219 checks = map(lambda s: ' ' + s.replace('.rst', '\n'),
220 filter(lambda s: s.endswith('.rst') and s != 'list.rst',
Alexander Kornienkoeef2c232015-10-01 09:23:20 +0000221 os.listdir(os.path.join(module_path, '../../docs/clang-tidy/checks'))))
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000222 checks.sort()
223
224 print('Updating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +0000225 with open(filename, 'wb') as f:
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000226 for line in lines:
227 f.write(line)
228 if line.startswith('.. toctree::'):
229 f.writelines(checks)
230 break
231
232# Adds a documentation for the check.
233def write_docs(module_path, module, check_name):
234 check_name_dashes = module + '-' + check_name
235 filename = os.path.normpath(
236 os.path.join(module_path, '../../docs/clang-tidy/checks/',
237 check_name_dashes + '.rst'))
238 print('Creating %s...' % filename)
Aaron Ballman017bfee2015-10-06 19:11:12 +0000239 with open(filename, 'wb') as f:
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000240 f.write(
241"""%(check_name_dashes)s
242%(underline)s
243
244FIXME: Describe what patterns does the check detect and why. Give examples.
245""" % {"check_name_dashes" : check_name_dashes,
246 "underline" : "=" * len(check_name_dashes)})
247
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000248def main():
249 if len(sys.argv) != 3:
250 print 'Usage: add_new_check.py <module> <check>, e.g.\n'
Alexander Kornienko423236b2014-12-09 12:43:09 +0000251 print 'add_new_check.py misc awesome-functions\n'
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000252 return
253
254 module = sys.argv[1]
255 check_name = sys.argv[2]
256 check_name_camel = ''.join(map(lambda elem: elem.capitalize(),
257 check_name.split('-'))) + 'Check'
258 clang_tidy_path = os.path.dirname(sys.argv[0])
259 module_path = os.path.join(clang_tidy_path, module)
260
261 if not adapt_cmake(module_path, check_name_camel):
262 return
263 write_header(module_path, module, check_name, check_name_camel)
264 write_implementation(module_path, check_name_camel)
265 adapt_module(module_path, module, check_name, check_name_camel)
266 write_test(module_path, module, check_name)
Alexander Kornienko3285f1b2015-09-10 13:56:39 +0000267 write_docs(module_path, module, check_name)
268 update_checks_list(module_path)
269 print('Done. Now it\'s your turn!')
Daniel Jasper4ecc8b32014-12-09 10:02:51 +0000270
271if __name__ == '__main__':
272 main()