Skip to content

Commit 14a125c

Browse files
committed
[IDE] Use consistent PrintOptions in swift-ide-test
Use the same set of PrintOptions when printing the results for different requests.
1 parent 3141b5a commit 14a125c

File tree

5 files changed

+61
-64
lines changed

5 files changed

+61
-64
lines changed

include/swift/IDE/Utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ struct ResolvedRangeInfo {
402402
/*Single entry*/true, /*UnhandledEffects*/{},
403403
OrphanKind::None, {}, {}, {}) {}
404404
ResolvedRangeInfo(): ResolvedRangeInfo(ArrayRef<Token>()) {}
405-
void print(llvm::raw_ostream &OS) const;
405+
void print(llvm::raw_ostream &OS,
406+
const PrintOptions &PO = PrintOptions()) const;
406407
ExitState exit() const { return ExitInfo.Exit; }
407408
Type getType() const { return ExitInfo.ReturnType; }
408409

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ void XMLEscapingPrinter::printXML(StringRef Text) {
7070
OS << Text;
7171
}
7272

73-
void ResolvedRangeInfo::print(llvm::raw_ostream &OS) const {
73+
void ResolvedRangeInfo::print(llvm::raw_ostream &OS,
74+
const PrintOptions &PO) const {
7475
OS << "<Kind>";
7576
switch (Kind) {
7677
case RangeKind::SingleExpression: OS << "SingleExpression"; break;
@@ -87,7 +88,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) const {
8788

8889
if (auto Ty = getType()) {
8990
OS << "<Type>";
90-
Ty->print(OS);
91+
Ty->print(OS, PO);
9192
OS << "</Type>";
9293
switch(exit()) {
9394
case ExitState::Positive: OS << "<Exit>true</Exit>"; break;
@@ -147,7 +148,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) const {
147148
for (auto &RD : ReferencedDecls) {
148149
OS << "<Referenced>" << RD.VD->getBaseName() << "</Referenced>";
149150
OS << "<Type>";
150-
RD.Ty->print(OS);
151+
RD.Ty->print(OS, PO);
151152
OS << "</Type>\n";
152153
}
153154

test/IDE/range_info_basics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func testPropertyWrapper() {
497497
// CHECK27-NEXT: foo9(1, 2)</Content>
498498
// CHECK27-NEXT: <Type>Void</Type>
499499
// CHECK27-NEXT: <Context>swift_ide_test.(file).foo9(_:_:)</Context>
500-
// CHECK27-NEXT: <Referenced>foo9</Referenced><Type>(Int, Int) -> Int</Type>
500+
// CHECK27-NEXT: <Referenced>foo9</Referenced><Type>(_ a: Int, _ b: Int) -> Int</Type>
501501
// CHECK27-NEXT: <ASTNodes>2</ASTNodes>
502502
// CHECK27-NEXT: <end>
503503

test/IDE/range_info_expr.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func bar(x: C2 = .main) {}
109109
// CHECK-MTEE-EXPR-3-NEXT: <Content>{ escapable in
110110
// CHECK-MTEE-EXPR-3-NEXT: _ = escapable
111111
// CHECK-MTEE-EXPR-3-NEXT: }</Content>
112-
// CHECK-MTEE-EXPR-3-NEXT: <Type>(@escaping (Int) -> Void) -> ()</Type><Exit>false</Exit>
112+
// CHECK-MTEE-EXPR-3-NEXT: <Type>(_ escapable: @escaping (Int) -> Void) -> ()</Type><Exit>false</Exit>
113113
// CHECK-MTEE-EXPR-3-NEXT: <Context>swift_ide_test.(file).testWithoutActuallyEscaping(closure:)</Context>
114114
// CHECK-MTEE-EXPR-3-NEXT: <Declared>escapable</Declared><OutscopeReference>false</OutscopeReference>
115115
// CHECK-MTEE-EXPR-3-NEXT: <Referenced>escapable</Referenced><Type>(Int) -> Void</Type>

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,14 +1186,15 @@ printResult(CancellableResult<ResultType> Result,
11861186
}
11871187
}
11881188

1189-
static int printTypeContextInfo(
1190-
CancellableResult<TypeContextInfoResult> CancellableResult) {
1189+
static int
1190+
printTypeContextInfo(CancellableResult<TypeContextInfoResult> CancellableResult,
1191+
const PrintOptions &PO) {
11911192
return printResult<TypeContextInfoResult>(
1192-
CancellableResult, [](const TypeContextInfoResult &Result) {
1193+
CancellableResult, [&](const TypeContextInfoResult &Result) {
11931194
llvm::outs() << "-----BEGIN TYPE CONTEXT INFO-----\n";
11941195
for (auto resultItem : Result.Results) {
11951196
llvm::outs() << "- TypeName: ";
1196-
resultItem.ExpectedTy.print(llvm::outs());
1197+
resultItem.ExpectedTy.print(llvm::outs(), PO);
11971198
llvm::outs() << "\n";
11981199

11991200
llvm::outs() << " TypeUSR: ";
@@ -1224,11 +1225,10 @@ static int printTypeContextInfo(
12241225
});
12251226
}
12261227

1227-
static int doTypeContextInfo(const CompilerInvocation &InitInvok,
1228-
StringRef SourceFilename,
1229-
StringRef SecondSourceFileName,
1230-
StringRef CodeCompletionToken,
1231-
bool CodeCompletionDiagnostics) {
1228+
static int
1229+
doTypeContextInfo(const CompilerInvocation &InitInvok, StringRef SourceFilename,
1230+
StringRef SecondSourceFileName, StringRef CodeCompletionToken,
1231+
bool CodeCompletionDiagnostics, const PrintOptions &PO) {
12321232
return performWithCompletionLikeOperationParams(
12331233
InitInvok, SourceFilename, SecondSourceFileName, CodeCompletionToken,
12341234
CodeCompletionDiagnostics,
@@ -1241,24 +1241,25 @@ static int doTypeContextInfo(const CompilerInvocation &InitInvok,
12411241
Params.CompletionBuffer, Params.Offset, Params.DiagC,
12421242
/*CancellationFlag=*/nullptr,
12431243
[&](CancellableResult<TypeContextInfoResult> Result) {
1244-
ExitCode = printTypeContextInfo(Result);
1244+
ExitCode = printTypeContextInfo(Result, PO);
12451245
});
12461246
return ExitCode;
12471247
});
12481248
}
12491249

12501250
static int printConformingMethodList(
1251-
CancellableResult<ConformingMethodListResults> CancellableResult) {
1251+
CancellableResult<ConformingMethodListResults> CancellableResult,
1252+
const PrintOptions &PO) {
12521253
return printResult<ConformingMethodListResults>(
1253-
CancellableResult, [](const ConformingMethodListResults &Results) {
1254+
CancellableResult, [&](const ConformingMethodListResults &Results) {
12541255
auto Result = Results.Result;
12551256
if (!Result) {
12561257
return 0;
12571258
}
12581259
llvm::outs() << "-----BEGIN CONFORMING METHOD LIST-----\n";
12591260

12601261
llvm::outs() << "- TypeName: ";
1261-
Result->ExprType.print(llvm::outs());
1262+
Result->ExprType.print(llvm::outs(), PO);
12621263
llvm::outs() << "\n";
12631264

12641265
llvm::outs() << "- Members: ";
@@ -1275,7 +1276,7 @@ static int printConformingMethodList(
12751276
llvm::outs() << "\n";
12761277

12771278
llvm::outs() << " TypeName: ";
1278-
resultTy.print(llvm::outs());
1279+
resultTy.print(llvm::outs(), PO);
12791280
llvm::outs() << "\n";
12801281

12811282
StringRef BriefDoc = VD->getSemanticBriefComment();
@@ -1291,12 +1292,11 @@ static int printConformingMethodList(
12911292
});
12921293
}
12931294

1294-
static int
1295-
doConformingMethodList(const CompilerInvocation &InitInvok,
1296-
StringRef SourceFilename, StringRef SecondSourceFileName,
1297-
StringRef CodeCompletionToken,
1298-
bool CodeCompletionDiagnostics,
1299-
const std::vector<std::string> expectedTypeNames) {
1295+
static int doConformingMethodList(
1296+
const CompilerInvocation &InitInvok, StringRef SourceFilename,
1297+
StringRef SecondSourceFileName, StringRef CodeCompletionToken,
1298+
bool CodeCompletionDiagnostics,
1299+
const std::vector<std::string> expectedTypeNames, const PrintOptions &PO) {
13001300
SmallVector<const char *, 4> typeNames;
13011301
for (auto &name : expectedTypeNames)
13021302
typeNames.push_back(name.c_str());
@@ -1313,7 +1313,7 @@ doConformingMethodList(const CompilerInvocation &InitInvok,
13131313
Params.CompletionBuffer, Params.Offset, Params.DiagC, typeNames,
13141314
/*CancellationFlag=*/nullptr,
13151315
[&](CancellableResult<ConformingMethodListResults> Result) {
1316-
ExitCode = printConformingMethodList(Result);
1316+
ExitCode = printConformingMethodList(Result, PO);
13171317
});
13181318
return ExitCode;
13191319
});
@@ -2754,7 +2754,8 @@ static int doPrintExpressionTypes(const CompilerInvocation &InitInvok,
27542754
}
27552755

27562756
static int doPrintLocalTypes(const CompilerInvocation &InitInvok,
2757-
const std::vector<std::string> ModulesToPrint) {
2757+
const std::vector<std::string> ModulesToPrint,
2758+
const PrintOptions &PO) {
27582759
using NodeKind = Demangle::Node::Kind;
27592760

27602761
CompilerInvocation Invocation(InitInvok);
@@ -2775,8 +2776,6 @@ static int doPrintLocalTypes(const CompilerInvocation &InitInvok,
27752776

27762777
int ExitCode = 0;
27772778

2778-
PrintOptions Options = PrintOptions::printDeclarations();
2779-
27802779
for (StringRef ModuleName : ModulesToPrint) {
27812780
auto *M = getModuleByFullName(Context, ModuleName);
27822781
if (!M) {
@@ -2867,9 +2866,7 @@ static int doPrintLocalTypes(const CompilerInvocation &InitInvok,
28672866

28682867
llvm::outs() << remangled << "\n";
28692868

2870-
auto Options = PrintOptions::printDeclarations();
2871-
Options.PrintAccess = false;
2872-
LTD->print(llvm::outs(), Options);
2869+
LTD->print(llvm::outs(), PO);
28732870
llvm::outs() << "\n";
28742871
}
28752872
}
@@ -3410,8 +3407,7 @@ class ASTTypePrinter : public ASTWalker {
34103407
} // unnamed namespace
34113408

34123409
static int doPrintTypes(const CompilerInvocation &InitInvok,
3413-
StringRef SourceFilename,
3414-
bool FullyQualifiedTypes) {
3410+
StringRef SourceFilename, const PrintOptions &PO) {
34153411
CompilerInvocation Invocation(InitInvok);
34163412
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFilename);
34173413

@@ -3427,9 +3423,7 @@ static int doPrintTypes(const CompilerInvocation &InitInvok,
34273423
registerIDERequestFunctions(CI.getASTContext().evaluator);
34283424
CI.performSema();
34293425

3430-
PrintOptions Options = PrintOptions::printDeclarations();
3431-
Options.FullyQualifiedTypes = FullyQualifiedTypes;
3432-
ASTTypePrinter Printer(CI.getSourceMgr(), Options);
3426+
ASTTypePrinter Printer(CI.getSourceMgr(), PO);
34333427

34343428
CI.getMainModule()->walk(Printer);
34353429

@@ -3971,10 +3965,12 @@ class TypeReconstructWalker : public SourceEntityWalker {
39713965
llvm::raw_ostream &Stream;
39723966
llvm::DenseSet<ValueDecl *> SeenDecls;
39733967
llvm::SmallVector<DeclContext *, 2> NestedDCs;
3968+
const PrintOptions &PO;
39743969

39753970
public:
3976-
TypeReconstructWalker(ASTContext &Ctx, llvm::raw_ostream &Stream)
3977-
: Ctx(Ctx), Stream(Stream) {}
3971+
TypeReconstructWalker(ASTContext &Ctx, llvm::raw_ostream &Stream,
3972+
const PrintOptions &PO)
3973+
: Ctx(Ctx), Stream(Stream), PO(PO) {}
39783974

39793975
bool walkToDeclPre(Decl *D, CharSourceRange range) override {
39803976
if (auto *VD = dyn_cast<ValueDecl>(D)) {
@@ -4019,7 +4015,7 @@ class TypeReconstructWalker : public SourceEntityWalker {
40194015
Demangle::getTypeForMangling(Ctx, mangledName));
40204016
Stream << "type: ";
40214017
if (ReconstructedType) {
4022-
ReconstructedType->print(Stream);
4018+
ReconstructedType->print(Stream, PO);
40234019
} else {
40244020
Stream << "FAILURE";
40254021
}
@@ -4044,7 +4040,11 @@ class TypeReconstructWalker : public SourceEntityWalker {
40444040
}
40454041

40464042
if (TypeDecl *reDecl = Demangle::getTypeDeclForUSR(Ctx, USR)) {
4047-
PrintOptions POpts;
4043+
PrintOptions POpts = PO.clone();
4044+
POpts.TypeDefinitions = false;
4045+
POpts.VarInitializers = false;
4046+
POpts.FunctionDefinitions = false;
4047+
POpts.PrintExprs = false;
40484048
POpts.PreferTypeRepr = false;
40494049
POpts.PrintParameterSpecifiers = true;
40504050
reDecl->print(Stream, POpts);
@@ -4056,7 +4056,7 @@ class TypeReconstructWalker : public SourceEntityWalker {
40564056
};
40574057

40584058
static int doReconstructType(const CompilerInvocation &InitInvok,
4059-
StringRef SourceFilename) {
4059+
StringRef SourceFilename, const PrintOptions &PO) {
40604060
CompilerInvocation Invocation(InitInvok);
40614061
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFilename);
40624062
Invocation.getLangOptions().DisableAvailabilityChecking = false;
@@ -4080,15 +4080,14 @@ static int doReconstructType(const CompilerInvocation &InitInvok,
40804080
break;
40814081
}
40824082
assert(SF && "no source file?");
4083-
TypeReconstructWalker Walker(SF->getASTContext(), llvm::outs());
4083+
TypeReconstructWalker Walker(SF->getASTContext(), llvm::outs(), PO);
40844084
Walker.walk(SF);
40854085
return 0;
40864086
}
40874087

40884088
static int doPrintRangeInfo(const CompilerInvocation &InitInvok,
4089-
StringRef SourceFileName,
4090-
StringRef StartPos,
4091-
StringRef EndPos) {
4089+
StringRef SourceFileName, StringRef StartPos,
4090+
StringRef EndPos, const PrintOptions &PO) {
40924091
auto StartOp = parseLineCol(StartPos);
40934092
auto EndOp = parseLineCol(EndPos);
40944093
if (!StartOp.has_value() || !EndOp.has_value())
@@ -4127,7 +4126,7 @@ static int doPrintRangeInfo(const CompilerInvocation &InitInvok,
41274126
EndLineCol.second);
41284127
ResolvedRangeInfo Result = evaluateOrDefault(SF->getASTContext().evaluator,
41294128
RangeInfoRequest(RangeInfoOwner({SF, StartLoc, EndLoc})), ResolvedRangeInfo());
4130-
Result.print(llvm::outs());
4129+
Result.print(llvm::outs(), PO);
41314130
return 0;
41324131
}
41334132

@@ -4828,11 +4827,10 @@ int main(int argc, char *argv[]) {
48284827
llvm::errs() << "token name required\n";
48294828
return 1;
48304829
}
4831-
ExitCode = doTypeContextInfo(InitInvok,
4832-
options::SourceFilename,
4833-
options::SecondSourceFilename,
4834-
options::CodeCompletionToken,
4835-
options::CodeCompletionDiagnostics);
4830+
ExitCode = doTypeContextInfo(InitInvok, options::SourceFilename,
4831+
options::SecondSourceFilename,
4832+
options::CodeCompletionToken,
4833+
options::CodeCompletionDiagnostics, PrintOpts);
48364834
break;
48374835

48384836
case ActionType::PrintExpressionTypes:
@@ -4846,12 +4844,10 @@ int main(int argc, char *argv[]) {
48464844
llvm::errs() << "token name required\n";
48474845
return 1;
48484846
}
4849-
ExitCode = doConformingMethodList(InitInvok,
4850-
options::SourceFilename,
4851-
options::SecondSourceFilename,
4852-
options::CodeCompletionToken,
4853-
options::CodeCompletionDiagnostics,
4854-
options::ConformingMethodListExpectedTypes);
4847+
ExitCode = doConformingMethodList(
4848+
InitInvok, options::SourceFilename, options::SecondSourceFilename,
4849+
options::CodeCompletionToken, options::CodeCompletionDiagnostics,
4850+
options::ConformingMethodListExpectedTypes, PrintOpts);
48554851
break;
48564852

48574853
case ActionType::SyntaxColoring:
@@ -4892,7 +4888,7 @@ int main(int argc, char *argv[]) {
48924888
break;
48934889
}
48944890
case ActionType::PrintLocalTypes:
4895-
ExitCode = doPrintLocalTypes(InitInvok, options::ModuleToPrint);
4891+
ExitCode = doPrintLocalTypes(InitInvok, options::ModuleToPrint, PrintOpts);
48964892
break;
48974893

48984894
case ActionType::PrintModuleGroups:
@@ -4943,8 +4939,7 @@ int main(int argc, char *argv[]) {
49434939
}
49444940

49454941
case ActionType::PrintTypes:
4946-
ExitCode = doPrintTypes(InitInvok, options::SourceFilename,
4947-
options::FullyQualifiedTypes);
4942+
ExitCode = doPrintTypes(InitInvok, options::SourceFilename, PrintOpts);
49484943
break;
49494944

49504945
case ActionType::PrintComments:
@@ -4979,12 +4974,12 @@ int main(int argc, char *argv[]) {
49794974
options::USR);
49804975
break;
49814976
case ActionType::ReconstructType:
4982-
ExitCode = doReconstructType(InitInvok, options::SourceFilename);
4977+
ExitCode = doReconstructType(InitInvok, options::SourceFilename, PrintOpts);
49834978
break;
49844979
case ActionType::Range:
49854980
ExitCode = doPrintRangeInfo(InitInvok, options::SourceFilename,
49864981
options::LineColumnPair,
4987-
options::EndLineColumnPair);
4982+
options::EndLineColumnPair, PrintOpts);
49884983
break;
49894984
case ActionType::PrintIndexedSymbols:
49904985
if (options::ModuleToPrint.empty()) {

0 commit comments

Comments
 (0)