How to disable Eclipse CDT code formatter for a code block - c++

The CDT code formatter has a pretty decent selection of options, but it doesn't seem to have to a feature that allows one to tell it to ignore a block of code. This feature exists in the Java code formatter:
// #formatter:off
... // code that should not be formatted
// #formatter:on
Does this feature exist and I just don't know about it, or does anyone know of any decent work-arounds?
In my particular case, I'm trying to define data structures (enum types and arrays of strings) that I want to have specific layouts.

Use Astyle (Artistic Style) formatter, it's far superior to the Eclipse CDT built-in formatter and has the feature you require:
http://astyle.sourceforge.net/astyle.html#_Disable_Formatting
Example:
#include <iostream>
int main(int argc, char** argv)
{
// *INDENT-OFF*
std::cout<<"hello world"<<'\n';
// *INDENT-ON*
}
Formatting this using astyle won't indent the code between // INDENT-OFF and // INDENT-ON but it will also disable any other formatting features astyle does, like the spacing of the instructions in this case.
I use it myself configured as an external tool.
The only problem, external tools don't have hotkeys, but there is one hotkey to "Run Last Launched External Tool", and if you only use one external tool it works the same.
More details about the configuration (linux):
Astyle:
You can get it easily from your distribution repositories or via the official site.
To setup a configuration file with the formatting settings:
http://astyle.sourceforge.net/astyle.html#_Options_File
I use the home folder variant, just create a .astylerc in your $HOME, mine contains:
--suffix=none
--style=allman
--indent=tab=4
--max-code-length=70
--close-templates
--keep-one-line-blocks
--break-elseifs
--break-closing-brackets
--align-reference=type
--align-pointer=type
--indent-classes
--indent-modifiers
--indent-switches
--indent-cases
--indent-labels
--indent-col1-comments
--min-conditional-indent=0
--pad-oper
--pad-header
--unpad-paren
Eclipse:
"Run" menu --> External tools --> External tools Configurations... Add a new "Program" and in the configuration window:
Location: /usr/bin/astyle (use whereis or locate to check this)
Working Directory: ${project_loc}
Arguments: ${selected_resource_loc}
In the same window, refresh tab:
Tick Refresh resources upon completion.
Tick "The selected resource"
Same window, common tab:
Display in favorites menu, Tick "External tools"

Yes, you can do it since CDT supports this feature starting from version 9.7. The behavior is exactly the same of JDT.

If you are using OS X or Linux (I haven't checked Windows, but it may be supported), you can use clang-format and CppStyle instead.
clang-format is a formatter utility which is provided with Clang, and it supports on/off comments // clang-format on and // clang-format off in C/C++/ObjC code. An introduction to build Clang and its utility tools can be found here.
http://clang.llvm.org/get_started.html
You do not need to install whole Clang and LLVM files on your system. Because clang-format is a standalone program which works without Clang. The on/off comments are not supported in old versions, so please use ver 3.7 (available from SVN as of Feb 2015).
CppStyle is an Eclipse plugin which enables us to use clang-format from Eclipse CDT.
https://github.com/wangzw/cppstyle
FYI. Here is the same feature request in the CDT Bugzilla. The functionality might be officially supported in future, but using clang-format or Astyle seems to be a better solution at the moment. https://bugs.eclipse.org/bugs/show_bug.cgi?id=453926

I guess I could stick these in a file with an extension ignored by the formatter and include this file where appropriate. I tried this out and it works - the data structure gets picked up the indexer (i.e. autocomplete works). Still, it would be nice to have an equivalent to the Java "#formatter:..." syntax.

As far as I know the answer is simply no, such a feature does not exist. You might be able to implement such a feature using the SDK though. Beware that in my experience the documentation is very incomplete and it's very hard to find an Eclipse developer who would be willing to help you fill in the holes. But since the feature exists in the Java formatter and it is an open source product, perhaps you could port over the logic to the C++ formatter.
You could also avoid formatting the whole file, and instead format only by selection.

Related

Qt creator could not parse stddef.h --> incorrect code completion and highlighting

I am developing a QT GUI for my application using QT Creator (4.11.0).
Recently, our IT updated my OS to Ubuntu 20.04 (from 18.04) - maybe the error is related to that.
I have not touched to project in some month but yesterday wanted to continue developing it.
However, within the IDE there are now thousands of errors highlighted at almost every line of my code. (with highlighted I mean that there is this red background and a red dot beside the line number)
On the very top, it says:
Warning: The code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
cstddef:50:10: fatal error: 'stddef.h' file not found
...
The errors that are listed in the IDE are very wired like:
type `QMainWindow`is not a direct or virtual base of `MainWindow` (my class is called `MainWindow`)
I'm pretty sure it is not related to my code:
the code compiles and works fine - it is really just the IDE that is highlighting errors at every line of my code.
I have the same code on a Windows Computer and there no errors are listed in the IDE.
even if I start a brand new QT Widget project the errors appear within the template code provided by the QT Creator.
Since the GCC version changed with the update of the OS, I thought maybe I have to define a new KIT but this also did not help.
Is there anything I can do to fix the issue?
Do I have to reinstall the QT Creator?
I know, pictures are not very well-liked but here I think it might help to understand what I mean with "IDE is highlighting errors":
1. First
~/.profile :
CPATH="/usr/include/clang/10/include:$CPATH"
C_INCLUDE_PATH="/usr/include/clang/10/include:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="/usr/include/clang/10/include:$CPLUS_INCLUDE_PATH"
export CPATH
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH
maybe /usr/include/clang/10/include see in you file system.
2. Second
Logout / login.
3. Third
Specifying Build Settings (Projects -> Build -> in every config build) in Build Environment section: [unset] CPATH, C_INCLUDE_PATH and CPLUS_INCLUDE_PATH
or [edit] replace this variable with the necessary values
I fixed this issue by sudo apt install clang-8.
Source: https://bugs.launchpad.net/ubuntu/+source/qtcreator/+bug/1890434

Calling a c++ function on Rstudio on a MAC and getting (clang: error: unsupported option '-fopenmp')

I know how to code but I really do not know my way around a computer.
I have a program that I have to run for my master thesis. It is a code with multiple collabs and runs perfectly on Linux. However, it is a very complex simulational code and therefore it takes time to run for multiple parameters. I've been using my Linux at the university to run it but would like to run some of it on my personal computer (MAC OS). It works by using the R language to call upon c++ functions as follows (being filename a code on c++).
On a Rstudio script:
Sys.setenv("PKG_CPPFLAGS" = "-fopenmp -DPARALLEL")
system("rm filename.so")
system("rm filename.o")
system ("R CMD SHLIB filename.cpp")
dyn.load("filename.so")
After system ("R CMD SHLIB filename.cpp") I get error:
clang: error: unsupported option '-fopenmp'
make: *** [filename.o] Error 1
I've researched on the subject and found this
Enable OpenMP support in clang in Mac OS X (sierra & Mojave)
I've Installed LLVM, yet I do not know how to use it in this case.
How do I use it in this case?
Thank you in advance.
"Don't do it that way." Read up on R and Rcpp and use the proper tools (especially for packaging and/or compiling) which should pick up OpenMP where possible. In particular,
scan at least the Rcpp Introduction vignette
also look at the Rcpp Attributes vignette
"Just say no" to building the compilation commands by hand unless you know what you are doing with R and have read Writing R Extensions carefully a few times. It can be done, I used to show how in tutorials and workshops (see old slides from 12-15 years ago on my website) but we first moved to package inline which helps here, and later relied on the much better Rcpp Attributes.
Now, macOS has some extra hurdles in which tools work and which ones don't. The rcpp-devel mailing list may be of help, the default step is otherwise to consult the tutorial by James.
Edit: And of course if you "just want the above to work" try the obvious step of removing the part causing the error, i.e. use
Sys.setenv("PKG_CPPFLAGS" = "")
as your macOS box appears to have a compiler but not OpenMP (which, as I understand it, is the default thanks to some "surprising" default choices at Apple -- see the aforementioned tutorial for installation help.)

How to Change LLDB Version CLion Uses?

TL;DR: I want to change the version of LLDB that CLion (v2016.3.5) uses to LLDB v3.8.1. Can I do this? If so, how?
Longer explanation of the question:
CLion is a C++ IDE that I've been using for a few years now. Recently, they released version 2016.3.X. When they went from 2016.2 to 2016.3, they changed the "built in lldb" version from v3.8.1 to v3.9.0. This has caused a problem for me as v3.9.0 doesn't seem to want to work correctly.
When I, say, "print some_var_name" (while at a break point) I get an error (below):
Assertion failed: (D->getCachedLinkage() == LV.getLinkage()), function
getLVForDecl, file
/Applications/buildAgent/work/92515a49514b3993/lldb/llvm/tools/clang/lib/AST/Decl.cpp,
line 1360.
The source of this file can be found here: https://clang.llvm.org/doxygen/Decl_8cpp_source.html
My options are
(1) Figure out why that error is happening. Creating a simple "hello world"program and debugging seems to work. This tells me that it has something to do with my code base, I suppose. But I have over 20,000 lines of code. So figuring out what's doing it would be extremely time consuming. LLDB version 3.8.1 seems like a faster/easier fix since it was doing me just fine in the past.
(2) Use an old version of CLion (which, by default, utilizes LLDB version 3.8.1)
(3) Get the new(er) version(s) of CLion to use LLDB version 3.8.1.
Thanks for any help/guidance.
I assumed you could just enter the path in this preferences page:

clang_complete vim extension not working with STL

I have installed clang and clang_complete on a Linux system.
I added the following lines to my .vimrc:
let g:clang_user_options='|| exit 0'
set completeopt-=preview
Auto-completion works for my own code. However, it doesn't work with STL container. Even when I just type std::, I see a "user defined completion - pattern not found" at the bottom of the screen. What is the problem?
I use Vim 7.3 and clang_complete 1.8
The workaround is to tell clang_complete to use the clang library (instead of binary) for parsing. This is done by adding the following line to .vimrc:
let g:clang_use_library=1

playing with GCC 4.6 on windows

I am very pleased to find out that GCC 4.6 supports the range-based for loop. I found an experimental release of MinGW 4.6 on xvidvideo.ru, is that a well-known, reliable website? What other options do I have (besides compiling myself from source code)?
I wanted to try out GCC 4.7 using the latest Code::Blocks under Windows 7.
Here's how I did it for myself, YMMV:
I downloaded the latest Equation GCC file at:
ftp://ftp.equation.com/gcc/ and installed it under the directory C:\gcc\ on my local machine. The installer makes the necessary changes to the path environment variable. Logging off and on will pick them up.
I downloaded the Code::Blocks latest nightly build at: http://forums.codeblocks.org/index.php?board=20.0
and followed the setup instructions.
After following the setup instructions (including about the needed DLL files), and starting C::B for the first time;
I chose 'GNU GCC Compiler', and 'Set as default' for the 'Compilers auto-detection' window.
Under the 'Settings > Compiler... > Compiler settings' tab:
I ticked the 'Have g++ follow the coming C++0x ISO C++ language standard [-std=c++0x]' checkbox on.
Under the 'Settings > Compiler... > Toolchain executables' tab:
I changed the 'Compiler's installation directory' entry field to C:\gcc\bin\.
I changed the names of these files physically located in the C:\gcc\bin\ directory
i686-pc-mingw32-gcc.exe -=to=- mingw32-gcc.exe
i686-pc-mingw32-g++.exe -=to=- mingw32-g++.exe
make.exe -=to=- mingw32-make.exe
to match the listed name requirements in Code::Blocks. You can simply browse to set the correct files (I just personally preferred renaming to match C::B's entries).
If everything went correctly, you should be able to create this program:
#include <iostream>
#include <vector>
int main() {
using namespace std;
vector<int> my_vec = { 1, 2, 3, 4, 5 };
for (auto x : my_vec) {
cout << x << endl;
}
}
and run it OK under Code::Blocks with F9.
Thanks to everyone for all the excellent work put into bringing this great new language to us. Happy C++0x computing!
Bud Alverson
(sorry for the very basic nature of this post) :)
I'm not really familiar with the site you linked as it's in Russian. The only other place I've found that offers current snapshots of GCC's build is from Equation Solution. I downloaded gcc4.5.1 from there and it's been working fairly well for me. I haven't tried the 4.6.x release yet however. Rumor has it that gcc 4.6.x is slower than its predecessors.
Please do report back what kind of results you're seeing if you do decide experimenting. I'm curious about what improvements they've done in the 4.6.x series.