goto usage in C/C++ source files.
- COBOL 100%
| LICENSE | ||
| nogoto.cbl | ||
| README.md | ||
nogoto
A lightweight static analysis tool written in COBOL that counts actual goto
usage in C/C++ source files. It skips comments, strings, and character
literals, so only logical control-flow goto statements are reported.
Background
In 1968, Edsger W. Dijkstra published his famous letter “Go To Statement
Considered Harmful.” Decades later, goto still survives in C and C++
code. This tool does not judge your style—it merely provides an objective
count. Consider it a quiet tribute to Dijkstra’s foresight, implemented in
a language born in the very same era.
Features
- Scans C/C++ source files line by line and counts occurrences of the
keyword
goto(case-insensitive). - Automatically ignores
gotothat appears inside:- Single-line comments (
//) - Multi-line comments (
/* */) - Double-quoted strings (
"...") - Single-quoted character constants (
'...')
- Single-line comments (
- Output preserves COBOL
PIC 9(9)leading zeros (e.g.000000002), making it easy to feed into other reporting systems.
Building
This program is written in GnuCOBOL and must be compiled in free format.
Requirements
- GnuCOBOL 3.1.2 or later
Compilation
cobc -x -free -o nogoto nogoto.cbl
Usage
./nogoto <source-file>
Example
Given the test file goto.c:
/* test_goto.c - sample for nogoto */
#include <stdio.h>
int main() {
int x = 0;
// This goto inside a comment should not be counted
if (x == 0)
goto label1; // this one should be counted
printf("This should be skipped by goto.\n");
label1:
printf("Arrived at label1.\n");
/*
Goto inside a block comment
should also be ignored
*/
goto label2; /* code goto – should be counted */
return 0;
label2:
printf("Arrived at label2.\n");
return 0;
}
Output:
C/C++ goto counter (ignores comments, strings, chars)
File: goto.c
Number of 'goto' (outside comments/strings/chars): 000000002
Batch scanning
Combine with find to scan an entire directory:
find . -name '*.c' -o -name '*.cpp' | xargs -I {} ./nogoto {}
Known Limitations
- C++ raw string literals (
R"...") are not handled. - Trigraphs and line continuation via
\are not supported. - Escape handling covers simple
\"and\'; extreme edge cases may produce minor inaccuracies.
For the vast majority of modern C/C++ code these limitations are negligible.
License
GNU General Public License v3.0 or later.