Skip to content
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

Support C/C++ formating from Eclipse CDT #265

Open
wmarkow opened this issue Jan 5, 2018 · 3 comments
Open

Support C/C++ formating from Eclipse CDT #265

wmarkow opened this issue Jan 5, 2018 · 3 comments

Comments

@wmarkow
Copy link

wmarkow commented Jan 5, 2018

It would be good to format C/C++ code as well. Eclipse CDT can be used as a C/C++ development IDE. It has already a formatter that seems to work in a similar way like for standard Java development.

I think it could use the CodeFormatter class from org.eclipse.cdt.core jar. At the beginning it could format the following files:

  • .c .h .cpp a standard C/C++ files
  • .ino .pde a files used by Arduino IDE; they are actually a C/C++ files but Arduino IDE just uses a different file extensions

With this solution it would be possible to format a large C/C++ project pretty fast. Let me know what you think.

@ctubbsii
Copy link
Member

ctubbsii commented Jan 8, 2018

I think this would be useful, but it would probably have to run outside of a Maven project, since most C/C++ projects don't use Maven to build. Running outside of a Maven project is being discussed as problematic on #261 , but it may be possible to create a separate mojo for formatting C/C++ code outside of a project.

@wmarkow
Copy link
Author

wmarkow commented Jan 16, 2018

I did some job on embedding Eclipse CDT into fromatter-maven-plgin. Some commits are available in my cdt_#265 branch.

There are some issues with Eclipse CDT formatter:

  • I wasn't able to find org.eclipse.cdt.core jar in Maven repository, so I have used the com.googlecode.maven-download-plugin:download-maven-plugin to download the missing jar from Eclipse CDT releases and copy it manually to local Maven repository. With my Maven 3.0.5 I need to execute just mvn clean so the missing jar will be downloaded and copied into local Maven repo. Then I can execute mvn clean install to compile and build the formatter-maven-plugin.
  • Eclipse CDT formatter can collect some warning messages during formatting. When the formatting is finished it wants to push the warnings out by calling
    org.eclipse.cdt.core.CCorePlugin.log(IStatus)
    and now fun begins because this log method can throw exception.
  • If the C/C++ file doesn't contain any formatting warning then org.eclipse.cdt.core.CCorePlugin.log(IStatus) will not be called at all. So far so good. But if you C/C++ will have some formatting warnings that we will have a NullPointerException thrown because CCorePlugin is not "prepared" to be executed outside of the Eclipse CDT. net.revelc.code.formatter.cpp.DefaultCppFormatter.java can catch this kind of exception; in this case the file will not be formatted.
  • it is possible to format files that contains some formatting warnings but it requires additional configuration of some CDT classes. Some fields of some classes must not be NULL so it will be possible to push out the warning messages to org.eclipse.cdt.core.CCorePlugin.log(IStatus). Because some of these fields are private, and some methods called are static, I have used a reflection to inject objects into those fields. I tried to use inheritance but I've failed (with reflection it was easy). This is what net.revelc.code.formatter.cpp.CppFormatter is doing.

I have prepared unit tests to cover some specific cases. I didn't create a separate mojo for C/C++ formatting, I just added this to a FormatterMojo. I have checked this and it works with my C/C++ code. I know that most C/C++ projects doesn't use Maven to build. Maybe it could be used together with nar-maven-plugin.

@ctubbsii
Copy link
Member

@wmarkow It sounds like you've made some good progress. I think before this would be ready for a pull request, I'd want to see the relevant CDT jars for formatting be published to Maven Central, and for it to better support formatting outside of an Eclipse CDT project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants