Skip to content

Commit

Permalink
SLI-1586 Sanitize preprocessorDefines before giving it to the analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaladin committed Sep 11, 2024
1 parent df67ab0 commit 51fa676
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -176,9 +177,13 @@ public static String getPreprocessorDefines(OCCompilerSettings compilerSettings)
throw new IllegalStateException(e);
}
if (result instanceof List) {
return String.join("\n", (List<String>) result) + "\n";
} else if (result instanceof String) {
return result + "\n";
return ((List<String>) 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SonarLint for IntelliJ IDEA
* Copyright (C) 2015-2024 SonarSource
* [email protected]
*
* 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() {
OCCompilerSettings 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"));

String 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);
}
}

0 comments on commit 51fa676

Please sign in to comment.