Skip to content

Conversation

@usama54321
Copy link
Contributor

@usama54321 usama54321 commented Nov 21, 2025

Add new clang build setting EMIT_SARIF_DIAGNOSTICS_FILE that passes
-fdiagnostic-add-output=sarif:file=. The flag emits a SARIF log during the compilation which contains diagnostic information. The path is currently fixed to <object_file_path>.compiled.sarif

passes
-fdiagnostic-add-output=sarif:file=<path>. The flag emits a SARIF log
during the compilation which contains diagnostic information. The path is currently fixed
to <object_file_path>.source-metadata.json
let dataCache = cbc.producer.getSpecDataCache(self, cacheType: DataCache.self)
let constantFlags = dataCache.getStandardFlags(self, producer: cbc.producer, scope: cbc.scope, optionContext: clangInfo, delegate: delegate, inputFileType: resolvedInputFileType)
let cbcWithOutput = cbc.outputs.isEmpty ? cbc.appendingOutputs([outputNode.path]) : cbc
let constantFlags = dataCache.getStandardFlags(self, cbc: cbcWithOutput, optionContext: clangInfo, delegate: delegate, inputFileType: resolvedInputFileType)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These flags are cached and reused across translation units, so I think this is going to incorrectly inject per-TU flags

@usama54321 usama54321 marked this pull request as ready for review December 9, 2025 20:37
@usama54321 usama54321 requested a review from owenv December 9, 2025 21:39
@usama54321 usama54321 changed the title [WIP] Add new clang build setting EMIT_COMPILER_SOURCE_METADATA that Add new clang build setting EMIT_COMPILER_SOURCE_METADATA that Dec 9, 2025
Copy link

@dbartol dbartol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly a bunch of naming suggestions.

public static let EMBEDDED_CONTENT_CONTAINS_SWIFT = BuiltinMacros.declareBooleanMacro("EMBEDDED_CONTENT_CONTAINS_SWIFT")
public static let EMBEDDED_PROFILE_NAME = BuiltinMacros.declareStringMacro("EMBEDDED_PROFILE_NAME")
public static let EMBED_PACKAGE_RESOURCE_BUNDLE_NAMES = BuiltinMacros.declareStringListMacro("EMBED_PACKAGE_RESOURCE_BUNDLE_NAMES")
public static let EMIT_COMPILER_SOURCE_METADATA = BuiltinMacros.declareBooleanMacro("EMIT_COMPILER_SOURCE_METADATA")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested name: CLANG_EMIT_SARIF_DIAGNOSTICS_FILE. Unless this macro is reused by the Swift compiler, in which case you should probably drop the CLANG_ prefix.

}
}

commandLine += addCompilerMetadataFlags(cbc, outputFileDir.join(outputNode.path.str + ".source-metadata.json"), delegate, &extraOutputs)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
commandLine += addCompilerMetadataFlags(cbc, outputFileDir.join(outputNode.path.str + ".source-metadata.json"), delegate, &extraOutputs)
commandLine += addCompilerSarifFlags(cbc, outputFileDir.join(outputNode.path.str + ".compiled.sarif"), delegate, &extraOutputs)

let ftb = FileToBuild(absolutePath: securityMetadataNode.path, fileType: metadatatype)
taskOutputs.append(securityMetadataNode)
delegate.declareOutput(ftb)
return ["-fdiagnostics-add-output=sarif:file=" + securityMetadataNode.path.str]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to add quoting and/or escaping here, since the path has to be part of the same option? Or does the fact the result is an array mean that the build system will property quote each array element?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter.

UTI = "public.protobuf-source";
},
{
Identifier = text.json.compiler-metadata.source;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Identifier = text.json.compiler-metadata.source;
Identifier = text.sarif;

Unless the hierarchy means something, in which case text.json.sarif is fine.

{
Identifier = text.json.compiler-metadata.source;
Type = FileType;
Name = "Source metadata emitted from compiler";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Name = "Source metadata emitted from compiler";
Name = "Diagnostics log in SARIF format.";

Identifier = text.json.compiler-metadata.source;
Type = FileType;
Name = "Source metadata emitted from compiler";
UTI = "com.apple.compiler-metadata.source";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UTI = "com.apple.compiler-metadata.source";
UTI = "com.apple.sarif";

@usama54321
Copy link
Contributor Author

@swift-ci test

@usama54321 usama54321 force-pushed the usama54321/fdiagnostics-add-output-clang-flag branch from deb9f17 to 8d321a5 Compare December 11, 2025 03:55
@usama54321
Copy link
Contributor Author

@swift-ci test

@usama54321 usama54321 changed the title Add new clang build setting EMIT_COMPILER_SOURCE_METADATA that Add new clang build setting EMIT_SARIF_DIAGNOSTICS_FILE Dec 11, 2025
@usama54321
Copy link
Contributor Author

@swift-ci test windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants