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

Add __FILE__ and __LINE__ support #512

Merged
merged 5 commits into from
Jan 15, 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
4 changes: 2 additions & 2 deletions Cesium.Ast/Cesium.Ast.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Yoakke.SynKit.C.Syntax" Version="2022.6.17-3.21.43-nightly" />
<PackageReference Include="Yoakke.SynKit.Lexer" Version="2022.6.17-3.21.43-nightly" />
<PackageReference Include="Yoakke.SynKit.C.Syntax" />
<PackageReference Include="Yoakke.SynKit.Lexer" />
</ItemGroup>

</Project>
12 changes: 6 additions & 6 deletions Cesium.CodeGen.Tests/Cesium.CodeGen.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Verify.Xunit" Version="16.3.5" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions Cesium.CodeGen/Cesium.CodeGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="QuikGraph" Version="2.5.0" />
<PackageReference Include="Mono.Cecil" />
<PackageReference Include="QuikGraph" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Cesium.Compiler.Tests/Cesium.Compiler.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions Cesium.Compiler/Cesium.Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Yoakke.SynKit.C.Syntax" Version="2023.5.30-5.54.38-nightly" />
<PackageReference Include="CommandLineParser" />
<PackageReference Include="Yoakke.SynKit.C.Syntax" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Cesium.Compiler/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static Task<string> Preprocess(string compilationSourcePath, string comp
.Concat(compilationOptions.AdditionalIncludeDirectories)
.ToImmutableArray();
var includeContext = new FileSystemIncludeContext(stdLibDirectory, includeDirectories);
var preprocessorLexer = new CPreprocessorLexer(reader);
var preprocessorLexer = new CPreprocessorLexer(new Yoakke.SynKit.Text.SourceFile(compilationSourcePath, reader));
var definesContext = new InMemoryDefinesContext();
var outOfFileRange = new Yoakke.SynKit.Text.Range();
foreach (var define in compilationOptions.DefineConstants)
Expand All @@ -73,7 +73,7 @@ private static Task<string> Preprocess(string compilationSourcePath, string comp
macroDefinition: new ObjectMacroDefinition(define),
replacement: new IToken<CPreprocessorTokenType>[]
{
new Token<CPreprocessorTokenType>(outOfFileRange, "1", CPreprocessorTokenType.PreprocessingToken)
new Token<CPreprocessorTokenType>(outOfFileRange, new(), "1", CPreprocessorTokenType.PreprocessingToken)
});
}

Expand Down
2 changes: 1 addition & 1 deletion Cesium.Core/Cesium.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="JetBrains.Annotations" />
</ItemGroup>

</Project>
14 changes: 7 additions & 7 deletions Cesium.IntegrationTests/Cesium.IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="MedallionShell" Version="1.6.2" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="MedallionShell" />
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NeoSmart.AsyncLock" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Cesium.IntegrationTests/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define Y
#define Z 0

#define SINGLE_HASH_(x) # x
#define SINGLE_HASH(x) SINGLE_HASH_(x)

int main(void)
{
printf("__TEST_DEFINE %i", __TEST_DEFINE);
Expand All @@ -12,5 +15,8 @@ int main(void)
printf("This does exists");
#endif

printf(SINGLE_HASH(x));
printf("line: %d file: %s ", __LINE__, __FILE__);

return 42;
}
10 changes: 5 additions & 5 deletions Cesium.Parser.Tests/Cesium.Parser.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Verify.Xunit" Version="16.3.5" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Cesium.Parser.Tests/ParserTests/TokenExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TokenExtensionsTests
[InlineData("\"\\x\"", "\\x")]
public void Test(string tokenText, string expected)
{
var token = new Token<CTokenType>(new Yoakke.SynKit.Text.Range(), tokenText, CTokenType.StringLiteral);
var token = new Token<CTokenType>(new Yoakke.SynKit.Text.Range(), new Yoakke.SynKit.Text.Location(), tokenText, CTokenType.StringLiteral);

var actual = token.UnwrapStringLiteral();

Expand Down
26 changes: 24 additions & 2 deletions Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ private static async Task DoTest(string source, Dictionary<string, string>? stan

private static async Task<string> DoPreprocess(string source, Dictionary<string, string>? standardHeaders = null, Dictionary<string, IList<IToken<CPreprocessorTokenType>>>? defines = null)
{
var lexer = new CPreprocessorLexer(source);
var filePath = "c:\\a\\b\\c.c";
var lexer = new CPreprocessorLexer(filePath, source);
var includeContext = new IncludeContextMock(standardHeaders ?? new Dictionary<string, string>());
var definesContext = new InMemoryDefinesContext(defines ?? new Dictionary<string, IList<IToken<CPreprocessorTokenType>>>());
var preprocessor = new CPreprocessor(source, lexer, includeContext, definesContext);
var preprocessor = new CPreprocessor(filePath, lexer, includeContext, definesContext);
var result = await preprocessor.ProcessSource();
return result;
}
Expand Down Expand Up @@ -417,5 +418,26 @@ public Task UnrollNestedDefines() => DoTest(
#define nested_foo foo
#define _(code) nested_foo(code)
_(""test"")
");

[Fact]
public Task DoubleHashOperator() => DoTest(
@"
#define HASHME(x) L ## x
#define NOHASHME(x) L x
HASHME(""test"")
NOHASHME(""test"")
");

[Fact]
public Task LineDefine() => DoTest(
@"
int x = __LINE__;
");

[Fact]
public Task FileDefine() => DoTest(
@"
char* x = __FILE__;
");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

L"test"
L "test"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

char* x = "c:\\a\\b\\c.c";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

int x = 2;
2 changes: 1 addition & 1 deletion Cesium.Parser/CParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,5 +1031,5 @@ private static ImmutableArray<Declaration> MakeDeclarationList(
}

private static ICToken MergeTokens(ICToken token1, ICToken token2) =>
new Token<CTokenType>(new Range(token1.Range, token2.Range), token1.Text + token2.Text, token2.Kind);
new Token<CTokenType>(new Range(token1.Range, token2.Range), token1.Location, token1.Text + token2.Text, token2.Kind);
}
8 changes: 4 additions & 4 deletions Cesium.Parser/Cesium.Parser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Yoakke.SynKit.C.Syntax" Version="2023.5.30-5.54.38-nightly" />
<PackageReference Include="Yoakke.SynKit.Lexer" Version="2023.5.30-5.54.38-nightly" />
<PackageReference Include="Yoakke.SynKit.Parser" Version="2023.5.30-5.54.38-nightly" />
<PackageReference Include="Yoakke.SynKit.Parser.Generator" Version="2023.5.30-5.54.38-nightly" />
<PackageReference Include="Yoakke.SynKit.C.Syntax" />
<PackageReference Include="Yoakke.SynKit.Lexer" />
<PackageReference Include="Yoakke.SynKit.Parser" />
<PackageReference Include="Yoakke.SynKit.Parser.Generator" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading