I've just noticed something interesting in the codeblocks IDE, via the editor settings > syntax highlighting, Saying 'User keywords', I am aware that they are reserved keywords such as for, int, signed, etc but what are these supposed to be?
My best guess is that it really means 'symbolic constants'. Google and Stack overflow doesn't seem to give me any information on what they are. However I do know that you can't really create your own keywords because they are part of the C/C++ language.
Any ideas? just curious.
However I do know that you can't really create your own keywords because they are part of the C/C++ language
You can't create your own in such a way that a compiler will understand. You need to separate what your IDE does from what your compiler does. The IDE has a facility for displaying keywords with a special color. That's easy to understand. But CodeBlocks doesn't rely on the compiler you use to "figure out" a token is a keyword. It has its own processor for this.
So what happens when you update your compiler and it supports a newer version of the language? Your IDE can't magically follow suit. It won't support those new keywords out of the box. That's why it allows you to specify "user keywords". So you may see the new keywords highlighted.
You are right, you can not create new keywords in C++. As you noticed this option is for syntax highlighting. You can enter there any words if you want to highlight them in the code editor. The code editor will highlight your words to make it easier to identify them in the code. Highlighting or in other words colorizing is the only purpose of that settings.
An example of a user keyword might be emit when invoking a Qt signal. Although qt signals are technically functions, they are meta compiled and are designed to mutate state outside their class scope. That's why many developers like to explicitly decorate them.
Typically emit evaluates macro expands to nothing
See this question for an explanation on how emit works:
Using emit vs calling a signal as if it's a regular function in Qt
Often compilers will have their own keywords that aren't officially in the standard. For example, GCC (C++) supports numerous C11 keywords like __thread or __restrict. You might need to manually add these to get syntax highlighting.
Related
In VS Code, with the C++ extension, is there any way I can get the parameter names for functions to be filled out automatically after e.g. selecting a function name suggested by intellisense? The parameters should then be filled out as if they were a snippet. This feature is common in several IDEs, such as Eclipse.
I think this is a perfectly valid question and nothing to do with IDE-like features as some are suggesting in the comments.
Short answer is No.
VS Code's C/C++ extension will not expand the autocompletion feature to the argument list of a function.
The more involved answer is that the authors of the C/C++ extension could do so but for some reason (probably a good one) have chosen not to.
The autocomplete feature is actually handled by the Language Server for C/C++. A Language Server is an implementation of the Language Server Protocol (LSP) which is simply a way to provide programming language-specific features for multiple code editors / IDEs.
The LSP, written by Microsoft originally for VSCode, absolutely permits such types of completion. After all, that is how snippets are handled. If you have a look at LSP's specification page you will see this bit
insertText is just a string so theoretically the server could pass the function name + argument list in the format of a snippet, set the variable InsertTextFormat == 2, marking it as a snippet and then pressing Tab would autocomplete to exactly what you want.
Here is an example of how it looks like (implemented in our Fortran language server)
You might want to open a feature request/ask the question in their repo. They will probably have a better answer as to why it is the way it is. If I had to guess I would say that it is related to overloading.
GCC seems to allow "and" / "or" to be used instead of "&&" / "||" in C++ code; however, as I expected, many compilers (notably MSVC 7) do not support this. The fact that GCC allows this has caused some annoyances for us in that we have different developers working on the same code base on multiple platforms and occasionally, these "errors" slip in as people are switching back and forth between Python and C++ development.
Ideally, we would all remember to use the appropriate syntax, but for those situations where we occasionally mess up, it would be really nice if GCC didn't let it slide. Anybody have any ideas on approaches to this?
If "and" and "or" are simply #defines then I could #undef when using GCC but I worry that it is more likely built into the compiler at more fundamental level.
Thanks.
They are part of the C++ standard, see for instance this StackOverflow answer (which quotes the relevant parts of the standard).
Another answer in the same question mentions how to do the opposite: make them work in MSVC.
To disable them in GCC, use -fno-operator-names. Note that, by doing so, you are in fact switching to a non-standard dialect of C++, and there is a risk that you end up writing code which might not compile correctly on standard-compliant compilers (for instance, if you declare a variable with a name that would normally be reserved).
The words are standard in C++ without the inclusion of any header.
The words are standard in C if you include the header <iso646.h>.
MSVC is doing you no service by not supporting the standards.
You could, however, use tools to enforce the non-use of the keywords. And it can be a coding guideline, and you can quickly train your team not to make silly portability mistakes. It isn't that hard to avoid the problem.
Have you considered any code analysis tools? Something similar to FxCop? With FxCop you can write your own rules( check for && ) and you can set it to run during the pre-compile stage.
-pedantic-errors may help for this, among other gnu-isms.
Previosly i used turboc++, but recently shifted to codeblocks and sometimes i also use notepad (i compile code using gcc via command promt, though possible in codeblocks). Now previously, in turbo c++ i used ctrl+F1 to get help for particular function or keywords or headerfiles etc, by keeping the cursor line on the keywords, the help is regarding the description of what the function does, whatever etc. Now in gcc(or in code::blocks) i want to know is it possible to get the help for, lets say for a perticular keyword, using some commands in gcc under command prompt?
(not sure, but i heard it is possible in clang).
What you're looking for is an IDE.
There are many, many choices. I'd strong recommend looking at Eclipse CDT:
http://www.eclipse.org/cdt/
http://www.ibm.com/developerworks/library/os-eclipse-stlcdt/
PS:
The "C:B" referred to above is "Code Blocks". That might also be a good choice for you - check it out, too:
http://www.codeblocks.org/
Visual C++ does not highlight userTypes by default like; vector or MyCustomType; I've gotten used to this in C# and am wondering is there some way to get it to highlight userTypes for c++. I have checked in the fonts and color settings; User Types is set to a color but it has no affect in the editor.
No, there is no way to do this in Visual Studio 2010.
The reason is probably because C++ is notoriously hard to parse and determining what tokens are class names would require a lot of processing. This was probably deemed impractical because it would be too slow, though I can only guess.
This absolutely CAN be done!
I do it all of the time with my C++ types because I have my own typedefs for any type which is not strictly defined (pretty much all of them). Defining my own types prevents future incompatibilities (or at least mitigates them), as well as helps with portability. However, it requires an extra step before the types are highlighted properly.
Simply create a plain text usertypes.dat file in the same directory as devenv.exe using a standard text editor.
Put one type name on each line.
Then restart the IDE.
There is also a tool that manages this if you have complex needs that can be found at:
http://msmvps.com/blogs/p3net/archive/2010/06/27/updateusertype-visual-studio-addin.aspx
I used to use Visual Assist X for highlighting and intellisense while coding C++ projects that use a lot of templates.. This used to help me quite a bit.
http://www.wholetomato.com/downloads/
You could give it a try. But I must say it relatively reduces the speed of VS. Install the free trial and if you don't like it you can always remove it..!
My target is to make a program (using C++) which would take C source code as input and check for "SYNTAX ERRORS ONLY".
Now for this, do i need to know about Regular Expressions, Grammar generation and Parsers??
I would like to use tools like Yacc/Flex/Bison - but the problems i am facing are -
How to use these tools? I mean i am only scratching at the surface when i read about these tools - i feel clueless.
How can i use these tools in tandem with my C++ source code?
How "The Hell" do i Get Started with this?
Use somebody else's C parser. For example, the parser used by the clang project. http://clang.llvm.org/
Then you can focus on the other hard part of your problem: detecting errors.
To get started with Yacc and Lex (or the Gnu versions, Bison and Flex) I can recommend Tom Niemann's A Compact Guide to Lex & Yacc.
I also suggest that you have a look of other projects doing the same thing. The are often named with lint in their name, as http://www.splint.org/
It all depends on what kind of errors you want to check.
In any cases you certainly need to learn more about compiler architectures. This book is a reference http://www.cs.princeton.edu/~appel/modern/c/
If you want to work at the syntactic level,
you certainly want to work with lex and Yacc.
This link may help you to get started with a working grammar (though outdated): http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
Less powerfull syntax checking can be done using regular expression. You can do less with regular expression than with an actual parser (see http://en.wikipedia.org/wiki/Chomsky_hierarchy). But it is certainly far more practical.
if you want to perform high level checking. Like "Does this group of function alway take const parameters ?" etc ... You can probably use GCC ability to dump abstract syntax trees (see http://digitocero.com/en/blog/exporting-and-visualizing-gccs-abstract-syntax-tree-ast). Checks other compilers or front-end as well. An abstract tree contains many information you can "check".
If you want to handle compilation errors: related to type checking etc... I can't help you, you probably want to look at other people projects before starting to write your own compiler.
see also:
http://decomp.ulb.ac.be/roelwuyts/playground/canalysistools/
http://wiki.altium.com/display/ADOH/Static+Code+Analysis+-+CERT+C+Secure+Code+Checking
Some people in my previous labs worked on C and C++ analysis and transformation
http://www.lrde.epita.fr/cgi-bin/twiki/view/Transformers/
The project is now in standby, and has proved to be a complex subject even for people used to compiler writting (especially in the case of C++ transformation).
Finally your needs are maybe far simpler than this. Did you think about
FILE *output = popen("gcc -Wall my_c_file.c", "r");
(and then just checking the output of gcc)
How do you define "SYNTAX ERRORS ONLY"? If you just want to know what are the errors, why don't you call external gcc to perform a compilation and report the errors?