1
0

Codestyle.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var Fs = require('fs');
  16. var nThen = require('nthen');
  17. var Semaphore = require('./Semaphore');
  18. var Child = require('child_process');
  19. var headerLines = [
  20. '/* vim: set expandtab ts=4 sw=4: */',
  21. '/*',
  22. ' * You may redistribute this program and/or modify it under the terms of',
  23. ' * the GNU General Public License as published by the Free Software Foundation,',
  24. ' * either version 3 of the License, or (at your option) any later version.',
  25. ' *',
  26. ' * This program is distributed in the hope that it will be useful,',
  27. ' * but WITHOUT ANY WARRANTY; without even the implied warranty of',
  28. ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the',
  29. ' * GNU General Public License for more details.',
  30. ' *',
  31. ' * You should have received a copy of the GNU General Public License',
  32. ' * along with this program. If not, see <http://www.gnu.org/licenses/>.',
  33. ' */'
  34. ];
  35. var parseFile = function (fileName, fileContent) {
  36. var output = '';
  37. var parenthCount = 0;
  38. var functionParenthCount = 0;
  39. var expectBracket = 0;
  40. var name = fileName.replace(/^.*\//, '').replace(/\..*$/,'');
  41. var lines = fileContent.split('\n');
  42. for (var lineNum = 0; lineNum < lines.length; lineNum++) {
  43. var line = lines[lineNum];
  44. // switch to 1 indexing for human readability
  45. var lineInfo = fileName + ":" + (lineNum+1);
  46. var ignore = false;
  47. var error = function(msg) {
  48. if (!ignore) {
  49. output += lineInfo + ' ' + msg + '\n';
  50. }
  51. };
  52. if (lineNum < headerLines.length) {
  53. var expectedLine = headerLines[lineNum];
  54. if (line !== headerLines[lineNum]) {
  55. error("missing header\n" + expectedLine + "\n" + line);
  56. }
  57. } else if (/\.h$/.test(fileName) && lineNum < headerLines.length + 1) {
  58. if (line !== '#ifndef ' + name + "_H") {
  59. error("expected #ifndef " + name + "_H found " + line);
  60. }
  61. } else if (/\.h$/.test(fileName) && lineNum < headerLines.length + 2) {
  62. if (line !== '#define ' + name + "_H") {
  63. error("expected #define " + name + "_H found " + line);
  64. }
  65. }
  66. ignore = /CHECKFILES_IGNORE/.test(line);
  67. if (expectBracket == 1) {
  68. expectBracket = 0;
  69. if (!(/^[\s]*{/.test(line))) {
  70. error("expecting a { bracket " + line);
  71. }
  72. }
  73. // implementations.. TUNConfigurator_Linux contains TUNConfigurator_doStuff...
  74. var n = name.replace(/_.*/, '');
  75. if ((/^\w+\s.*\(/).test(line)) {
  76. if (!(/^int main\(/.test(line)
  77. || line.indexOf(' '+n) > -1
  78. || /^[ ]?static /.test(line)
  79. || /^typedef /.test(line)))
  80. {
  81. error("all globally visible functions must begin with the name of the file.");
  82. }
  83. }
  84. var matches;
  85. if (functionParenthCount === 0) {
  86. matches = /^\w+\s.*(\(.*)$/.exec(line);
  87. }
  88. if (functionParenthCount > 0 || matches) {
  89. var txt = (functionParenthCount > 0) ? line : matches[1];
  90. functionParenthCount += (txt.match(/\(/g)||[]).length;
  91. functionParenthCount -= (txt.match(/\)/g)||[]).length;
  92. if (functionParenthCount === 0) {
  93. txt = txt.substring(txt.lastIndexOf(')') + 1);
  94. if (/{/.test(txt)) {
  95. error("please put the opening bracket on the next line.");
  96. }
  97. }
  98. }
  99. if (/[\w]*int[\w]*\s+\*+\w/.test(line) || /[\w]*struct\s+[\w]+\s+\*+\w/.test(line)) {
  100. error("int* blah; means int pointer named blah, int *blah; means int names splatblah");
  101. }
  102. if (line.length > 100) {
  103. error("cjd's editor window is only 100 characters wide");
  104. }
  105. if (/\.h$/.test(fileName) && fileName.indexOf('util/platform/libc/') === -1) {
  106. // If the name is CryptoAuth_pvt.h, it's ok to make a structure called CryptoAuth
  107. var n = name.replace(/_pvt$/, '').replace(/_impl$/, '');
  108. if (/^struct /.test(line) && line.indexOf('struct ' + n) !== 0 && !(/\(/.test(line))) {
  109. error("all structures must begin with the name of the file.");
  110. }
  111. if (/#define /.test(line) && line.indexOf('#define ' + n) === -1) {
  112. error("all defines must begin with the name of the file.");
  113. }
  114. }
  115. if (/\t/.test(line)) {
  116. error("tabs are not allowed, use 4 spaces.");
  117. }
  118. if (/\s$/.test(line)) {
  119. error("trailing whitespace.");
  120. }
  121. if (/(if|for|while)\(/.test(line)) {
  122. error("If/for/while statements must be followed by whitespace.");
  123. }
  124. matches = null;
  125. if (parenthCount === 0) {
  126. matches = /[^\w#](if|for|while) (\(.*$)/.exec(line);
  127. }
  128. if (parenthCount > 0 || matches) {
  129. var txt = (parenthCount > 0) ? line : matches[2];
  130. parenthCount += (txt.match(/\(/g)||[]).length;
  131. parenthCount -= (txt.match(/\)/g)||[]).length;
  132. if (parenthCount == 0) {
  133. txt = txt.substring(txt.lastIndexOf(')') + 1);
  134. // for (x; y; z) ; <-- ok
  135. // for (x; y; z) { <-- ok
  136. // for (x; y; z) { \ <-- ok (in preprocessor macro)
  137. // for (x; y; z) <-- ok but you better put a bracket on the next line
  138. // for (x; y; z) { j++; } <-- ok
  139. // for (x; y; z) j++; <-- BZZZZZZZZZZT
  140. if (!(/^[\s]*[;{].*$/.test(txt)) && !(/^[\s]+{[\s]*\\$/).test(txt)) {
  141. if (/[\s]*$/.test(txt)) {
  142. expectBracket = 1;
  143. } else {
  144. error(parenthCount + ' ' + line);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return output;
  151. };
  152. var checkFile = module.exports.checkFile = function (file, callback) {
  153. Fs.readFile(file, function (err, ret) {
  154. if (err) { throw err; }
  155. callback(parseFile(file, ret.toString()));
  156. });
  157. };
  158. var checkFiles = module.exports.checkFiles = function (files, callback) {
  159. var sema = Semaphore.create(64);
  160. var errors = '';
  161. nThen(function (waitFor) {
  162. files.forEach(function (file) {
  163. sema.take(waitFor(function (returnAfter) {
  164. checkFile(file, waitFor(returnAfter(function (err) {
  165. if (err) {
  166. errors += file + '\n' + err + '\n';
  167. }
  168. })));
  169. }));
  170. });
  171. }).nThen(function (waitFor) {
  172. callback(errors);
  173. });
  174. };
  175. var checkDir = module.exports.checkDir = function (dir, runInFork, callback) {
  176. var gitIgnoreLines;
  177. if (runInFork) {
  178. var err = '';
  179. var out = '';
  180. var proc = Child.spawn(process.execPath, [__filename]);
  181. proc.stdout.on('data', function (data) { err += data.toString('utf8') });
  182. proc.stderr.on('data', function (data) { err += data.toString('utf8') });
  183. proc.on('close', function (ret) {
  184. out += err;
  185. var error;
  186. if (ret !== 0) { error = new Error(out); }
  187. callback(error, out);
  188. });
  189. return;
  190. }
  191. var output = '';
  192. nThen(function (waitFor) {
  193. Fs.readFile('.gitignore', waitFor(function (err, ret) {
  194. if (err) { throw err; }
  195. gitIgnoreLines = ret.toString('utf8').split('\n');
  196. }))
  197. }).nThen(function (waitFor) {
  198. var addDir = function (dir) {
  199. Fs.readdir(dir, waitFor(function (err, files) {
  200. if (err) { throw err; }
  201. files.forEach(function (file) {
  202. Fs.stat(dir + '/' + file, waitFor(function (err, stat) {
  203. if (err) { throw err; }
  204. if (file === '.git') {
  205. } else if (file === 'contrib') {
  206. } else if (file === 'dependencies') {
  207. } else if (gitIgnoreLines.indexOf(file) !== -1) {
  208. } else {
  209. if (stat.isDirectory()) {
  210. addDir(dir + '/' + file);
  211. } else if (/.*(\.c|\.h)$/.test(file)) {
  212. checkFile(dir + '/' + file, waitFor(function (ret) {
  213. output += ret;
  214. }));
  215. }
  216. }
  217. }));
  218. });
  219. }));
  220. };
  221. addDir(dir);
  222. }).nThen(function (waitFor) {
  223. callback(output);
  224. });
  225. };
  226. if (module.parent === null) {
  227. checkDir('.', false, function(output) {
  228. if (output !== '') {
  229. console.log(output);
  230. process.exit(1);
  231. }
  232. });
  233. }