Skip to content

Commit af7cf15

Browse files
authored
[Compile Time Constant Extraction] Extract Property Wrapper Line + File Information (#63195)
1 parent 6bc52fd commit af7cf15

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

include/swift/AST/ConstTypeInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_CONST_TYPE_INFO_H
1414
#define SWIFT_AST_CONST_TYPE_INFO_H
1515

16+
#include "swift/AST/Attr.h"
1617
#include "swift/AST/Type.h"
1718
#include <memory>
1819
#include <string>
@@ -194,7 +195,7 @@ class RuntimeValue : public CompileTimeValue {
194195
};
195196

196197
struct CustomAttrValue {
197-
swift::Type Type;
198+
swift::CustomAttr *Attr;
198199
std::vector<FunctionParameter> Parameters;
199200
};
200201

lib/ConstExtract/ConstExtract.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ extractCustomAttrValues(VarDecl *propertyDecl) {
325325
{label, argExpr->getType(), extractCompileTimeValue(argExpr)});
326326
}
327327
}
328-
customAttrValues.push_back({propertyWrapper->getType(), parameters});
328+
329+
customAttrValues.push_back({propertyWrapper, parameters});
329330
}
330331

331332
return customAttrValues;
@@ -458,29 +459,14 @@ gatherConstValuesForPrimary(const std::unordered_set<std::string> &Protocols,
458459
return Result;
459460
}
460461

461-
void writeFileInformation(llvm::json::OStream &JSON, const VarDecl *VD) {
462-
SourceRange sourceRange = VD->getSourceRange();
463-
if (sourceRange.isInvalid())
464-
return;
465-
466-
const ASTContext &ctx = VD->getDeclContext()->getASTContext();
467-
JSON.attribute("file", ctx.SourceMgr.getDisplayNameForLoc(sourceRange.Start));
468-
JSON.attribute(
469-
"line",
470-
ctx.SourceMgr.getPresumedLineAndColumnForLoc(sourceRange.Start).first);
471-
}
472-
473-
void writeFileInformation(llvm::json::OStream &JSON,
474-
const NominalTypeDecl *NTD) {
475-
DeclContext *DC = NTD->getInnermostDeclContext();
476-
SourceLoc loc = extractNearestSourceLoc(DC);
477-
if (loc.isInvalid())
462+
void writeLocationInformation(llvm::json::OStream &JSON, SourceLoc Loc,
463+
const ASTContext &ctx) {
464+
if (Loc.isInvalid())
478465
return;
479466

480-
const ASTContext &ctx = DC->getASTContext();
481-
JSON.attribute("file", ctx.SourceMgr.getDisplayNameForLoc(loc));
467+
JSON.attribute("file", ctx.SourceMgr.getDisplayNameForLoc(Loc));
482468
JSON.attribute("line",
483-
ctx.SourceMgr.getPresumedLineAndColumnForLoc(loc).first);
469+
ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).first);
484470
}
485471

486472
void writeValue(llvm::json::OStream &JSON,
@@ -592,17 +578,20 @@ void writeValue(llvm::json::OStream &JSON,
592578
}
593579
}
594580

595-
void writeAttributes(
581+
void writePropertyWrapperAttributes(
596582
llvm::json::OStream &JSON,
597-
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers) {
583+
llvm::Optional<std::vector<CustomAttrValue>> PropertyWrappers,
584+
const ASTContext &ctx) {
598585
if (!PropertyWrappers.has_value()) {
599586
return;
600587
}
601588

602-
JSON.attributeArray("attributes", [&] {
589+
JSON.attributeArray("propertyWrappers", [&] {
603590
for (auto PW : PropertyWrappers.value()) {
604591
JSON.object([&] {
605-
JSON.attribute("type", toFullyQualifiedTypeNameString(PW.Type));
592+
JSON.attribute("type",
593+
toFullyQualifiedTypeNameString(PW.Attr->getType()));
594+
writeLocationInformation(JSON, PW.Attr->getLocation(), ctx);
606595
JSON.attributeArray("arguments", [&] {
607596
for (auto FP : PW.Parameters) {
608597
JSON.object([&] {
@@ -730,7 +719,9 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
730719
"kind",
731720
TypeDecl->getDescriptiveKindName(TypeDecl->getDescriptiveKind())
732721
.str());
733-
writeFileInformation(JSON, TypeDecl);
722+
writeLocationInformation(
723+
JSON, extractNearestSourceLoc(TypeDecl->getInnermostDeclContext()),
724+
TypeDecl->getInnermostDeclContext()->getASTContext());
734725
JSON.attributeArray("properties", [&] {
735726
for (const auto &PropertyInfo : TypeInfo.Properties) {
736727
JSON.object([&] {
@@ -741,9 +732,11 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
741732
JSON.attribute("isStatic", decl->isStatic() ? "true" : "false");
742733
JSON.attribute("isComputed",
743734
!decl->hasStorage() ? "true" : "false");
744-
writeFileInformation(JSON, decl);
735+
writeLocationInformation(JSON, decl->getLoc(),
736+
decl->getDeclContext()->getASTContext());
745737
writeValue(JSON, PropertyInfo.Value);
746-
writeAttributes(JSON, PropertyInfo.PropertyWrappers);
738+
writePropertyWrapperAttributes(
739+
JSON, PropertyInfo.PropertyWrappers, decl->getASTContext());
747740
writeResultBuilderInformation(JSON, TypeDecl, decl);
748741
writeAttrInformation(JSON, decl->getAttrs());
749742
});

test/ConstExtraction/ExtractLiterals.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ public struct PropertyWrappers : MyProto {
352352
// CHECK-NEXT: }
353353
// CHECK-NEXT: ]
354354
// CHECK-NEXT: },
355-
// CHECK-NEXT: "attributes": [
355+
// CHECK-NEXT: "propertyWrappers": [
356356
// CHECK-NEXT: {
357357
// CHECK-NEXT: "type": "ExtractLiterals.Buffered",
358+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
359+
// CHECK-NEXT: "line": 38,
358360
// CHECK-NEXT: "arguments": []
359361
// CHECK-NEXT: }
360362
// CHECK-NEXT: ]
@@ -399,9 +401,11 @@ public struct PropertyWrappers : MyProto {
399401
// CHECK-NEXT: }
400402
// CHECK-NEXT: ]
401403
// CHECK-NEXT: },
402-
// CHECK-NEXT: "attributes": [
404+
// CHECK-NEXT: "propertyWrappers": [
403405
// CHECK-NEXT: {
404406
// CHECK-NEXT: "type": "ExtractLiterals.Clamping",
407+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
408+
// CHECK-NEXT: "line": 41,
405409
// CHECK-NEXT: "arguments": [
406410
// CHECK-NEXT: {
407411
// CHECK-NEXT: "label": "min",
@@ -460,13 +464,17 @@ public struct PropertyWrappers : MyProto {
460464
// CHECK-NEXT: }
461465
// CHECK-NEXT: ]
462466
// CHECK-NEXT: },
463-
// CHECK-NEXT: "attributes": [
467+
// CHECK-NEXT: "propertyWrappers": [
464468
// CHECK-NEXT: {
465469
// CHECK-NEXT: "type": "ExtractLiterals.Buffered",
470+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
471+
// CHECK-NEXT: "line": 44,
466472
// CHECK-NEXT: "arguments": []
467473
// CHECK-NEXT: },
468474
// CHECK-NEXT: {
469475
// CHECK-NEXT: "type": "ExtractLiterals.Clamping",
476+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
477+
// CHECK-NEXT: "line": 44,
470478
// CHECK-NEXT: "arguments": [
471479
// CHECK-NEXT: {
472480
// CHECK-NEXT: "label": "min",

0 commit comments

Comments
 (0)