-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CodeFormatter produces inconsistent results when used through CodeFormatterApplication #738
Comments
Ok, I tried a few things but still wasn't able to get things working. I tried to change: cdt/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java Lines 189 to 190 in 98e86ff
To pass in Map<String, String> macros = new HashMap<>();
macros.put("__LDBL_DECIMAL_DIG__", "21");
macros.put("__INT64_MAX__", "0x7fffffffffffffffL");
// <snip />
// pass in macro map instead of calling default constructor
IScannerInfo scanInfo = new ScannerInfo(macros); This had the effect of getting all of the macros and predefined macros getting added to cdt/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java Lines 539 to 557 in 98e86ff
Unfortunately, that wasn't sufficient and I'm out of obvious differences to try and hack in. |
Experiencing something similar, very annoying |
Describe the bug
We have some workflows that use the CDT code formatter application to auto-format source code and noticed some unexpected results when run on projects using C++11 or later features. When using the same ruleset and formatting from the GUI, the format was consistent with our expectations and different from the output when running from CodeFormatter application from command line.
To Reproduce
Steps to reproduce the behavior:
eclipse -nosplash -application org.eclipse.cdt.core.CodeFormatter -verbose -config .settings/org.eclipse.cdt.core.prefs src/test_format.cpp -vmargs -Dosgi.dataAreaRequiresExplicitInit=false
Note
std::get <0>
turned intostd::get < 0 >
Expected behavior
Formatting code using the same ruleset in Eclipse GUI and from CodeFormatter command line application produce identical results.
Screenshots
If applicable, add screenshots to help explain your problem.
Version Information (please complete the following information):
Additional context
The issue seems to be that the CodeFormatter application is configuring a default toolchain that doesn't understand modern C++. I tried cloning the CDT source code and hacking at it. I have never looked at any Eclipse source code before, so some of this may be incorrect, but here are my observations anyways. The formatting starts here:
cdt/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java
Line 189 in 98e86ff
Things that seem important for future processing is that we're always creating a blank
ScanInfo
(line 180) andILanguage language
(line 183) will always be null as that option stores an object instance that gets set at runtime when opening projects and isn't going to be present in the project's .settings/org.eclipse.cdt.core.prefs .Moving on to creating the
IASTTranslationUnit
cdt/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/AbstractCLikeLanguage.java
Line 145 in 98e86ff
Two bits seem to be important - the scanner and parser. The scanner when run CodeFormatter applications ends up returning the default GCC implementation, which lacks keywords in versioned companions:
cdt/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java
Line 48 in 98e86ff
I tried changing the default
private static GPPScannerExtensionConfiguration CONFIG = new GPPScannerExtensionConfiguration();
to instead instantiate newer versions (e.g.new GPPScannerExtensionConfiguration(VERSION_10_0);
), but that didn't have any observable effect on behavior. So that alone isn't sufficient.When running the formatter from the GUI, the parser is created with an
ExtendedScanInfo
object that has parser settings:cdt/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/AbstractCLikeLanguage.java
Lines 149 to 152 in 98e86ff
I can't see any way to hack those in without having actually imported a CDT Project, and not sure if I'm going down a rabbit hole. However, there seems to be something wrong with how the CodeFormatter is parsing modern C++, and there should be some way to configure it to understand modern syntax to prevent conflicting output with GUI.
The text was updated successfully, but these errors were encountered: