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

IEP-1277 Create .clang_format #1022

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface ILSPConstants
{
String CLANGD_EXECUTABLE = "clangd"; //$NON-NLS-1$
String CLANGD_CONFIG_FILE = ".clangd"; //$NON-NLS-1$
String CLANG_FORMAT_FILE = ".clang-format"; //$NON-NLS-1$
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import com.espressif.idf.core.internal.CMakeConsoleWrapper;
import com.espressif.idf.core.internal.CMakeErrorParser;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.ClangFormatFileHandler;
import com.espressif.idf.core.util.ClangdConfigFileHandler;
import com.espressif.idf.core.util.DfuCommandsUtil;
import com.espressif.idf.core.util.HintsUtil;
Expand Down Expand Up @@ -135,7 +136,7 @@
public IDFBuildConfiguration(IBuildConfiguration config, String name, IToolChain toolChain,
ICMakeToolChainFile toolChainFile, String launchMode)
{
super(config, name, toolChain, launchMode);

Check warning on line 139 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

View workflow job for this annotation

GitHub Actions / spotbugs

EI_EXPOSE_REP2

new com.espressif.idf.core.build.IDFBuildConfiguration(IBuildConfiguration, String, IToolChain, ICMakeToolChainFile, String) may expose internal representation by storing an externally mutable object into IDFBuildConfiguration.toolChainFile
Raw output
This code stores a reference to an externally mutable object into the internal representation of the object.  If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.
this.toolChainFile = toolChainFile;
}

Expand Down Expand Up @@ -167,7 +168,7 @@
{
org.eclipse.core.runtime.Path path = new org.eclipse.core.runtime.Path(customBuildDir);
if (!path.toFile().exists())
{

Check warning on line 171 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

View workflow job for this annotation

GitHub Actions / spotbugs

RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

Exceptional return value of java.io.File.mkdirs() ignored in com.espressif.idf.core.build.IDFBuildConfiguration.getBuildContainerPath()
Raw output
This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.
path.toFile().mkdirs();
}
return path;
Expand Down Expand Up @@ -277,7 +278,7 @@

private boolean isLocal() throws CoreException
{
IToolChain toolchain = getToolChain();

Check warning on line 281 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

View workflow job for this annotation

GitHub Actions / spotbugs

EI_EXPOSE_REP

com.espressif.idf.core.build.IDFBuildConfiguration.getToolChainFile() may expose internal representation by returning IDFBuildConfiguration.toolChainFile
Raw output
Returning a reference to a mutable object value stored in one of the object's fields exposes the internal representation of the object.  If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Returning a new copy of the object is better approach in many situations.
return (Platform.getOS().equals(toolchain.getProperty(IToolChain.ATTR_OS))
|| "linux-container".equals(toolchain.getProperty(IToolChain.ATTR_OS))) //$NON-NLS-1$
&& (Platform.getOSArch().equals(toolchain.getProperty(IToolChain.ATTR_ARCH)));
Expand All @@ -304,7 +305,7 @@

IProject project = getProject();
toolChainFile = getToolChainFile();

Check warning on line 308 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

View workflow job for this annotation

GitHub Actions / spotbugs

EI_EXPOSE_REP2

com.espressif.idf.core.build.IDFBuildConfiguration.build(int, Map, IConsole, IProgressMonitor) may expose internal representation by storing an externally mutable object into IDFBuildConfiguration.monitor
Raw output
This code stores a reference to an externally mutable object into the internal representation of the object.  If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.
Instant start = Instant.now();
if (!checkLaunchTarget(console) || !checkSpacesSupport(project, console) || !checkToolChainFile(console))
{
Expand All @@ -325,7 +326,7 @@

try
{
infoStream.write(String.format(Messages.CMakeBuildConfiguration_BuildingIn, buildDir.toString()));

Check warning on line 329 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

View workflow job for this annotation

GitHub Actions / spotbugs

RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

Exceptional return value of java.io.File.mkdir() ignored in com.espressif.idf.core.build.IDFBuildConfiguration.build(int, Map, IConsole, IProgressMonitor)
Raw output
This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.
boolean runCMake = cmakeListsModified;
if (!runCMake)
{
Expand All @@ -339,6 +340,7 @@
}
runCmakeBuildCommand(console, monitor, project, start, generator, infoStream, buildDir);
new ClangdConfigFileHandler().update(project);
new ClangFormatFileHandler(project).update();
return new IProject[] { project };
}
catch (Exception e)
Expand All @@ -355,38 +357,38 @@
Path pathPdIdfPath = Paths.get(projectDescriptionIdfPath);

if (StringUtil.isEmpty(projectDescriptionIdfPath))
{
return true;
}
IDFEnvironmentVariables idfEnvironmentVariables = new IDFEnvironmentVariables();
String envIdfPath = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.IDF_PATH);
Path pathEnvIdf = Paths.get(envIdfPath);

boolean samePaths = false;
if (Platform.getOS().equals(Platform.OS_WIN32))
{
samePaths = pathEnvIdf.toString().equalsIgnoreCase(pathPdIdfPath.toString());
}
else
{
samePaths = pathEnvIdf.toString().equals(pathPdIdfPath.toString());
}

if (!samePaths)
{
String outputMessage = MessageFormat.format(Messages.IDFBuildConfiguration_PreCheck_DifferentIdfPath,
projectDescriptionIdfPath, envIdfPath);
console.getInfoStream().write(outputMessage);

return false;
}

return true;
}

private void runCmakeBuildCommand(IConsole console, IProgressMonitor monitor, IProject project, Instant start,
String generator, ConsoleOutputStream infoStream, Path buildDir)
throws CoreException, IOException, CmakeBuildException

Check warning on line 391 in bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

View workflow job for this annotation

GitHub Actions / spotbugs

THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION

Method lists Exception in its throws clause.
Raw output
Method lists Exception in its throws clause.
When declaring a method, the types of exceptions in the throws clause should be the most specific. Therefore, using Exception in the throws clause would force the caller to either use it in its own throws clause, or use it in a try-catch block (when it does not necessarily contain any meaningful information about the thrown exception).

For more information, see the SEI CERT ERR07-J rule [https://wiki.sei.cmu.edu/confluence/display/java/ERR07-J.+Do+not+throw+RuntimeException%2C+Exception%2C+or+Throwable].
{
try (ErrorParserManager epm = new ErrorParserManager(project, getBuildDirectoryURI(), this,
getToolChain().getErrorParserIds()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# We'll use defaults from the LLVM style, but with some modifications so that it's close to the CDT K&R style.
BasedOnStyle: LLVM
UseTab: Always
IndentWidth: 4
TabWidth: 4
PackConstructorInitializers: NextLineOnly
BreakConstructorInitializers: AfterColon
IndentAccessModifiers: false
AccessModifierOffset: -4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright 2024 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.core.util;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.espressif.idf.core.ILSPConstants;

public class ClangFormatFileHandler
{
private final IFile clangFormatFile;

public ClangFormatFileHandler(IProject project) throws CoreException
{
this.clangFormatFile = project.getFile(ILSPConstants.CLANG_FORMAT_FILE);
}

/**
* Updates the .clang-format file. If the file does not exist, it is created and initialized with default settings.
*
* @throws IOException if an I/O error occurs during file creation or writing
* @throws CoreException if an error occurs while refreshing the project
*/
public void update() throws IOException, CoreException
{
if (clangFormatFile.exists())
{
return;
}
try (final var source = getClass().getResourceAsStream(".clang-format-project");) //$NON-NLS-1$
{
clangFormatFile.create(source, true, new NullProgressMonitor());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ public void updateLspQueryDrivers()
metadata.queryDriver().defaultValue());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.espressif.idf.core.IDFConstants;
import com.espressif.idf.core.build.IDFLaunchConstants;
import com.espressif.idf.core.logging.Logger;
import com.espressif.idf.core.util.ClangFormatFileHandler;
import com.espressif.idf.core.util.ClangdConfigFileHandler;
import com.espressif.idf.core.util.LaunchUtil;
import com.espressif.idf.ui.UIPlugin;
Expand Down Expand Up @@ -110,7 +111,7 @@
String projectName = projectCreationWizardPage.getProjectName();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
selProvider.setSelection(new StructuredSelection(project));
updateClangdFile(project);
updateClangFiles(project);
}
}

Expand Down Expand Up @@ -141,11 +142,12 @@
return performFinish;
}

private void updateClangdFile(IProject project)
private void updateClangFiles(IProject project)
{
try
{
new ClangdConfigFileHandler().update(project);
new ClangFormatFileHandler(project).update();
}
catch (Exception e)
{
Expand Down Expand Up @@ -198,7 +200,7 @@
}

IDFProjectGenerator generator = new IDFProjectGenerator(manifest, selectedTemplate, true,
projectCreationWizardPage.getSelectedTarget());

Check warning on line 203 in bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/NewIDFProjectWizard.java

View workflow job for this annotation

GitHub Actions / spotbugs

NP_NULL_ON_SOME_PATH

Possible null pointer dereference of NewIDFProjectWizard.projectCreationWizardPage in com.espressif.idf.ui.wizard.NewIDFProjectWizard.getGenerator()
Raw output
There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed. Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception cannot ever be executed; deciding that is beyond the ability of SpotBugs.
generator.setProjectName(projectCreationWizardPage.getProjectName());
if (!projectCreationWizardPage.useDefaults())
{
Expand All @@ -213,73 +215,73 @@
private String target;

public TargetSwitchJob(String target)
{
super(TARGET_SWITCH_JOB);
this.target = target;
launchBarManager = UIPlugin.getService(ILaunchBarManager.class);
}

private Job findInternalJob()
{
for (Job job : Job.getJobManager().find(null))
{
if (job.getName().equals(InternalDebugCoreMessages.CoreBuildLaunchBarTracker_Job))
{
return job;
}
}

return null;
}

@Override
protected IStatus run(IProgressMonitor monitor)
{
Job job = findInternalJob();
if (job != null)
{
try
{
job.join();
}
catch (InterruptedException e1)
{
Logger.log(e1);
}
}

Display.getDefault().syncExec(() -> {
ILaunchTarget launchTarget = findSuitableTargetForSelectedTargetString();
try
{
launchBarManager.setActiveLaunchTarget(launchTarget);
}
catch (CoreException e)
{
Logger.log(e);
}
});

return Status.OK_STATUS;

}

private ILaunchTarget findSuitableTargetForSelectedTargetString()
{
ILaunchTargetManager launchTargetManager = UIPlugin.getService(ILaunchTargetManager.class);
ILaunchTarget[] targets = launchTargetManager
.getLaunchTargetsOfType(IDFLaunchConstants.ESP_LAUNCH_TARGET_TYPE);

for (ILaunchTarget iLaunchTarget : targets)
{
String idfTarget = iLaunchTarget.getAttribute(IDFLaunchConstants.ATTR_IDF_TARGET, null);
if (idfTarget.contentEquals(target))
{
return iLaunchTarget;
}
}

return null;

Check warning on line 284 in bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/wizard/NewIDFProjectWizard.java

View workflow job for this annotation

GitHub Actions / spotbugs

SIC_INNER_SHOULD_BE_STATIC

Should com.espressif.idf.ui.wizard.NewIDFProjectWizard$TargetSwitchJob be a _static_ inner class?
Raw output
This class is an inner class, but does not use its embedded reference to the object which created it.  This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.  If possible, the class should be made static.
}
}
}
Loading