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

SLI-1586 Sanitize preprocessorDefines before giving it to the analyzer #1160

Merged
merged 2 commits into from
Sep 12, 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 @@ -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() {
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);
}
}
Loading