Skip to content

Commit 0975824

Browse files
committed
feat(csharp): add test case for macro preprocessor
Add a new test case to `CSharpAstListenerTest` to test the macro preprocessor functionality. The test code includes a sample C# code with preprocessor directives. The test verifies that the analysis correctly identifies the data structures in the code.
1 parent aabd390 commit 0975824

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

chapi-ast-csharp/src/main/kotlin/chapi/ast/csharpast/CSharpAnalyser.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,24 @@ open class CSharpAnalyser : Analyser {
5656
preprocessorParser.inputStream = directiveTokenStream
5757
preprocessorParser.reset()
5858
// Parse condition in preprocessor directive (based on CSharpPreprocessorParser.g4 grammar).
59-
val directive: CSharpPreprocessorParser.Preprocessor_directiveContext = preprocessorParser.preprocessor_directive()
59+
val directive = preprocessorParser.preprocessor_directive()
60+
6061
// if true than next code is valid and not ignored.
6162
compiledTokens = directive.value
63+
val directiveStr = tokens[index + 1].text.trim { it <= ' ' }
64+
if ("line" == directiveStr || "error" == directiveStr || "warning" == directiveStr || "define" == directiveStr || "endregion" == directiveStr || "endif" == directiveStr || "pragma" == directiveStr) {
65+
compiledTokens = true
66+
}
67+
var conditionalSymbol: String?
68+
if ("define" == tokens[index + 1].text) {
69+
// add to the conditional symbols
70+
conditionalSymbol = tokens[index + 2].text
71+
preprocessorParser.ConditionalSymbols.add(conditionalSymbol)
72+
}
73+
if ("undef" == tokens[index + 1].text) {
74+
conditionalSymbol = tokens[index + 2].text
75+
preprocessorParser.ConditionalSymbols.remove(conditionalSymbol)
76+
}
6277
index = directiveTokenIndex - 1
6378
}
6479
token.channel == CSharpLexer.COMMENTS_CHANNEL -> {

chapi-ast-csharp/src/test/kotlin/chapi/ast/csharpast/CSharpAstListenerTest.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package chapi.ast.csharpast
33
import org.junit.jupiter.api.Test
44
import kotlin.test.assertEquals
55

6-
internal class CSharpAstListenerTest {
6+
internal class CSharpAstListenerTest {
77
private val helloworld = """
88
using System;
99
@@ -336,5 +336,26 @@ namespace Chapi {
336336
val structs = codeContainer.Containers[0].DataStructures
337337
assertEquals(structs.size, 1)
338338
}
339+
340+
@Test
341+
fun macroPreprocessor() {
342+
val code = """
343+
using System.Text;
344+
namespace testns {
345+
public class testcls {
346+
public static void Main(string []args) {
347+
#if DEBUG
348+
int x = 2;
349+
#else
350+
int y = 10;
351+
#endif
352+
}
353+
}
354+
}"""
355+
val codeContainer = CSharpAnalyser().analysis(code, "test.cs")
356+
val structs = codeContainer.Containers[0].DataStructures
357+
println(structs)
358+
assertEquals(structs.size, 1)
359+
}
339360
}
340361

0 commit comments

Comments
 (0)