Prevent g++/clang++ from accepting files ending with open braces?

Viewed 32

Sometimes, if you have mismatched braces in a header file you're working on, you can get weird bugs in other, perfectly correct files. For example:

foo.hpp:

namespace foo {

/* etc. etc */

// } // whoops! no closing brace

main.cpp:

#include "foo.hpp"
#include "bar.hpp"

int main() { /* etc. */ }

The compiler will likely complain about improper definitions within bar.hpp.

My question: Can I get the compiler to notice that there are open braces at file boundary, and warn or emit an error in this case?

Notes:

  • This issue is partially relevant in C as well, but perhaps less so - no namespace there.
  • I'm asking about g++ and clang++, but you can mention other compilers as well.
  • I realize that the compiler only parses preprocessed C++, but - that includes comments with original file line numbers, so it can tell when a file "ends".
0 Answers
Related