diff --git a/clion-common/src/main/java/org/sonarlint/intellij/clion/common/AnalyzerConfiguration.java b/clion-common/src/main/java/org/sonarlint/intellij/clion/common/AnalyzerConfiguration.java index 0bf449d6c9..e7cd57e740 100644 --- a/clion-common/src/main/java/org/sonarlint/intellij/clion/common/AnalyzerConfiguration.java +++ b/clion-common/src/main/java/org/sonarlint/intellij/clion/common/AnalyzerConfiguration.java @@ -29,6 +29,7 @@ import com.jetbrains.cidr.lang.workspace.headerRoots.HeadersSearchPath; import com.jetbrains.cidr.project.workspace.CidrWorkspace; import java.lang.reflect.Method; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -176,9 +177,13 @@ public static String getPreprocessorDefines(OCCompilerSettings compilerSettings) throw new IllegalStateException(e); } if (result instanceof List) { - return String.join("\n", (List) result) + "\n"; - } else if (result instanceof String) { - return result + "\n"; + return ((List) result).stream() + .map(String::trim) + .collect(Collectors.joining("\n")) + "\n"; + } else if (result instanceof String resultString) { + return Arrays.stream(resultString.split("\n")) + .map(String::trim) + .collect(Collectors.joining("\n")) + "\n"; } else { throw new IllegalStateException(result.toString()); } diff --git a/clion-common/src/test/java/org/sonarlint/intellij/clion/common/AnalyzerConfigurationTest.java b/clion-common/src/test/java/org/sonarlint/intellij/clion/common/AnalyzerConfigurationTest.java new file mode 100644 index 0000000000..a4ec5f48cf --- /dev/null +++ b/clion-common/src/test/java/org/sonarlint/intellij/clion/common/AnalyzerConfigurationTest.java @@ -0,0 +1,40 @@ +/* + * SonarLint for IntelliJ IDEA + * Copyright (C) 2015-2024 SonarSource + * sonarlint@sonarsource.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonarlint.intellij.clion.common; + +import com.jetbrains.cidr.lang.workspace.OCCompilerSettings; +import java.util.List; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AnalyzerConfigurationTest { + @Test + void testPreprocessorDefines() { + var compilerSettings = mock(OCCompilerSettings.class); + when(compilerSettings.getPreprocessorDefines()).thenReturn(List.of( + "#define a b", "#define c d ", " #define e f ", " #define g h", " #define i j")); + + var preprocessorDefines = AnalyzerConfiguration.getPreprocessorDefines(compilerSettings); + assertEquals("#define a b\n#define c d\n#define e f\n#define g h\n#define i j\n", preprocessorDefines); + } +}