looking at a sample swiftui App (Fruta), put together by Apple, and I see code like:
#if EXTENDED_ALL
...
#endif
But I can't find any documentation talking about this directive. Anybody know where I can find some docs on this?
Thx.
That is a “Conditional Compilation Block”:
A conditional compilation block allows code to be conditionally compiled depending on the value of one or more compilation conditions.
Every conditional compilation block begins with the #if compilation directive and ends with the #endif compilation directive.
Read more in “Compiler Control Statements” in The Swift Programming Language.
Ah, the question I was really asking was "what does EXTENDED_ALL mean?", thinking it was some standard condition. But it's not, it's actually custom, specific to this particular project. It's configured in the Build settings for the project.
Related
Is it possible to test whether a file has been included using preprocessing directives?
I wanted to achieve something like this:
#ifincluded assert.h
#error "Should not include assert.h"
#endif
At the moment, I solve this knowing that assert.h has defined _ASSERT_H. In this case, I can use #ifdef _ASSERT_H. The problem is that this is implementation specific.
Is there a better way to achieve this?
You could simply make use of the fact that assert() itself is a macro, so you could just do an
#ifdef assert
#error "Should not include assert.h"
#endif
This would, of course, also complain in case someone defines their own assert macro, which would, strictly speaking, be a legal thing to do as long as they don't include any standard header…
At the end of the day, I would reconsider whatever it is exactly that you think you're trying to do here. If you're at a point where you have to make sure someone does not include a specific standard header, you're most likely on the wrong path and there's most likely a much better but completely different approach to be found if you just take a step back and rethink from the very beginning. So
Is there a better way to achieve this?
Almost certainly. But it will not have anything to do with detecting and adjusting behavior depending on whether specific parts of the standard library have been included or not…
I was wondering if it is possible, and if yes how, can I run a C preprocessor, like cpp, on a
C++ source file and only process the conditional directives #if #endif etc. I would like other
directives to stay intact in the output file.
I'm doing some analysis on C# code and there is no C# pre-processor. My idea is to run a C preprocessor on C# file and process only conditionals. This way for example, the #region directive, will stay
in the file, but cpp appears to remove #region.
You might be looking for a tool like coan:
Coan is a software engineering tool for analysing preprocessor-based configurations of C or C++ source code. Its principal use is to simplify a body of source code by eliminating any parts that are redundant with respect to a specified configuration.
It's precisely designed to process #if and #ifdef preprocessor lines, and remove code accordingly, but it has a lot of other possible uses.
The linux unifdef command does what you want:
http://linux.die.net/man/1/unifdef
Even if you're not on linux, there is source available on the web.
BTW, this is a duplicate of another question: Way to omit undefined preprocessor branches by default with unifdef?
Oh, this is the same task as I had in the past. I've tried cpp unifdef and coan tools - all of them stumbled upon special C# preprocessor things like #region. In the end I've decided to make my own one:
https://github.com/gaDZella/undefine.
The tool has a pretty simple set of options compared to the mentioned cpp tools but it is fully compatible with C# preprocessor syntax.
You can use g++ -E option to stop after preprocessing stage
-E -> stop after the preprocessing stage.The output is in the form of preprocessed source code, which is sent to the standard output
I have a chunk of C and C++ code. I have to release the source to two different customers. I don't want them to see what the features others having. So, I'm planning to use switches for compilation. When to deliver the code, I would like to have few lines of the code stripped out. I know I can write a script to do it but I would like to know if are there any tools that exist to do this job.
#ifdef CUSTOMER_1
Code for Customer 1
#else //Customer_2
Code for Customer 2
#endif
For customer 2, I would like to have the code removed which comes under #ifdef and #else. I would like to remove the line #endif. Are there any tools that are readily available for this?
There's a utility called unifdef that's designed exactly for this purpose.
Why don't you put the customer-specific bits in a a customers.h file and include that file? You give different customers different customers.h. You haven't fully explained the problem, but by first sniff, static filtering of source code is a kludge.
The C preprocessor can do this for you of course, but it will process any other directives too. I think you would be best to do this with a specialised script
I'm looking for a tool which can do at least one of two things.
Guess what headers might be unused and can be removed.
Guess which headers should be included in a file, but are included indirectly through inclusion of other files. Thus allows proper compilation of the file.
Is there such a tool?
You could use GCC warnings "-Wmissing-declarations" and "-Wredundant-decls". It's not precisely what you want, but might help a lot.
A colleague of mine wrote a very simple script to achieve part of this (and slow too...).
Basically the idea is to try to comment each include in turn and then try to compile the object, it doesn't deal with include within headers but already remove a substantial number of unused files :)
EDIT:
Pseudo code of the algorithm
for s in sourceFiles:
while t := commentNextInclude(s):
if compilationOk(): s := t
As I said, comment each #include in turn, and each time check if the program still compiles, if it does, validate the commenting, and move on to the next one.
I don't have the rights to disclose the script source though.
While working within C++ libraries, I've noticed that I am not granted any intellisense while inside directive blocks like "#ifndef CLIENT_DLL ... #endif". This is obviously due to the fact that "CLIENT_DLL" has been defined. I realize that I can work around this by simply commenting out the directives.
Are there any intellisense options that will enable intellisense regardless of directive evaluation?
By getting what you want, you would lose a lot.
Visual C++ IntelliSense is based on a couple major presumptions
1. that you want good/usable results.
2. that your current IntelliSense compiland will present information related to the "configuration" you are currently in.
Because your current configuration has that preprocessor directive, you will not be able to get results from the #ifndef region.
The reason makes sense if you think it through. What if the IntelliSense compiler just tried to compile the region you were in, regardless of #ifdef regions? You would get nonsense and non-compilable code. It would not be able to make heads or tails of your compiland.
I can imagine a very complex solution where it runs a smaller (new) parse on the region you are in, with only that region being assumed to be part of the compiland. However, there are so many holes in this approach (like nothing in that region being declared/defined) that this possible approach would immediately frustrate you, except in very very simple scenarios.
Generally it's best to avoid logic in #ifdef regions, and instead to delegate the usage of parameterized compilation to entire functions, so that the front-end of the compiler is always compiling those modules, but the linker/optimizer will select the correct OBJ later on.
Hope that helps,
Will
Visual Studio 6.0 has a little better support for C++ in some arena's such as this. If you need the intellisense then just comment it out temporarily, build and then you should have intellisense. Just remember to recomment it when you're through if that was your intent.
I just wish Intellisense would work when it SHOULD in VS2008. MS "workarounds" don't work (deleting .ncb files) most of the time. Oooh,
here's another SO discussion..., let's see what IT has to say (I just love SO)
I'm often annoyed by that too ... but I wonder whether intellisense would actually be able to provide any useful information, in general, within a conditioned-out block?
The problem I see is that if the use of a variable or function changes depending on the value of a preprocessor directive then so may it's definition. If code-browsing features like "go to definition" were active within a conditioned-out block would you want them to lead to the currently-enabled definition or to one that was disabled by the same preprocessor conditions as the disabled code you're looking at?
I think the "princple of least surprise" dictates that the current behaviour is the safest, annoying though it is.
Why you want to do explicitly in the code?
There is already cofiguration setting in VS and the way you can enable and disble the intellisense.
see the link.
http://msdn.microsoft.com/en-us/library/ms173379(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ks1ka3t6(v=VS.80).aspx
This link may help you.