A lightweight static analysis tool written in COBOL that counts actual goto usage in C/C++ source files.
Find a file
2026-05-19 22:59:26 +08:00
LICENSE Initial commit 2026-05-19 22:53:33 +08:00
nogoto.cbl Rename .nogoto.cbl to nogoto.cbl 2026-05-19 22:59:26 +08:00
README.md Enhance README with usage and feature details 2026-05-19 22:56:13 +08:00

nogoto

License: GPL v3 Language: COBOL Dijkstra Approved goto considered harmful

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 Dijkstras 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 goto that appears inside:
    • Single-line comments (//)
    • Multi-line comments (/* */)
    • Double-quoted strings ("...")
    • Single-quoted character constants ('...')
  • 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

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.