3

I want to check for all syntax & semantic errors in the flow while not wasting time on waiting it to compile every time.

  1. Is it possible with compiler architecture that gcc uses?

  2. If possible, how?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • gcc manages replacable front-ends (to support compiling of multiple languages) as well as replacable back-ends (to support code generation for multiple platforms, and specifically cross-platform compiling). So, theoretically, it should be possible to use a "null-backend" which would result in compiling the code (with all diagnostics as usual) just without creating a binary output. May be, such back-end already exists but I don't know. "quick"... Yeah... `gcc` is IMHO not slower than other C++ compilers I know but is this quick enough? – Scheff's Cat Sep 11 '19 at 13:23
  • Sounds like you need something like intellisense that MSVS offers. I believe VS code offers that if you are on linux. – NathanOliver Sep 11 '19 at 13:24
  • I mean raw compilation speed, in my experience it lags behind something like tcc. –  Sep 11 '19 at 13:25
  • 8
    Use flag `-fsyntax-only` – Mansoor Sep 11 '19 at 13:25
  • @Mansoor does it report type errors, printf etc warnings & casting warnings etc..? –  Sep 11 '19 at 13:27
  • @Mansoor Found the doc. side: [3.8 Options to Request or Suppress Warnings](https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html#Warning-Options): _Check the code for syntax errors, but don’t do anything beyond that._ If taken literally, semantic checks would be excluded... (Although, I find it sounds promising.) – Scheff's Cat Sep 11 '19 at 13:30
  • I got at least some warnings with [Demo](http://coliru.stacked-crooked.com/a/72b2b2bedc11649e). – Jarod42 Sep 11 '19 at 13:32
  • There seems to be a dupl. (which recommends `-fsyntax-only` as well): [SO: Compile without generating output file in GCC](https://stackoverflow.com/a/26738152/7478597) and a comment which recommends `gcc -Wall -Wextra -O -c somefile.c -o /dev/null` instead. Not the "null-backend" but just throwing any generated code away... ;-) – Scheff's Cat Sep 11 '19 at 13:33
  • @Jarod42 I modified your demo a bit: [Demo](http://coliru.stacked-crooked.com/a/49bf0f5f59890070). The syntax-only run seems really to skip some of the things diagnosed in ful compile. – Scheff's Cat Sep 11 '19 at 13:42
  • The `-S` option generates assembler; is that stopping soon enough? – Jonathan Leffler Sep 11 '19 at 13:45
  • Well, you can rarely check for *semantic* errors even in compiled program. – Eugene Sh. Sep 11 '19 at 14:04
  • Possible duplicate of [Compile without generating output file in GCC](https://stackoverflow.com/questions/26738082/compile-without-generating-output-file-in-gcc) – klutt Sep 11 '19 at 14:19
  • If it takes longer than 1 second for you to compile a single source file, then the problem is not the compiler. The problem is your IDE, or your computer or your LAN. Or less likely, you have a horrifically huge source file. In either case, not a compiler problem. – Lundin Sep 11 '19 at 14:47
  • @Lundin The codegen step can be surprisingly expensive. Both in terms of i/o and in terms of actual generation. I noticed a massive speedup in the compile step when enabling lto for a project, which led me down the path of looking for a "syntax-only" mode. – Chuu Jun 14 '23 at 15:53

1 Answers1

7

GCC has a -fsyntax-only flag which checks for syntax errors, see 3.8 Options to Request or Suppress Warnings.

Mansoor
  • 2,357
  • 1
  • 17
  • 27