0

I'm creating a language parser on a microprocessor in C++. For the tables of keywords and commands, rather than maintaining a single curated file (alphabetically sorted, etc), I'd prefer to declare them in the header files for each set of functions (ie, unsorted). Then, I can have the makefile run a program to scan the header files and generate a curated master header file of enums and such that the compiler can chew on. Thus, I need to hide my declarations of tokens/commands during regular preprocessing. I could go the doxygen route and just hide them within comments with a special tag. I'm wondering what latitude there is for creating my own pragma directives for my task.

Is it safe and portable to just declare my own pragma (eg. #pragma decl_token "goleft" )? As long as the preprocessor doesn't recognize it, it seems to be ignored. I've also tried using the #assert statement, which I would have thought could be a perfectly legitimate usage, but it seems problematic with being non-standard. I admit to being a little non-standard myself but I'm trying not to go too rogue.

0

1 Answer 1

6

#pragma directives are completely implementation-defined. That means the language implementation, which you are creating. You can do whatever you like with them. Literally anything.

It would be sensible if the first thing in your own custom pragmas was the name of your implementation, like GCC does, so you don't conflict with pragmas someone might put in their code meant for a different implementation. On the other hand, if you're deliberately adding a pragma that some other implementation also uses, you should make it the same (including if it has that implementation's name) so that the same pragma works on both implementations. For example, clang uses a lot of GCC pragmas.

When other implementations like GCC read your code, they typically emit warnings for unknown pragmas, not errors, so having your own pragmas doesn't prevent someone compiling your code with a different compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.