nxstyle.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. /********************************************************************************
  2. * tools/nxstyle.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ********************************************************************************/
  20. /********************************************************************************
  21. * Included Files
  22. ********************************************************************************/
  23. #include <stdlib.h>
  24. #include <stdbool.h>
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <ctype.h>
  30. #include <limits.h>
  31. #include <unistd.h>
  32. #include <libgen.h>
  33. /********************************************************************************
  34. * Pre-processor Definitions
  35. ********************************************************************************/
  36. #define NXSTYLE_VERSION "0.01"
  37. #define LINE_SIZE 512
  38. #define RANGE_NUMBER 4096
  39. #define DEFAULT_WIDTH 78
  40. #define FIRST_SECTION INCLUDED_FILES
  41. #define LAST_SECTION PUBLIC_FUNCTION_PROTOTYPES
  42. #define FATAL(m, l, o) message(FATAL, (m), (l), (o))
  43. #define FATALFL(m, s) message(FATAL, (m), -1, -1)
  44. #define WARN(m, l, o) message(WARN, (m), (l), (o))
  45. #define ERROR(m, l, o) message(ERROR, (m), (l), (o))
  46. #define ERRORFL(m, s) message(ERROR, (m), -1, -1)
  47. #define INFO(m, l, o) message(INFO, (m), (l), (o))
  48. #define INFOFL(m, s) message(INFO, (m), -1, -1)
  49. /********************************************************************************
  50. * Private types
  51. ********************************************************************************/
  52. enum class_e
  53. {
  54. INFO,
  55. WARN,
  56. ERROR,
  57. FATAL
  58. };
  59. const char *class_text[] =
  60. {
  61. "info",
  62. "warning",
  63. "error",
  64. "fatal"
  65. };
  66. enum file_e
  67. {
  68. UNKNOWN = 0x00,
  69. C_HEADER = 0x01,
  70. C_SOURCE = 0x02
  71. };
  72. enum section_s
  73. {
  74. NO_SECTION = 0,
  75. INCLUDED_FILES,
  76. PRE_PROCESSOR_DEFINITIONS,
  77. PUBLIC_TYPES,
  78. PRIVATE_TYPES,
  79. PRIVATE_DATA,
  80. PUBLIC_DATA,
  81. PRIVATE_FUNCTIONS,
  82. PRIVATE_FUNCTION_PROTOTYPES,
  83. INLINE_FUNCTIONS,
  84. PUBLIC_FUNCTIONS,
  85. PUBLIC_FUNCTION_PROTOTYPES
  86. };
  87. enum pptype_e
  88. {
  89. PPLINE_NONE = 0,
  90. PPLINE_DEFINE,
  91. PPLINE_IF,
  92. PPLINE_ELIF,
  93. PPLINE_ELSE,
  94. PPLINE_ENDIF,
  95. PPLINE_OTHER
  96. };
  97. struct file_section_s
  98. {
  99. const char *name; /* File section name */
  100. uint8_t ftype; /* File type where section found */
  101. };
  102. /********************************************************************************
  103. * Private data
  104. ********************************************************************************/
  105. static char *g_file_name = "";
  106. static enum file_e g_file_type = UNKNOWN;
  107. static enum section_s g_section = NO_SECTION;
  108. static int g_maxline = DEFAULT_WIDTH;
  109. static int g_status = 0;
  110. static int g_verbose = 2;
  111. static int g_rangenumber = 0;
  112. static int g_rangestart[RANGE_NUMBER];
  113. static int g_rangecount[RANGE_NUMBER];
  114. static const struct file_section_s g_section_info[] =
  115. {
  116. {
  117. " *\n", /* Index: NO_SECTION */
  118. C_SOURCE | C_HEADER
  119. },
  120. {
  121. " * Included Files\n", /* Index: INCLUDED_FILES */
  122. C_SOURCE | C_HEADER
  123. },
  124. {
  125. " * Pre-processor Definitions\n", /* Index: PRE_PROCESSOR_DEFINITIONS */
  126. C_SOURCE | C_HEADER
  127. },
  128. {
  129. " * Public Types\n", /* Index: PUBLIC_TYPES */
  130. C_HEADER
  131. },
  132. {
  133. " * Private Types\n", /* Index: PRIVATE_TYPES */
  134. C_SOURCE
  135. },
  136. {
  137. " * Private Data\n", /* Index: PRIVATE_DATA */
  138. C_SOURCE
  139. },
  140. {
  141. " * Public Data\n", /* Index: PUBLIC_DATA */
  142. C_SOURCE | C_HEADER
  143. },
  144. {
  145. " * Private Functions\n", /* Index: PRIVATE_FUNCTIONS */
  146. C_SOURCE
  147. },
  148. {
  149. " * Private Function Prototypes\n", /* Index: PRIVATE_FUNCTION_PROTOTYPES */
  150. C_SOURCE
  151. },
  152. {
  153. " * Inline Functions\n", /* Index: INLINE_FUNCTIONS */
  154. C_SOURCE | C_HEADER
  155. },
  156. {
  157. " * Public Functions\n", /* Index: PUBLIC_FUNCTIONS */
  158. C_SOURCE
  159. },
  160. {
  161. " * Public Function Prototypes\n", /* Index: PUBLIC_FUNCTION_PROTOTYPES */
  162. C_SOURCE | C_HEADER
  163. }
  164. };
  165. static const char *g_white_prefix[] =
  166. {
  167. "Elf", /* Ref: include/elf.h, include/elf32.h, include/elf64.h */
  168. "PRIx", /* Ref: intttypes.h */
  169. "SYS_", /* Ref: include/sys/syscall.h */
  170. "STUB_", /* Ref: syscall/syscall_lookup.h, syscall/sycall_stublookup.c */
  171. "b8", /* Ref: include/fixedmath.h */
  172. "b16", /* Ref: include/fixedmath.h */
  173. "b32", /* Ref: include/fixedmath.h */
  174. "ub8", /* Ref: include/fixedmath.h */
  175. "ub16", /* Ref: include/fixedmath.h */
  176. "ub32", /* Ref: include/fixedmath.h */
  177. NULL
  178. };
  179. static const char *g_white_list[] =
  180. {
  181. "__EIT_entry", /* Ref: gnu_unwind_find_exidx.c */
  182. "__gnu_Unwind_Find_exidx", /* Ref: gnu_unwind_find_exidx.c */
  183. "_Exit", /* Ref: stdlib.h */
  184. "_Unwind_Ptr", /* Ref: unwind-arm-common.h */
  185. NULL
  186. };
  187. /********************************************************************************
  188. * Private Functions
  189. ********************************************************************************/
  190. /********************************************************************************
  191. * Name: show_usage
  192. *
  193. * Description:
  194. *
  195. ********************************************************************************/
  196. static void show_usage(char *progname, int exitcode, char *what)
  197. {
  198. fprintf(stderr, "%s version %s\n\n", basename(progname), NXSTYLE_VERSION);
  199. if (what)
  200. {
  201. fprintf(stderr, "%s\n", what);
  202. }
  203. fprintf(stderr, "Usage: %s [-m <excess>] [-v <level>] "
  204. "[-r <start,count>] <filename>\n",
  205. basename(progname));
  206. fprintf(stderr, " %s -h this help\n", basename(progname));
  207. fprintf(stderr, " %s -v <level> where level is\n",
  208. basename(progname));
  209. fprintf(stderr, " 0 - no output\n");
  210. fprintf(stderr, " 1 - PASS/FAIL\n");
  211. fprintf(stderr, " 2 - output each line (default)\n");
  212. exit(exitcode);
  213. }
  214. /********************************************************************************
  215. * Name: skip
  216. *
  217. * Description:
  218. *
  219. ********************************************************************************/
  220. static int skip(int lineno)
  221. {
  222. int i;
  223. for (i = 0; i < g_rangenumber; i++)
  224. {
  225. if (lineno >= g_rangestart[i] && lineno < g_rangestart[i] +
  226. g_rangecount[i])
  227. {
  228. return 0;
  229. }
  230. }
  231. return g_rangenumber != 0;
  232. }
  233. /********************************************************************************
  234. * Name: message
  235. *
  236. * Description:
  237. *
  238. ********************************************************************************/
  239. static int message(enum class_e class, const char *text, int lineno, int ndx)
  240. {
  241. FILE *out = stdout;
  242. if (skip(lineno))
  243. {
  244. return g_status;
  245. }
  246. if (class > INFO)
  247. {
  248. out = stderr;
  249. g_status |= 1;
  250. }
  251. if (g_verbose == 2)
  252. {
  253. if (lineno == -1 && ndx == -1)
  254. {
  255. fprintf(out, "%s: %s: %s\n", g_file_name, class_text[class], text);
  256. }
  257. else
  258. {
  259. fprintf(out, "%s:%d:%d: %s: %s\n", g_file_name, lineno, ndx,
  260. class_text[class], text);
  261. }
  262. }
  263. return g_status;
  264. }
  265. /********************************************************************************
  266. * Name: check_spaces_left
  267. *
  268. * Description:
  269. *
  270. ********************************************************************************/
  271. static void check_spaces_left(char *line, int lineno, int ndx)
  272. {
  273. /* Unary operator should generally be preceded by a space but make also
  274. * follow a left parenthesis at the beginning of a parenthetical list or
  275. * expression or follow a right parentheses in the case of a cast.
  276. */
  277. if (ndx-- > 0 && line[ndx] != ' ' && line[ndx] != '(' && line[ndx] != ')')
  278. {
  279. ERROR("Operator/assignment must be preceded with whitespace",
  280. lineno, ndx);
  281. }
  282. }
  283. /********************************************************************************
  284. * Name: check_spaces_leftright
  285. *
  286. * Description:
  287. *
  288. ********************************************************************************/
  289. static void check_spaces_leftright(char *line, int lineno, int ndx1, int ndx2)
  290. {
  291. if (ndx1 > 0 && line[ndx1 - 1] != ' ')
  292. {
  293. ERROR("Operator/assignment must be preceded with whitespace",
  294. lineno, ndx1);
  295. }
  296. if (line[ndx2 + 1] != '\0' && line[ndx2 + 1] != '\n' && line[ndx2 + 1] != ' ')
  297. {
  298. ERROR("Operator/assignment must be followed with whitespace",
  299. lineno, ndx2);
  300. }
  301. }
  302. /********************************************************************************
  303. * Name: block_comment_width
  304. *
  305. * Description:
  306. * Get the width of a block comment
  307. *
  308. ********************************************************************************/
  309. static int block_comment_width(char *line)
  310. {
  311. int b;
  312. int e;
  313. int n;
  314. /* Skip over any leading whitespace on the line */
  315. for (b = 0; isspace(line[b]); b++)
  316. {
  317. }
  318. /* Skip over any trailing whitespace at the end of the line */
  319. for (e = strlen(line) - 1; e >= 0 && isspace(line[e]); e--)
  320. {
  321. }
  322. /* Number of characters on the line */
  323. n = e - b + 1;
  324. if (n < 4)
  325. {
  326. return 0;
  327. }
  328. /* The first line of a block comment starts with "[slash]***" and ends with
  329. * "***"
  330. */
  331. if (strncmp(&line[b], "/***", 4) == 0 &&
  332. strncmp(&line[e - 2], "***", 3) == 0)
  333. {
  334. /* Return the the length of the line up to the final '*' */
  335. return e + 1;
  336. }
  337. /* The last line of a block begins with whitespace then "***" and ends
  338. * with "***[slash]"
  339. */
  340. if (strncmp(&line[b], "***", 3) == 0 &&
  341. strncmp(&line[e - 3], "***/", 4) == 0)
  342. {
  343. /* Return the the length of the line up to the final '*' */
  344. return e;
  345. }
  346. /* But there is also a special single line comment that begins with "[slash]* "
  347. * and ends with "***[slash]"
  348. */
  349. if (strncmp(&line[b], "/*", 2) == 0 &&
  350. strncmp(&line[e - 3], "***/", 4) == 0)
  351. {
  352. /* Return the the length of the line up to the final '*' */
  353. return e;
  354. }
  355. /* Return zero if the line is not the first or last line of a block
  356. * comment.
  357. */
  358. return 0;
  359. }
  360. /********************************************************************************
  361. * Name: get_line_width
  362. *
  363. * Description:
  364. * Get the maximum line width by examining the width of the block comments.
  365. *
  366. ********************************************************************************/
  367. static int get_line_width(FILE *instream)
  368. {
  369. char line[LINE_SIZE]; /* The current line being examined */
  370. int max = 0;
  371. int min = INT_MAX;
  372. int lineno = 0;
  373. int lineno_max = 0;
  374. int lineno_min = 0;
  375. int len;
  376. while (fgets(line, LINE_SIZE, instream))
  377. {
  378. lineno++;
  379. len = block_comment_width(line);
  380. if (len > 0)
  381. {
  382. if (len > max)
  383. {
  384. max = len;
  385. lineno_max = lineno;
  386. }
  387. if (len < min)
  388. {
  389. min = len;
  390. lineno_min = lineno;
  391. }
  392. }
  393. }
  394. if (max < min)
  395. {
  396. ERRORFL("No block comments found", g_file_name);
  397. return DEFAULT_WIDTH;
  398. }
  399. else if (max != min)
  400. {
  401. ERROR("Block comments have different lengths", lineno_max, max);
  402. ERROR("Block comments have different lengths", lineno_min, min);
  403. return DEFAULT_WIDTH;
  404. }
  405. return min;
  406. }
  407. /********************************************************************************
  408. * Name: check_section_header
  409. *
  410. * Description:
  411. * Check if the current line holds a section header
  412. *
  413. ********************************************************************************/
  414. static bool check_section_header(const char *line, int lineno)
  415. {
  416. int i;
  417. /* Search g_section_info[] to find a matching section header line */
  418. for (i = FIRST_SECTION; i <= LAST_SECTION; i++)
  419. {
  420. if (strcmp(line, g_section_info[i].name) == 0)
  421. {
  422. g_section = (enum section_s)i;
  423. /* Verify that this section is appropriate for this file type */
  424. if ((g_file_type & g_section_info[i].ftype) == 0)
  425. {
  426. ERROR("Invalid section for this file type", lineno, 3);
  427. }
  428. return true;
  429. }
  430. }
  431. return false;
  432. }
  433. /********************************************************************************
  434. * Name: white_prefix
  435. *
  436. * Description:
  437. * Return true if the identifier string begins with a white-listed prefix
  438. *
  439. ********************************************************************************/
  440. static bool white_list(const char *ident, int lineno)
  441. {
  442. const char **pptr;
  443. const char *str;
  444. for (pptr = g_white_prefix;
  445. (str = *pptr) != NULL;
  446. pptr++)
  447. {
  448. if (strncmp(ident, str, strlen(str)) == 0)
  449. {
  450. return true;
  451. }
  452. }
  453. for (pptr = g_white_list;
  454. (str = *pptr) != NULL;
  455. pptr++)
  456. {
  457. size_t len = strlen(str);
  458. if (strncmp(ident, str, len) == 0 &&
  459. isalnum(ident[len]) == 0)
  460. {
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. /********************************************************************************
  467. * Public Functions
  468. ********************************************************************************/
  469. int main(int argc, char **argv, char **envp)
  470. {
  471. FILE *instream; /* File input stream */
  472. char line[LINE_SIZE]; /* The current line being examined */
  473. char buffer[100]; /* Localy format error strings */
  474. char *lptr; /* Temporary pointer into line[] */
  475. char *ext; /* Temporary file extension */
  476. bool btabs; /* True: TAB characters found on the line */
  477. bool bcrs; /* True: Carriage return found on the line */
  478. bool bfunctions; /* True: In private or public functions */
  479. bool bstatm; /* True: This line is beginning of a statement */
  480. bool bfor; /* True: This line is beginning of a 'for' statement */
  481. bool bswitch; /* True: Within a switch statement */
  482. bool bstring; /* True: Within a string */
  483. bool bquote; /* True: Backslash quoted character next */
  484. bool bblank; /* Used to verify block comment terminator */
  485. bool bexternc; /* True: Within 'extern "C"' */
  486. enum pptype_e ppline; /* > 0: The next line the continuation of a
  487. * pre-processor command */
  488. int rhcomment; /* Indentation of Comment to the right of code
  489. * (-1 -> don't check position) */
  490. int prevrhcmt; /* Indentation of previous Comment to the right
  491. * of code (-1 -> don't check position) */
  492. int lineno; /* Current line number */
  493. int indent; /* Indentation level */
  494. int ncomment; /* Comment nesting level on this line */
  495. int prevncomment; /* Comment nesting level on the previous line */
  496. int bnest; /* Brace nesting level on this line */
  497. int prevbnest; /* Brace nesting level on the previous line */
  498. int dnest; /* Data declaration nesting level on this line */
  499. int prevdnest; /* Data declaration nesting level on the previous line */
  500. int pnest; /* Parenthesis nesting level on this line */
  501. int ppifnest; /* #if nesting level on this line */
  502. int inasm; /* > 0: Within #ifdef __ASSEMBLY__ */
  503. int comment_lineno; /* Line on which the last comment was closed */
  504. int blank_lineno; /* Line number of the last blank line */
  505. int noblank_lineno; /* A blank line is not needed after this line */
  506. int lbrace_lineno; /* Line number of last left brace */
  507. int rbrace_lineno; /* Last line containing a right brace */
  508. int externc_lineno; /* Last line where 'extern "C"' declared */
  509. int linelen; /* Length of the line */
  510. int excess;
  511. int n;
  512. int i;
  513. int c;
  514. excess = 0;
  515. while ((c = getopt(argc, argv, ":hv:gm:r:")) != -1)
  516. {
  517. switch (c)
  518. {
  519. case 'm':
  520. excess = atoi(optarg);
  521. if (excess < 1)
  522. {
  523. show_usage(argv[0], 1, "Bad value for <excess>.");
  524. excess = 0;
  525. }
  526. break;
  527. case 'v':
  528. g_verbose = atoi(optarg);
  529. if (g_verbose < 0 || g_verbose > 2)
  530. {
  531. show_usage(argv[0], 1, "Bad value for <level>.");
  532. }
  533. break;
  534. case 'r':
  535. g_rangestart[g_rangenumber] = atoi(strtok(optarg, ","));
  536. g_rangecount[g_rangenumber++] = atoi(strtok(NULL, ","));
  537. break;
  538. case 'h':
  539. show_usage(argv[0], 0, NULL);
  540. break;
  541. case ':':
  542. show_usage(argv[0], 1, "Missing argument.");
  543. break;
  544. case '?':
  545. show_usage(argv[0], 1, "Unrecognized option.");
  546. break;
  547. default:
  548. show_usage(argv[0], 0, NULL);
  549. break;
  550. }
  551. }
  552. if (optind < argc - 1 || argv[optind] == NULL)
  553. {
  554. show_usage(argv[0], 1, "No file name given.");
  555. }
  556. g_file_name = argv[optind];
  557. /* Are we parsing a header file? */
  558. ext = strrchr(g_file_name, '.');
  559. if (ext == 0)
  560. {
  561. }
  562. else if (strcmp(ext, ".h") == 0)
  563. {
  564. g_file_type = C_HEADER;
  565. }
  566. else if (strcmp(ext, ".c") == 0)
  567. {
  568. g_file_type = C_SOURCE;
  569. }
  570. if (g_file_type == UNKNOWN)
  571. {
  572. return 0;
  573. }
  574. instream = fopen(g_file_name, "r");
  575. if (!instream)
  576. {
  577. FATALFL("Failed to open", g_file_name);
  578. return 1;
  579. }
  580. /* Determine the line width */
  581. g_maxline = get_line_width(instream) + excess;
  582. rewind(instream);
  583. btabs = false; /* True: TAB characters found on the line */
  584. bcrs = false; /* True: Carriage return found on the line */
  585. bfunctions = false; /* True: In private or public functions */
  586. bswitch = false; /* True: Within a switch statement */
  587. bstring = false; /* True: Within a string */
  588. bexternc = false; /* True: Within 'extern "C"' */
  589. ppline = PPLINE_NONE; /* > 0: The next line the continuation of a
  590. * pre-processor command */
  591. rhcomment = 0; /* Indentation of Comment to the right of code
  592. * (-1 -> don't check position) */
  593. prevrhcmt = 0; /* Indentation of previous Comment to the right
  594. * of code (-1 -> don't check position) */
  595. lineno = 0; /* Current line number */
  596. ncomment = 0; /* Comment nesting level on this line */
  597. bnest = 0; /* Brace nesting level on this line */
  598. dnest = 0; /* Data declaration nesting level on this line */
  599. pnest = 0; /* Parenthesis nesting level on this line */
  600. ppifnest = 0; /* #if nesting level on this line */
  601. inasm = 0; /* > 0: Within #ifdef __ASSEMBLY__ */
  602. comment_lineno = -1; /* Line on which the last comment was closed */
  603. blank_lineno = -1; /* Line number of the last blank line */
  604. noblank_lineno = -1; /* A blank line is not needed after this line */
  605. lbrace_lineno = -1; /* Line number of last left brace */
  606. rbrace_lineno = -1; /* Last line containing a right brace */
  607. externc_lineno = -1; /* Last line where 'extern "C"' declared */
  608. /* Process each line in the input stream */
  609. while (fgets(line, LINE_SIZE, instream))
  610. {
  611. lineno++;
  612. indent = 0;
  613. prevbnest = bnest; /* Brace nesting level on the previous line */
  614. prevdnest = dnest; /* Data declaration nesting level on the
  615. * previous line */
  616. prevncomment = ncomment; /* Comment nesting level on the previous line */
  617. bstatm = false; /* True: This line is beginning of a
  618. * statement */
  619. bfor = false; /* REVISIT: Implies for() is all on one line */
  620. /* If we are not in a comment, then this certainly is not a right-hand
  621. * comment.
  622. */
  623. prevrhcmt = rhcomment;
  624. if (ncomment <= 0)
  625. {
  626. rhcomment = 0;
  627. }
  628. /* Check for a blank line */
  629. for (n = 0; line[n] != '\n' && isspace((int)line[n]); n++)
  630. {
  631. }
  632. if (line[n] == '\n')
  633. {
  634. if (n > 0)
  635. {
  636. ERROR("Blank line contains whitespace", lineno, 1);
  637. }
  638. if (lineno == 1)
  639. {
  640. ERROR("File begins with a blank line", 1, 1);
  641. }
  642. else if (lineno == blank_lineno + 1)
  643. {
  644. ERROR("Too many blank lines", lineno, 1);
  645. }
  646. else if (lineno == lbrace_lineno + 1)
  647. {
  648. ERROR("Blank line follows left brace", lineno, 1);
  649. }
  650. blank_lineno = lineno;
  651. continue;
  652. }
  653. else /* This line is non-blank */
  654. {
  655. /* Check for a missing blank line after a comment */
  656. if (lineno == comment_lineno + 1)
  657. {
  658. /* No blank line should be present if the current line contains
  659. * a right brace, a pre-processor line, the start of another
  660. * comment.
  661. *
  662. * REVISIT: Generates a false alarm if the current line is also
  663. * a comment. Generally it is acceptable for one comment to
  664. * follow another with no space separation.
  665. *
  666. * REVISIT: prevrhcmt is tested to case the preceding line
  667. * contained comments to the right of the code. In such cases,
  668. * the comments are normally aligned and do not follow normal
  669. * indentation rules. However, this code will generate a false
  670. * alarm if the comments are aligned to the right BUT the
  671. * preceding line has no comment.
  672. */
  673. if (line[n] != '}' && line[n] != '#' && prevrhcmt == 0)
  674. {
  675. ERROR("Missing blank line after comment", comment_lineno,
  676. 1);
  677. }
  678. }
  679. /* Files must begin with a comment (the file header).
  680. * REVISIT: Logically, this belongs in the STEP 2 operations
  681. * below.
  682. */
  683. if (lineno == 1 && (line[n] != '/' || line[n + 1] != '*'))
  684. {
  685. ERROR("Missing file header comment block", lineno, 1);
  686. }
  687. /* Check for a blank line following a right brace */
  688. if (bfunctions && lineno == rbrace_lineno + 1)
  689. {
  690. /* Check if this line contains a right brace. A right brace
  691. * must be followed by 'else', 'while', 'break', a blank line,
  692. * another right brace, or a pre-processor directive like #endif
  693. */
  694. if (dnest == 0 &&
  695. strchr(line, '}') == NULL && line[n] != '#' &&
  696. strncmp(&line[n], "else", 4) != 0 &&
  697. strncmp(&line[n], "while", 5) != 0 &&
  698. strncmp(&line[n], "break", 5) != 0)
  699. {
  700. ERROR("Right brace must be followed by a blank line",
  701. rbrace_lineno, n + 1);
  702. }
  703. /* If the right brace is followed by a pre-processor command
  704. * like #endif (but not #else or #elif), then set the right
  705. * brace line number to the line number of the pre-processor
  706. * command (it then must be followed by a blank line)
  707. */
  708. if (line[n] == '#')
  709. {
  710. int ii;
  711. for (ii = n + 1; line[ii] != '\0' && isspace(line[ii]); ii++)
  712. {
  713. }
  714. if (strncmp(&line[ii], "else", 4) != 0 &&
  715. strncmp(&line[ii], "elif", 4) != 0)
  716. {
  717. rbrace_lineno = lineno;
  718. }
  719. }
  720. }
  721. }
  722. /* STEP 1: Find the indentation level and the start of real stuff on
  723. * the line.
  724. */
  725. for (n = 0; line[n] != '\n' && isspace((int)line[n]); n++)
  726. {
  727. switch (line[n])
  728. {
  729. case ' ':
  730. {
  731. indent++;
  732. }
  733. break;
  734. case '\t':
  735. {
  736. if (!btabs)
  737. {
  738. ERROR("TABs found. First detected", lineno, n);
  739. btabs = true;
  740. }
  741. indent = (indent + 4) & ~3;
  742. }
  743. break;
  744. case '\r':
  745. {
  746. if (!bcrs)
  747. {
  748. ERROR("Carriage returns found. "
  749. "First detected", lineno, n);
  750. bcrs = true;
  751. }
  752. }
  753. break;
  754. default:
  755. {
  756. snprintf(buffer, sizeof(buffer),
  757. "Unexpected white space character %02x found",
  758. line[n]);
  759. ERROR(buffer, lineno, n);
  760. }
  761. break;
  762. }
  763. }
  764. /* STEP 2: Detect some certain start of line conditions */
  765. /* Skip over pre-processor lines (or continuations of pre-processor
  766. * lines as indicated by ppline)
  767. */
  768. if (line[indent] == '#' || ppline != PPLINE_NONE)
  769. {
  770. int len;
  771. int ii;
  772. /* Suppress error for comment following conditional compilation */
  773. noblank_lineno = lineno;
  774. /* Check pre-processor commands if this is not a continuation
  775. * line.
  776. */
  777. ii = indent + 1;
  778. if (ppline == PPLINE_NONE)
  779. {
  780. /* Skip to the pre-processor command following the '#' */
  781. while (line[ii] != '\0' && isspace(line[ii]))
  782. {
  783. ii++;
  784. }
  785. if (line[ii] != '\0')
  786. {
  787. /* Make sure that pre-processor definitions are all in
  788. * the pre-processor definitions section.
  789. */
  790. ppline = PPLINE_OTHER;
  791. if (strncmp(&line[ii], "define", 6) == 0)
  792. {
  793. ppline = PPLINE_DEFINE;
  794. if (g_section != PRE_PROCESSOR_DEFINITIONS)
  795. {
  796. /* A complication is the header files always have
  797. * the idempotence guard definitions before the
  798. * "Pre-processor Definitions section".
  799. */
  800. if (g_section == NO_SECTION &&
  801. g_file_type != C_HEADER)
  802. {
  803. /* Only a warning because there is some usage
  804. * of define outside the Pre-processor
  805. * Definitions section which is justifiable.
  806. * Should be manually checked.
  807. */
  808. WARN("#define outside of 'Pre-processor "
  809. "Definitions' section",
  810. lineno, ii);
  811. }
  812. }
  813. }
  814. /* Make sure that files are included only in the Included
  815. * Files section.
  816. */
  817. else if (strncmp(&line[ii], "include", 7) == 0)
  818. {
  819. if (g_section != INCLUDED_FILES)
  820. {
  821. /* Only a warning because there is some usage of
  822. * include outside the Included Files section
  823. * which may be is justifiable. Should be
  824. * manually checked.
  825. */
  826. WARN("#include outside of 'Included Files' "
  827. "section",
  828. lineno, ii);
  829. }
  830. }
  831. else if (strncmp(&line[ii], "if", 2) == 0)
  832. {
  833. ppifnest++;
  834. ppline = PPLINE_IF;
  835. ii += 2;
  836. }
  837. else if (strncmp(&line[ii], "elif", 4) == 0)
  838. {
  839. if (ppifnest == inasm)
  840. {
  841. inasm = 0;
  842. }
  843. ppline = PPLINE_ELIF;
  844. ii += 4;
  845. }
  846. else if (strncmp(&line[ii], "else", 4) == 0)
  847. {
  848. if (ppifnest == inasm)
  849. {
  850. inasm = 0;
  851. }
  852. ppline = PPLINE_ELSE;
  853. }
  854. else if (strncmp(&line[ii], "endif", 4) == 0)
  855. {
  856. if (ppifnest == inasm)
  857. {
  858. inasm = 0;
  859. }
  860. ppifnest--;
  861. ppline = PPLINE_ENDIF;
  862. }
  863. }
  864. }
  865. if (ppline == PPLINE_IF || ppline == PPLINE_ELIF)
  866. {
  867. int bdef = 0;
  868. if (strncmp(&line[ii], "def", 3) == 0)
  869. {
  870. bdef = 1;
  871. ii += 3;
  872. }
  873. else
  874. {
  875. while (line[ii] != '\0' && isspace(line[ii]))
  876. {
  877. ii++;
  878. }
  879. if (strncmp(&line[ii], "defined", 7) == 0)
  880. {
  881. bdef = 1;
  882. ii += 7;
  883. }
  884. }
  885. if (bdef)
  886. {
  887. while (line[ii] != '\0' &&
  888. (isspace(line[ii]) || line[ii] == '('))
  889. {
  890. ii++;
  891. }
  892. if (strncmp(&line[ii], "__ASSEMBLY__", 12) == 0)
  893. {
  894. inasm = ppifnest;
  895. }
  896. }
  897. }
  898. /* Check if the next line will be a continuation of the pre-
  899. * processor command.
  900. */
  901. len = strlen(&line[indent]) + indent - 1;
  902. if (line[len] == '\n')
  903. {
  904. len--;
  905. }
  906. /* Propagate rhcomment over preprocessor lines Issue #120 */
  907. if (prevrhcmt != 0)
  908. {
  909. /* Don't check position */
  910. rhcomment = -1;
  911. }
  912. lptr = strstr(line, "/*");
  913. if (lptr != NULL)
  914. {
  915. n = lptr - &line[0];
  916. if (line[n + 2] == '\n')
  917. {
  918. ERROR("C comment opening on separate line", lineno, n);
  919. }
  920. else if (!isspace((int)line[n + 2]) && line[n + 2] != '*')
  921. {
  922. ERROR("Missing space after opening C comment", lineno, n);
  923. }
  924. if (strstr(lptr, "*/") == NULL)
  925. {
  926. /* Increment the count of nested comments */
  927. ncomment++;
  928. }
  929. if (ppline == PPLINE_DEFINE)
  930. {
  931. rhcomment = n;
  932. if (prevrhcmt > 0 && n != prevrhcmt)
  933. {
  934. rhcomment = prevrhcmt;
  935. WARN("Wrong column position of comment right of code",
  936. lineno, n);
  937. }
  938. }
  939. else
  940. {
  941. /* Signal rhcomment, but ignore position */
  942. rhcomment = -1;
  943. if (ncomment > 0 &&
  944. (ppline == PPLINE_IF ||
  945. ppline == PPLINE_ELSE ||
  946. ppline == PPLINE_ELIF))
  947. {
  948. /* in #if... and #el... */
  949. ERROR("No multiline comment right of code allowed here",
  950. lineno, n);
  951. }
  952. }
  953. }
  954. if (line[len] != '\\' || ncomment > 0)
  955. {
  956. ppline = PPLINE_NONE;
  957. }
  958. continue;
  959. }
  960. /* Check for a single line comment */
  961. linelen = strlen(line);
  962. if (linelen >= 5) /* Minimum is slash, star, star, slash, newline */
  963. {
  964. lptr = strstr(line, "*/");
  965. if (line[indent] == '/' && line[indent + 1] == '*' &&
  966. lptr - line == linelen - 3)
  967. {
  968. /* If preceding comments were to the right of code, then we can
  969. * assume that there is a columnar alignment of columns that do
  970. * no follow the usual alignment. So the rhcomment flag
  971. * should propagate.
  972. */
  973. rhcomment = prevrhcmt;
  974. /* Check if there should be a blank line before the comment */
  975. if (lineno > 1 &&
  976. comment_lineno != lineno - 1 &&
  977. blank_lineno != lineno - 1 &&
  978. noblank_lineno != lineno - 1 &&
  979. rhcomment == 0)
  980. {
  981. /* TODO: This generates a false alarm if preceded
  982. * by a label.
  983. */
  984. ERROR("Missing blank line before comment found", lineno, 1);
  985. }
  986. /* 'comment_lineno 'holds the line number of the last closing
  987. * comment. It is used only to verify that the comment is
  988. * followed by a blank line.
  989. */
  990. comment_lineno = lineno;
  991. }
  992. }
  993. /* Check for the comment block indicating the beginning of a new file
  994. * section.
  995. */
  996. if (check_section_header(line, lineno))
  997. {
  998. if (g_section == PRIVATE_FUNCTIONS || g_section == PUBLIC_FUNCTIONS)
  999. {
  1000. bfunctions = true; /* Latched */
  1001. }
  1002. }
  1003. /* Check for some kind of declaration.
  1004. * REVISIT: The following logic fails for any non-standard types.
  1005. * REVISIT: Terminator after keyword might not be a space. Might be
  1006. * a newline, for example. struct and unions are often unnamed, for
  1007. * example.
  1008. */
  1009. else if (inasm == 0)
  1010. {
  1011. if (strncmp(&line[indent], "auto ", 5) == 0 ||
  1012. strncmp(&line[indent], "bool ", 5) == 0 ||
  1013. strncmp(&line[indent], "char ", 5) == 0 ||
  1014. strncmp(&line[indent], "CODE ", 5) == 0 ||
  1015. strncmp(&line[indent], "const ", 6) == 0 ||
  1016. strncmp(&line[indent], "double ", 7) == 0 ||
  1017. strncmp(&line[indent], "struct ", 7) == 0 ||
  1018. strncmp(&line[indent], "struct\n", 7) == 0 || /* May be unnamed */
  1019. strncmp(&line[indent], "enum ", 5) == 0 ||
  1020. strncmp(&line[indent], "extern ", 7) == 0 ||
  1021. strncmp(&line[indent], "EXTERN ", 7) == 0 ||
  1022. strncmp(&line[indent], "FAR ", 4) == 0 ||
  1023. strncmp(&line[indent], "float ", 6) == 0 ||
  1024. strncmp(&line[indent], "int ", 4) == 0 ||
  1025. strncmp(&line[indent], "int16_t ", 8) == 0 ||
  1026. strncmp(&line[indent], "int32_t ", 8) == 0 ||
  1027. strncmp(&line[indent], "long ", 5) == 0 ||
  1028. strncmp(&line[indent], "off_t ", 6) == 0 ||
  1029. strncmp(&line[indent], "register ", 9) == 0 ||
  1030. strncmp(&line[indent], "short ", 6) == 0 ||
  1031. strncmp(&line[indent], "signed ", 7) == 0 ||
  1032. strncmp(&line[indent], "size_t ", 7) == 0 ||
  1033. strncmp(&line[indent], "ssize_t ", 8) == 0 ||
  1034. strncmp(&line[indent], "static ", 7) == 0 ||
  1035. strncmp(&line[indent], "time_t ", 7) == 0 ||
  1036. strncmp(&line[indent], "typedef ", 8) == 0 ||
  1037. strncmp(&line[indent], "uint8_t ", 8) == 0 ||
  1038. strncmp(&line[indent], "uint16_t ", 9) == 0 ||
  1039. strncmp(&line[indent], "uint32_t ", 9) == 0 ||
  1040. strncmp(&line[indent], "union ", 6) == 0 ||
  1041. strncmp(&line[indent], "union\n", 6) == 0 || /* May be unnamed */
  1042. strncmp(&line[indent], "unsigned ", 9) == 0 ||
  1043. strncmp(&line[indent], "void ", 5) == 0 ||
  1044. strncmp(&line[indent], "volatile ", 9) == 0)
  1045. {
  1046. /* Check if this is extern "C"; We don't typically indent
  1047. * following this.
  1048. */
  1049. if (strncmp(&line[indent], "extern \"C\"", 10) == 0)
  1050. {
  1051. externc_lineno = lineno;
  1052. }
  1053. /* bfunctions: True: Processing private or public functions.
  1054. * bnest: Brace nesting level on this line
  1055. * dnest: Data declaration nesting level on this line
  1056. */
  1057. /* REVISIT: Also picks up function return types */
  1058. /* REVISIT: Logic problem for nested data/function declarations */
  1059. if ((!bfunctions || bnest > 0) && dnest == 0)
  1060. {
  1061. dnest = 1;
  1062. }
  1063. /* Check for multiple definitions of variables on the line.
  1064. * Ignores declarations within parentheses which are probably
  1065. * formal parameters.
  1066. */
  1067. if (pnest == 0)
  1068. {
  1069. int tmppnest;
  1070. /* Note, we have not yet parsed each character on the line so
  1071. * a comma have have been be preceded by '(' on the same line.
  1072. * We will have parse up to any comma to see if that is the
  1073. * case.
  1074. */
  1075. for (i = indent, tmppnest = 0;
  1076. line[i] != '\n' && line[i] != '\0';
  1077. i++)
  1078. {
  1079. if (tmppnest == 0 && line[i] == ',')
  1080. {
  1081. ERROR("Multiple data definitions", lineno, i + 1);
  1082. break;
  1083. }
  1084. else if (line[i] == '(')
  1085. {
  1086. tmppnest++;
  1087. }
  1088. else if (line[i] == ')')
  1089. {
  1090. if (tmppnest < 1)
  1091. {
  1092. /* We should catch this later */
  1093. break;
  1094. }
  1095. tmppnest--;
  1096. }
  1097. else if (line[i] == ';')
  1098. {
  1099. /* Break out if the semicolon terminates the
  1100. * declaration is found. Avoids processing any
  1101. * righthand comments in most cases.
  1102. */
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. /* Check for a keyword indicating the beginning of a statement.
  1109. * REVISIT: This, obviously, will not detect statements that do not
  1110. * begin with a C keyword (such as assignment statements).
  1111. */
  1112. else if (strncmp(&line[indent], "break ", 6) == 0 ||
  1113. strncmp(&line[indent], "case ", 5) == 0 ||
  1114. #if 0 /* Part of switch */
  1115. strncmp(&line[indent], "case ", 5) == 0 ||
  1116. #endif
  1117. strncmp(&line[indent], "continue ", 9) == 0 ||
  1118. #if 0 /* Part of switch */
  1119. strncmp(&line[indent], "default ", 8) == 0 ||
  1120. #endif
  1121. strncmp(&line[indent], "do ", 3) == 0 ||
  1122. strncmp(&line[indent], "else ", 5) == 0 ||
  1123. strncmp(&line[indent], "goto ", 5) == 0 ||
  1124. strncmp(&line[indent], "if ", 3) == 0 ||
  1125. strncmp(&line[indent], "return ", 7) == 0 ||
  1126. #if 0 /* Doesn't follow pattern */
  1127. strncmp(&line[indent], "switch ", 7) == 0 ||
  1128. #endif
  1129. strncmp(&line[indent], "while ", 6) == 0)
  1130. {
  1131. bstatm = true;
  1132. }
  1133. /* Spacing works a little differently for and switch statements */
  1134. else if (strncmp(&line[indent], "for ", 4) == 0)
  1135. {
  1136. bfor = true;
  1137. bstatm = true;
  1138. }
  1139. else if (strncmp(&line[indent], "switch ", 7) == 0)
  1140. {
  1141. bswitch = true;
  1142. }
  1143. /* Also check for C keywords with missing white space */
  1144. else if (strncmp(&line[indent], "do(", 3) == 0 ||
  1145. strncmp(&line[indent], "if(", 3) == 0 ||
  1146. strncmp(&line[indent], "while(", 6) == 0)
  1147. {
  1148. ERROR("Missing whitespace after keyword", lineno, n);
  1149. bstatm = true;
  1150. }
  1151. else if (strncmp(&line[indent], "for(", 4) == 0)
  1152. {
  1153. ERROR("Missing whitespace after keyword", lineno, n);
  1154. bfor = true;
  1155. bstatm = true;
  1156. }
  1157. else if (strncmp(&line[indent], "switch(", 7) == 0)
  1158. {
  1159. ERROR("Missing whitespace after keyword", lineno, n);
  1160. bswitch = true;
  1161. }
  1162. }
  1163. /* STEP 3: Parse each character on the line */
  1164. bquote = false; /* True: Backslash quoted character next */
  1165. bblank = true; /* Used to verify block comment terminator */
  1166. for (; line[n] != '\n' && line[n] != '\0'; n++)
  1167. {
  1168. /* Report any use of non-standard white space characters */
  1169. if (isspace(line[n]))
  1170. {
  1171. if (line[n] == '\t')
  1172. {
  1173. if (!btabs)
  1174. {
  1175. ERROR("TABs found. First detected", lineno, n);
  1176. btabs = true;
  1177. }
  1178. }
  1179. else if (line[n] == '\r')
  1180. {
  1181. if (!bcrs)
  1182. {
  1183. ERROR("Carriage returns found. "
  1184. "First detected", lineno, n);
  1185. bcrs = true;
  1186. }
  1187. }
  1188. else if (line[n] != ' ')
  1189. {
  1190. snprintf(buffer, sizeof(buffer),
  1191. "Unexpected white space character %02x found",
  1192. line[n]);
  1193. ERROR(buffer, lineno, n);
  1194. }
  1195. }
  1196. /* Skip over identifiers */
  1197. if (ncomment == 0 && !bstring && (line[n] == '_' || isalpha(line[n])))
  1198. {
  1199. bool have_upper = false;
  1200. bool have_lower = false;
  1201. int ident_index = n;
  1202. /* Parse over the identifier. Check if it contains mixed upper-
  1203. * and lower-case characters.
  1204. */
  1205. do
  1206. {
  1207. have_upper |= isupper(line[n]);
  1208. /* The coding standard provides for some exceptions of lower
  1209. * case characters in pre-processor strings:
  1210. *
  1211. * IPv[4|6] as an IP version number
  1212. * ICMPv6 as an ICMP version number
  1213. * IGMPv2 as an IGMP version number
  1214. * [0-9]p[0-9] as a decimal point
  1215. * d[0-9] as a divisor
  1216. * Hz for frequencies (including KHz, MHz, etc.)
  1217. */
  1218. if (!have_lower && islower(line[n]))
  1219. {
  1220. switch (line[n])
  1221. {
  1222. /* A sequence containing 'v' may occur at the
  1223. * beginning of the identifier.
  1224. */
  1225. case 'v':
  1226. if (n > 1 &&
  1227. line[n - 2] == 'I' &&
  1228. line[n - 1] == 'P' &&
  1229. (line[n + 1] == '4' ||
  1230. line[n + 1] == '6'))
  1231. {
  1232. }
  1233. else if (n > 3 &&
  1234. line[n - 4] == 'I' &&
  1235. line[n - 3] == 'C' &&
  1236. line[n - 2] == 'M' &&
  1237. line[n - 1] == 'P' &&
  1238. line[n + 1] == '6')
  1239. {
  1240. }
  1241. else if (n > 3 &&
  1242. line[n - 4] == 'I' &&
  1243. line[n - 3] == 'G' &&
  1244. line[n - 2] == 'M' &&
  1245. line[n - 1] == 'P' &&
  1246. line[n + 1] == '2')
  1247. {
  1248. }
  1249. else
  1250. {
  1251. have_lower = true;
  1252. }
  1253. break;
  1254. /* Sequences containing 'p', 'd', or 'z' must have
  1255. * been preceded by upper case characters.
  1256. */
  1257. case 'p':
  1258. if (!have_upper || n < 1 ||
  1259. !isdigit(line[n - 1]) ||
  1260. !isdigit(line[n + 1]))
  1261. {
  1262. have_lower = true;
  1263. }
  1264. break;
  1265. case 'd':
  1266. if (!have_upper || !isdigit(line[n + 1]))
  1267. {
  1268. have_lower = true;
  1269. }
  1270. break;
  1271. case 'z':
  1272. if (!have_upper || n < 1 ||
  1273. line[n - 1] != 'H')
  1274. {
  1275. have_lower = true;
  1276. }
  1277. break;
  1278. break;
  1279. default:
  1280. have_lower = true;
  1281. break;
  1282. }
  1283. }
  1284. n++;
  1285. }
  1286. while (line[n] == '_' || isalnum(line[n]));
  1287. /* Check for mixed upper and lower case */
  1288. if (have_upper && have_lower)
  1289. {
  1290. /* Ignore symbols that begin with white-listed prefixes */
  1291. if (white_list(&line[ident_index], lineno))
  1292. {
  1293. /* No error */
  1294. }
  1295. /* Special case hex constants. These will look like
  1296. * identifiers starting with 'x' or 'X' but preceded
  1297. * with '0'
  1298. */
  1299. else if (ident_index < 1 ||
  1300. (line[ident_index] != 'x' &&
  1301. line[ident_index] != 'X') ||
  1302. line[ident_index - 1] != '0')
  1303. {
  1304. ERROR("Mixed case identifier found",
  1305. lineno, ident_index);
  1306. }
  1307. else if (have_upper)
  1308. {
  1309. ERROR("Upper case hex constant found",
  1310. lineno, ident_index);
  1311. }
  1312. }
  1313. /* Check if the identifier is the last thing on the line */
  1314. if (line[n] == '\n' || line[n] == '\0')
  1315. {
  1316. break;
  1317. }
  1318. }
  1319. /* Handle comments */
  1320. if (line[n] == '/' && !bstring)
  1321. {
  1322. /* Check for start of a C comment */
  1323. if (line[n + 1] == '*')
  1324. {
  1325. if (line[n + 2] == '\n')
  1326. {
  1327. ERROR("C comment opening on separate line", lineno, n);
  1328. }
  1329. else if (!isspace((int)line[n + 2]) && line[n + 2] != '*')
  1330. {
  1331. ERROR("Missing space after opening C comment", lineno, n);
  1332. }
  1333. /* Increment the count of nested comments */
  1334. ncomment++;
  1335. /* If there is anything to the left of the left brace, then
  1336. * this must be a comment to the right of code.
  1337. * Also if preceding comments were to the right of code, then
  1338. * we can assume that there is a columnar alignment of columns
  1339. * that do no follow the usual alignment. So the rhcomment
  1340. * flag should propagate.
  1341. */
  1342. if (prevrhcmt == 0)
  1343. {
  1344. if (n != indent)
  1345. {
  1346. rhcomment = n;
  1347. }
  1348. }
  1349. else
  1350. {
  1351. rhcomment = n;
  1352. if (prevrhcmt > 0 && n != prevrhcmt)
  1353. {
  1354. rhcomment = prevrhcmt;
  1355. if (n != indent)
  1356. {
  1357. WARN("Wrong column position of "
  1358. "comment right of code", lineno, n);
  1359. }
  1360. else
  1361. {
  1362. ERROR("Wrong column position or missing "
  1363. "blank line before comment", lineno, n);
  1364. }
  1365. }
  1366. }
  1367. n++;
  1368. continue;
  1369. }
  1370. /* Check for end of a C comment */
  1371. else if (n > 0 && line[n - 1] == '*')
  1372. {
  1373. if (n < 2)
  1374. {
  1375. ERROR("Closing C comment not indented", lineno, n);
  1376. }
  1377. else if (!isspace((int)line[n - 2]) && line[n - 2] != '*')
  1378. {
  1379. ERROR("Missing space before closing C comment", lineno,
  1380. n);
  1381. }
  1382. /* Check for block comments that are not on a separate line.
  1383. * This would be the case if we are we are within a comment
  1384. * that did not start on this line and the current line is
  1385. * not blank up to the point where the comment was closed.
  1386. */
  1387. if (prevncomment > 0 && !bblank && rhcomment == 0)
  1388. {
  1389. ERROR("Block comment terminator must be on a "
  1390. "separate line", lineno, n);
  1391. }
  1392. #if 0
  1393. /* REVISIT: Generates false alarms when portions of an
  1394. * expression are commented out within the expression.
  1395. */
  1396. if (line[n + 1] != '\n')
  1397. {
  1398. ERROR("Garbage on line after C comment", lineno, n);
  1399. }
  1400. #endif
  1401. /* Handle nested comments */
  1402. if (ncomment > 0)
  1403. {
  1404. /* Remember the line number of the line containing the
  1405. * closing of the outermost comment.
  1406. */
  1407. if (--ncomment == 0)
  1408. {
  1409. /* 'comment_lineno 'holds the line number of the
  1410. * last closing comment. It is used only to
  1411. * verify that the comment is followed by a blank
  1412. * line.
  1413. */
  1414. comment_lineno = lineno;
  1415. /* Note that rhcomment must persist to support a
  1416. * later test for comment alignment. We will fix
  1417. * that at the top of the loop when ncomment == 0.
  1418. */
  1419. }
  1420. }
  1421. else
  1422. {
  1423. /* Note that rhcomment must persist to support a later
  1424. * test for comment alignment. We will will fix that
  1425. * at the top of the loop when ncomment == 0.
  1426. */
  1427. ncomment = 0;
  1428. ERROR("Closing without opening comment", lineno, n);
  1429. }
  1430. n++;
  1431. continue;
  1432. }
  1433. /* Check for C++ style comments */
  1434. else if (line[n + 1] == '/')
  1435. {
  1436. /* Check for "http://" or "https://" */
  1437. if ((n < 5 || strncmp(&line[n - 5], "http://", 7) != 0) &&
  1438. (n < 6 || strncmp(&line[n - 6], "https://", 8) != 0))
  1439. {
  1440. ERROR("C++ style comment", lineno, n);
  1441. n++;
  1442. continue;
  1443. }
  1444. }
  1445. }
  1446. /* Check if the line is blank so far. This is only used to
  1447. * to verify the the closing of a block comment is on a separate
  1448. * line. So we also need to treat '*' as a 'blank'.
  1449. */
  1450. if (!isblank(line[n]) && line[n] != '*')
  1451. {
  1452. bblank = false;
  1453. }
  1454. /* Check for a string... ignore if we are in the middle of a
  1455. * comment.
  1456. */
  1457. if (ncomment == 0)
  1458. {
  1459. /* Backslash quoted character */
  1460. if (line[n] == '\\')
  1461. {
  1462. bquote = true;
  1463. n++;
  1464. }
  1465. /* Check for quoted characters: \" in string */
  1466. if (line[n] == '"' && !bquote)
  1467. {
  1468. bstring = !bstring;
  1469. }
  1470. bquote = false;
  1471. }
  1472. /* The rest of the line is only examined of we are not in a comment,
  1473. * in a string or in assembly.
  1474. *
  1475. * REVISIT: Should still check for whitespace at the end of the
  1476. * line.
  1477. */
  1478. if (ncomment == 0 && !bstring && inasm == 0)
  1479. {
  1480. switch (line[n])
  1481. {
  1482. /* Handle logic nested with curly braces */
  1483. case '{':
  1484. {
  1485. if (n > indent)
  1486. {
  1487. /* REVISIT: dnest is always > 0 here if bfunctions ==
  1488. * false.
  1489. */
  1490. if (dnest == 0 || !bfunctions || lineno == rbrace_lineno)
  1491. {
  1492. ERROR("Left bracket not on separate line", lineno,
  1493. n);
  1494. }
  1495. }
  1496. else if (line[n + 1] != '\n')
  1497. {
  1498. if (dnest == 0)
  1499. {
  1500. ERROR("Garbage follows left bracket", lineno, n);
  1501. }
  1502. }
  1503. bnest++;
  1504. if (dnest > 0)
  1505. {
  1506. dnest++;
  1507. }
  1508. /* Check if we are within 'extern "C"', we don't
  1509. * normally indent in that case because the 'extern "C"'
  1510. * is conditioned on __cplusplus.
  1511. */
  1512. if (lineno == externc_lineno ||
  1513. lineno - 1 == externc_lineno)
  1514. {
  1515. bexternc = true;
  1516. }
  1517. /* Suppress error for comment following a left brace */
  1518. noblank_lineno = lineno;
  1519. lbrace_lineno = lineno;
  1520. }
  1521. break;
  1522. case '}':
  1523. {
  1524. /* Decrement the brace nesting level */
  1525. if (bnest < 1)
  1526. {
  1527. ERROR("Unmatched right brace", lineno, n);
  1528. }
  1529. else
  1530. {
  1531. bnest--;
  1532. if (bnest < 1)
  1533. {
  1534. bnest = 0;
  1535. bswitch = false;
  1536. }
  1537. }
  1538. /* Decrement the declaration nesting level */
  1539. if (dnest < 3)
  1540. {
  1541. dnest = 0;
  1542. bexternc = false;
  1543. }
  1544. else
  1545. {
  1546. dnest--;
  1547. }
  1548. /* The right brace should be on a separate line */
  1549. if (n > indent)
  1550. {
  1551. if (dnest == 0)
  1552. {
  1553. ERROR("Right bracket not on separate line",
  1554. lineno, n);
  1555. }
  1556. }
  1557. /* Check for garbage following the left brace */
  1558. if (line[n + 1] != '\n' &&
  1559. line[n + 1] != ',' &&
  1560. line[n + 1] != ';')
  1561. {
  1562. int sndx = n + 1;
  1563. bool whitespace = false;
  1564. /* Skip over spaces */
  1565. while (line[sndx] == ' ')
  1566. {
  1567. sndx++;
  1568. }
  1569. /* One possibility is that the right bracket is
  1570. * followed by an identifier then a semi-colon.
  1571. * Comma is possible to but would be a case of
  1572. * multiple declaration of multiple instances.
  1573. */
  1574. if (line[sndx] == '_' || isalpha(line[sndx]))
  1575. {
  1576. int endx = sndx;
  1577. /* Skip to the end of the identifier. Checking
  1578. * for mixed case identifiers will be done
  1579. * elsewhere.
  1580. */
  1581. while (line[endx] == '_' ||
  1582. isalnum(line[endx]))
  1583. {
  1584. endx++;
  1585. }
  1586. /* Skip over spaces */
  1587. while (line[endx] == ' ')
  1588. {
  1589. whitespace = true;
  1590. endx++;
  1591. }
  1592. /* Handle according to what comes after the
  1593. * identifier.
  1594. */
  1595. if (strncmp(&line[sndx], "while", 5) == 0)
  1596. {
  1597. ERROR("'while' must be on a separate line",
  1598. lineno, sndx);
  1599. }
  1600. else if (line[endx] == ',')
  1601. {
  1602. ERROR("Multiple data definitions on line",
  1603. lineno, endx);
  1604. }
  1605. else if (line[endx] == ';')
  1606. {
  1607. if (whitespace)
  1608. {
  1609. ERROR("Space precedes semi-colon",
  1610. lineno, endx);
  1611. }
  1612. }
  1613. else if (line[endx] == '=')
  1614. {
  1615. /* There's a struct initialization following */
  1616. check_spaces_leftright(line, lineno, endx, endx);
  1617. dnest = 1;
  1618. }
  1619. else
  1620. {
  1621. ERROR("Garbage follows right bracket",
  1622. lineno, n);
  1623. }
  1624. }
  1625. else
  1626. {
  1627. ERROR("Garbage follows right bracket", lineno, n);
  1628. }
  1629. }
  1630. /* The right brace should not be preceded with a a blank
  1631. * line.
  1632. */
  1633. if (lineno == blank_lineno + 1)
  1634. {
  1635. ERROR("Blank line precedes right brace at line",
  1636. lineno, 1);
  1637. }
  1638. rbrace_lineno = lineno;
  1639. }
  1640. break;
  1641. /* Handle logic with parentheses */
  1642. case '(':
  1643. {
  1644. /* Increase the parenthetical nesting level */
  1645. pnest++;
  1646. /* Check for inappropriate space around parentheses */
  1647. if (line[n + 1] == ' ') /* && !bfor */
  1648. {
  1649. ERROR("Space follows left parenthesis", lineno, n);
  1650. }
  1651. }
  1652. break;
  1653. case ')':
  1654. {
  1655. /* Decrease the parenthetical nesting level */
  1656. if (pnest < 1)
  1657. {
  1658. ERROR("Unmatched right parentheses", lineno, n);
  1659. pnest = 0;
  1660. }
  1661. else
  1662. {
  1663. pnest--;
  1664. }
  1665. /* Allow ')' as first thing on the line (n == indent)
  1666. * Allow "for (xx; xx; )" (bfor == true)
  1667. */
  1668. if (n > 0 && n != indent && line[n - 1] == ' ' && !bfor)
  1669. {
  1670. ERROR("Space precedes right parenthesis", lineno, n);
  1671. }
  1672. }
  1673. break;
  1674. /* Check for inappropriate space around square brackets */
  1675. case '[':
  1676. {
  1677. if (line[n + 1] == ' ')
  1678. {
  1679. ERROR("Space follows left bracket", lineno, n);
  1680. }
  1681. }
  1682. break;
  1683. case ']':
  1684. {
  1685. if (n > 0 && line[n - 1] == ' ')
  1686. {
  1687. ERROR("Space precedes right bracket", lineno, n);
  1688. }
  1689. }
  1690. break;
  1691. /* Semi-colon may terminate a declaration */
  1692. case ';':
  1693. {
  1694. if (!isspace((int)line[n + 1]))
  1695. {
  1696. ERROR("Missing whitespace after semicolon", lineno, n);
  1697. }
  1698. /* Semicolon terminates a declaration/definition if there
  1699. * was no left curly brace (i.e., dnest is only 1).
  1700. */
  1701. if (dnest == 1)
  1702. {
  1703. dnest = 0;
  1704. }
  1705. }
  1706. break;
  1707. /* Semi-colon may terminate a declaration */
  1708. case ',':
  1709. {
  1710. if (!isspace((int)line[n + 1]))
  1711. {
  1712. ERROR("Missing whitespace after comma", lineno, n);
  1713. }
  1714. }
  1715. break;
  1716. /* Skip over character constants */
  1717. case '\'':
  1718. {
  1719. int endndx = n + 2;
  1720. if (line[n + 1] != '\n' && line[n + 1] != '\0')
  1721. {
  1722. if (line[n + 1] == '\\')
  1723. {
  1724. for (;
  1725. line[endndx] != '\n' &&
  1726. line[endndx] != '\0' &&
  1727. line[endndx] != '\'';
  1728. endndx++);
  1729. }
  1730. n = endndx + 1;
  1731. }
  1732. }
  1733. break;
  1734. /* Check for space around various operators */
  1735. case '-':
  1736. /* ->, -- */
  1737. if (line[n + 1] == '>' || line[n + 1] == '-')
  1738. {
  1739. n++;
  1740. }
  1741. /* -= */
  1742. else if (line[n + 1] == '=')
  1743. {
  1744. check_spaces_leftright(line, lineno, n, n + 1);
  1745. n++;
  1746. }
  1747. /* Scientific notation with a negative exponent (eg. 10e-10)
  1748. * REVISIT: This fails for cases where the variable name
  1749. * ends with 'e' preceded by a digit:
  1750. * a = abc1e-10;
  1751. * a = ABC1E-10;
  1752. */
  1753. else if ((line[n - 1] == 'e' || line[n - 1] == 'E') &&
  1754. isdigit(line[n + 1]) && isdigit(line[n - 2]))
  1755. {
  1756. n++;
  1757. }
  1758. else
  1759. {
  1760. /* '-' may function as a unary operator and snuggle
  1761. * on the left.
  1762. */
  1763. check_spaces_left(line, lineno, n);
  1764. }
  1765. break;
  1766. case '+':
  1767. /* ++ */
  1768. if (line[n + 1] == '+')
  1769. {
  1770. n++;
  1771. }
  1772. /* += */
  1773. else if (line[n + 1] == '=')
  1774. {
  1775. check_spaces_leftright(line, lineno, n, n + 1);
  1776. n++;
  1777. }
  1778. else
  1779. {
  1780. /* '+' may function as a unary operator and snuggle
  1781. * on the left.
  1782. */
  1783. check_spaces_left(line, lineno, n);
  1784. }
  1785. break;
  1786. case '&':
  1787. /* &<variable> OR &(<expression>) */
  1788. if (isalpha((int)line[n + 1]) || line[n + 1] == '_' ||
  1789. line[n + 1] == '(')
  1790. {
  1791. }
  1792. /* &&, &= */
  1793. else if (line[n + 1] == '=' || line[n + 1] == '&')
  1794. {
  1795. check_spaces_leftright(line, lineno, n, n + 1);
  1796. n++;
  1797. }
  1798. else
  1799. {
  1800. check_spaces_leftright(line, lineno, n, n);
  1801. }
  1802. break;
  1803. case '/':
  1804. /* C comment terminator */
  1805. if (line[n - 1] == '*')
  1806. {
  1807. n++;
  1808. }
  1809. /* C++-style comment */
  1810. else if (line[n + 1] == '/')
  1811. {
  1812. /* Check for "http://" or "https://" */
  1813. if ((n < 5 || strncmp(&line[n - 5], "http://", 7) != 0) &&
  1814. (n < 6 || strncmp(&line[n - 6], "https://", 8) != 0))
  1815. {
  1816. ERROR("C++ style comment on at %d:%d\n",
  1817. lineno, n);
  1818. }
  1819. n++;
  1820. }
  1821. /* /= */
  1822. else if (line[n + 1] == '=')
  1823. {
  1824. check_spaces_leftright(line, lineno, n, n + 1);
  1825. n++;
  1826. }
  1827. /* Division operator */
  1828. else
  1829. {
  1830. check_spaces_leftright(line, lineno, n, n);
  1831. }
  1832. break;
  1833. case '*':
  1834. /* *\/, ** */
  1835. if (line[n] == '*' &&
  1836. (line[n + 1] == '/' ||
  1837. line[n + 1] == '*'))
  1838. {
  1839. n++;
  1840. break;
  1841. }
  1842. /* *<variable>, *(<expression>) */
  1843. else if (isalpha((int)line[n + 1]) ||
  1844. line[n + 1] == '_' ||
  1845. line[n + 1] == '(')
  1846. {
  1847. break;
  1848. }
  1849. /* (<type> *) */
  1850. else if (line[n + 1] == ')')
  1851. {
  1852. /* REVISIT: This gives false alarms on syntax like *--ptr */
  1853. if (line[n - 1] != ' ')
  1854. {
  1855. ERROR("Operator/assignment must be preceded "
  1856. "with whitespace", lineno, n);
  1857. }
  1858. break;
  1859. }
  1860. /* *= */
  1861. else if (line[n + 1] == '=')
  1862. {
  1863. check_spaces_leftright(line, lineno, n, n + 1);
  1864. n++;
  1865. }
  1866. else
  1867. {
  1868. /* A single '*' may be an binary operator, but
  1869. * it could also be a unary operator when used to deference
  1870. * a pointer.
  1871. */
  1872. check_spaces_left(line, lineno, n);
  1873. }
  1874. break;
  1875. case '%':
  1876. /* %= */
  1877. if (line[n + 1] == '=')
  1878. {
  1879. check_spaces_leftright(line, lineno, n, n + 1);
  1880. n++;
  1881. }
  1882. else
  1883. {
  1884. check_spaces_leftright(line, lineno, n, n);
  1885. }
  1886. break;
  1887. case '<':
  1888. /* <=, <<, <<= */
  1889. if (line[n + 1] == '=')
  1890. {
  1891. check_spaces_leftright(line, lineno, n, n + 1);
  1892. n++;
  1893. }
  1894. else if (line[n + 1] == '<')
  1895. {
  1896. if (line[n + 2] == '=')
  1897. {
  1898. check_spaces_leftright(line, lineno, n, n + 2);
  1899. n += 2;
  1900. }
  1901. else
  1902. {
  1903. check_spaces_leftright(line, lineno, n, n + 1);
  1904. n++;
  1905. }
  1906. }
  1907. else
  1908. {
  1909. check_spaces_leftright(line, lineno, n, n);
  1910. }
  1911. break;
  1912. case '>':
  1913. /* >=, >>, >>= */
  1914. if (line[n + 1] == '=')
  1915. {
  1916. check_spaces_leftright(line, lineno, n, n + 1);
  1917. n++;
  1918. }
  1919. else if (line[n + 1] == '>')
  1920. {
  1921. if (line[n + 2] == '=')
  1922. {
  1923. check_spaces_leftright(line, lineno, n, n + 2);
  1924. n += 2;
  1925. }
  1926. else
  1927. {
  1928. check_spaces_leftright(line, lineno, n, n + 1);
  1929. n++;
  1930. }
  1931. }
  1932. else
  1933. {
  1934. check_spaces_leftright(line, lineno, n, n);
  1935. }
  1936. break;
  1937. case '|':
  1938. /* |=, || */
  1939. if (line[n + 1] == '=')
  1940. {
  1941. check_spaces_leftright(line, lineno, n, n + 1);
  1942. n++;
  1943. }
  1944. else if (line[n + 1] == '|')
  1945. {
  1946. check_spaces_leftright(line, lineno, n, n + 1);
  1947. n++;
  1948. }
  1949. else
  1950. {
  1951. check_spaces_leftright(line, lineno, n, n);
  1952. }
  1953. break;
  1954. case '^':
  1955. /* ^= */
  1956. if (line[n + 1] == '=')
  1957. {
  1958. check_spaces_leftright(line, lineno, n, n + 1);
  1959. n++;
  1960. }
  1961. else
  1962. {
  1963. check_spaces_leftright(line, lineno, n, n);
  1964. }
  1965. break;
  1966. case '=':
  1967. /* == */
  1968. if (line[n + 1] == '=')
  1969. {
  1970. check_spaces_leftright(line, lineno, n, n + 1);
  1971. n++;
  1972. }
  1973. else
  1974. {
  1975. check_spaces_leftright(line, lineno, n, n);
  1976. }
  1977. break;
  1978. case '~':
  1979. check_spaces_left(line, lineno, n);
  1980. break;
  1981. case '!':
  1982. /* != */
  1983. if (line[n + 1] == '=')
  1984. {
  1985. check_spaces_leftright(line, lineno, n, n + 1);
  1986. n++;
  1987. }
  1988. /* !! */
  1989. else if (line[n + 1] == '!')
  1990. {
  1991. check_spaces_left(line, lineno, n);
  1992. n++;
  1993. }
  1994. else
  1995. {
  1996. check_spaces_left(line, lineno, n);
  1997. }
  1998. break;
  1999. default:
  2000. break;
  2001. }
  2002. }
  2003. }
  2004. /* Loop terminates when NUL or newline character found */
  2005. if (line[n] == '\n' || line[n] == '\0')
  2006. {
  2007. /* If the parse terminated on the NULL, then back up to the last
  2008. * character (which should be the newline).
  2009. */
  2010. int m = n;
  2011. if (line[m] == '\0' && m > 0)
  2012. {
  2013. m--;
  2014. }
  2015. /* Check for space at the end of the line. Except for carriage
  2016. * returns which we have already reported (one time) above.
  2017. */
  2018. if (m > 1 && isspace((int)line[m - 1]) &&
  2019. line[m - 1] != '\n' && line[m - 1] != '\r')
  2020. {
  2021. ERROR("Dangling whitespace at the end of line", lineno, m);
  2022. }
  2023. /* The line width is determined by the location of the final
  2024. * asterisk in block comments. The closing line of the block
  2025. * comment will exceed that by one one character, the '/'
  2026. * following the final asterisk.
  2027. */
  2028. else if (m > g_maxline)
  2029. {
  2030. bool bslash;
  2031. int a;
  2032. for (bslash = false, a = m;
  2033. a > 2 && strchr("\n\r/", line[a]) != NULL;
  2034. a--)
  2035. {
  2036. if (line[a] == '/')
  2037. {
  2038. bslash = true;
  2039. }
  2040. }
  2041. if (bslash && line[a] == '*')
  2042. {
  2043. m = a + 1;
  2044. }
  2045. }
  2046. /* Check for long lines
  2047. *
  2048. * REVISIT: Long line checks suppressed on right hand comments
  2049. * for now. This just prevents a large number of difficult-to-
  2050. * fix complaints that we would have otherwise.
  2051. */
  2052. if (m > g_maxline && !rhcomment)
  2053. {
  2054. ERROR("Long line found", lineno, m);
  2055. }
  2056. }
  2057. /* STEP 4: Check alignment */
  2058. /* Within a comment block, we need only check on the alignment of the
  2059. * comment.
  2060. */
  2061. if ((ncomment > 0 || prevncomment > 0) && !bstring)
  2062. {
  2063. /* Nothing should begin in comment zero */
  2064. if (indent == 0 && line[0] != '/' && !bexternc)
  2065. {
  2066. /* NOTE: if this line contains a comment to the right of the
  2067. * code, then ncomment will be misleading because it was
  2068. * already incremented above.
  2069. */
  2070. if (ncomment > 1 || rhcomment == 0)
  2071. {
  2072. ERROR("No indentation line", lineno, indent);
  2073. }
  2074. }
  2075. else if (indent == 1 && line[0] == ' ' && line[1] == '*')
  2076. {
  2077. /* Good indentation */
  2078. }
  2079. else if (indent > 0 && line[indent] == '\n')
  2080. {
  2081. ERROR("Whitespace on blank line", lineno, indent);
  2082. }
  2083. else if (indent > 0 && indent < 2)
  2084. {
  2085. if (bnest > 0)
  2086. {
  2087. ERROR("Insufficient indentation", lineno, indent);
  2088. }
  2089. else
  2090. {
  2091. ERROR("Expected indentation line", lineno, indent);
  2092. }
  2093. }
  2094. else if (indent > 0 && !bswitch)
  2095. {
  2096. if (line[indent] == '/')
  2097. {
  2098. /* Comments should like at offsets 2, 6, 10, ...
  2099. * This rule is not followed, however, if the comments are
  2100. * aligned to the right of the code.
  2101. */
  2102. if ((indent & 3) != 2 && rhcomment == 0)
  2103. {
  2104. ERROR("Bad comment alignment", lineno, indent);
  2105. }
  2106. /* REVISIT: This screws up in cases where there is C code,
  2107. * followed by a comment that continues on the next line.
  2108. */
  2109. else if (line[indent + 1] != '*')
  2110. {
  2111. ERROR("Missing asterisk in comment", lineno, indent);
  2112. }
  2113. }
  2114. else if (line[indent] == '*')
  2115. {
  2116. /* REVISIT: Generates false alarms on comments at the end of
  2117. * the line if there is nothing preceding (such as the aligned
  2118. * comments with a structure field definition). So disabled
  2119. * for comments before beginning of function definitions.
  2120. *
  2121. * Suppress this error if this is a comment to the right of
  2122. * code.
  2123. * Those may be unaligned.
  2124. */
  2125. if ((indent & 3) != 3 && bfunctions && dnest == 0 &&
  2126. rhcomment == 0)
  2127. {
  2128. ERROR("Bad comment block alignment", lineno, indent);
  2129. }
  2130. if (line[indent + 1] != ' ' &&
  2131. line[indent + 1] != '*' &&
  2132. line[indent + 1] != '\n' &&
  2133. line[indent + 1] != '/')
  2134. {
  2135. ERROR("Invalid character after asterisk "
  2136. "in comment block", lineno, indent);
  2137. }
  2138. }
  2139. /* If this is not the line containing the comment start, then this
  2140. * line should begin with '*'
  2141. */
  2142. else if (prevncomment > 0)
  2143. {
  2144. ERROR("Missing asterisk in comment block", lineno, indent);
  2145. }
  2146. }
  2147. }
  2148. /* Check for various alignment outside of the comment block */
  2149. else if ((ncomment == 0 && prevncomment == 0) && !bstring)
  2150. {
  2151. if (indent == 0 && strchr("\n#{}", line[0]) == NULL)
  2152. {
  2153. /* Ignore if we are at global scope */
  2154. if (prevbnest > 0)
  2155. {
  2156. bool blabel = false;
  2157. if (isalpha((int)line[indent]))
  2158. {
  2159. for (i = indent + 1; isalnum((int)line[i]) ||
  2160. line[i] == '_'; i++);
  2161. blabel = (line[i] == ':');
  2162. }
  2163. if (!blabel && !bexternc)
  2164. {
  2165. ERROR("No indentation line", lineno, indent);
  2166. }
  2167. }
  2168. }
  2169. else if (indent == 1 && line[0] == ' ' && line[1] == '*')
  2170. {
  2171. /* Good indentation */
  2172. }
  2173. else if (indent > 0 && line[indent] == '\n')
  2174. {
  2175. ERROR("Whitespace on blank line", lineno, indent);
  2176. }
  2177. else if (indent > 0 && indent < 2)
  2178. {
  2179. ERROR("Insufficient indentation line", lineno, indent);
  2180. }
  2181. else if (line[indent] == '{')
  2182. {
  2183. /* Check for left brace in first column, but preceded by a
  2184. * blank line. Should never happen (but could happen with
  2185. * internal compound statements).
  2186. */
  2187. if (indent == 0 && lineno == blank_lineno + 1)
  2188. {
  2189. ERROR("Blank line before opening left brace", lineno, indent);
  2190. }
  2191. /* REVISIT: Possible false alarms in compound statements
  2192. * without a preceding conditional. That usage often violates
  2193. * the coding standard.
  2194. */
  2195. else if (!bfunctions && (indent & 1) != 0)
  2196. {
  2197. ERROR("Bad left brace alignment", lineno, indent);
  2198. }
  2199. else if ((indent & 3) != 0 && !bswitch && dnest == 0)
  2200. {
  2201. ERROR("Bad left brace alignment", lineno, indent);
  2202. }
  2203. }
  2204. else if (line[indent] == '}')
  2205. {
  2206. /* REVISIT: Possible false alarms in compound statements
  2207. * without a preceding conditional. That usage often violates
  2208. * the coding standard.
  2209. */
  2210. if (!bfunctions && (indent & 1) != 0)
  2211. {
  2212. ERROR("right left brace alignment", lineno, indent);
  2213. }
  2214. else if ((indent & 3) != 0 && !bswitch && prevdnest == 0)
  2215. {
  2216. ERROR("Bad right brace alignment", lineno, indent);
  2217. }
  2218. }
  2219. else if (indent > 0)
  2220. {
  2221. /* REVISIT: Generates false alarms when a statement continues on
  2222. * the next line. The bstatm check limits to lines beginning
  2223. * with C keywords.
  2224. * REVISIT: The bstatm check will not detect statements that
  2225. * do not begin with a C keyword (such as assignment statements).
  2226. * REVISIT: Generates false alarms on comments at the end of
  2227. * the line if there is nothing preceding (such as the aligned
  2228. * comments with a structure field definition). So disabled for
  2229. * comments before beginning of function definitions.
  2230. */
  2231. if ((bstatm || /* Begins with C keyword */
  2232. (line[indent] == '/' && bfunctions)) && /* Comment in functions */
  2233. !bswitch && /* Not in a switch */
  2234. dnest == 0) /* Not a data definition */
  2235. {
  2236. if ((indent & 3) != 2)
  2237. {
  2238. ERROR("Bad alignment", lineno, indent);
  2239. }
  2240. }
  2241. /* Crazy cases. There should be no small odd alignments
  2242. * outside of comment/string. Odd alignments are possible
  2243. * on continued lines, but not if they are small.
  2244. */
  2245. else if (indent == 1 || indent == 3)
  2246. {
  2247. ERROR("Small odd alignment", lineno, indent);
  2248. }
  2249. }
  2250. }
  2251. }
  2252. if (!bfunctions && g_file_type == C_SOURCE)
  2253. {
  2254. ERROR("\"Private/Public Functions\" not found!"
  2255. " File was not be checked", lineno, 1);
  2256. }
  2257. if (ncomment > 0 || bstring)
  2258. {
  2259. ERROR("Comment or string found at end of file", lineno, 1);
  2260. }
  2261. fclose(instream);
  2262. if (g_verbose == 1)
  2263. {
  2264. fprintf(stderr, "%s: %s nxstyle check\n", g_file_name,
  2265. g_status == 0 ? "PASSED" : "FAILED");
  2266. }
  2267. return g_status;
  2268. }