Skip to content

Remove swift-indent tool #76577

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

Merged
merged 1 commit into from
Sep 21, 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
2 changes: 0 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ provide greater clarity to contributors wanting to add new documentation.
- [Testing.md](/docs/Testing.md):
TODO: Consider splitting into a how-to guide on writing a new test case
and an explanation for how the compiler is tested.
- [SwiftIndent.md](/docs/SwiftIndent.md):
TODO: Unclear if this is intended to be an explanation or a reference guide.
- [Random.md](/docs/Random.md): Stub.

### Archive
Expand Down
56 changes: 0 additions & 56 deletions docs/SwiftIndent.md

This file was deleted.

1 change: 0 additions & 1 deletion include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ class Driver {
SwiftDependencyTool, // swift-dependency-tool
SwiftLLVMOpt, // swift-llvm-opt
AutolinkExtract, // swift-autolink-extract
SwiftIndent, // swift-indent
SymbolGraph, // swift-symbolgraph
APIDigester, // swift-api-digester
CacheTool, // swift-cache-tool
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Option/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace options {
DoesNotAffectIncrementalBuild = (1 << 8),
AutolinkExtractOption = (1 << 9),
ModuleWrapOption = (1 << 10),
SwiftIndentOption = (1 << 11),
// 1 << 11 was previously `SwiftIndentOption`
ArgumentIsPath = (1 << 12),
ModuleInterfaceOption = (1 << 13),
SupplementaryOutput = (1 << 14),
Expand Down
20 changes: 8 additions & 12 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ def AutolinkExtractOption : OptionFlag;
// The option should be accepted by swift -modulewrap
def ModuleWrapOption : OptionFlag;

// The option should be accepted by swift-indent
def SwiftIndentOption : OptionFlag;

// The option should not be accepted by the driver.
def NoDriverOption : OptionFlag;

Expand Down Expand Up @@ -218,7 +215,6 @@ def driver_mode : Joined<["--"], "driver-mode=">, Flags<[HelpHidden]>,

def help : Flag<["-", "--"], "help">,
Flags<[FrontendOption, AutolinkExtractOption, ModuleWrapOption,
SwiftIndentOption,
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
HelpText<"Display available options">;
def h : Flag<["-"], "h">, Alias<help>;
Expand Down Expand Up @@ -268,8 +264,8 @@ def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,

def o : JoinedOrSeparate<["-"], "o">,
Flags<[FrontendOption, AutolinkExtractOption, ModuleWrapOption,
NoInteractiveOption, SwiftIndentOption, ArgumentIsPath,
SwiftAPIDigesterOption, CacheInvariant]>,
NoInteractiveOption, ArgumentIsPath, SwiftAPIDigesterOption,
CacheInvariant]>,
HelpText<"Write output to <file>">, MetaVarName<"<file>">;

def j : JoinedOrSeparate<["-"], "j">, Flags<[DoesNotAffectIncrementalBuild]>,
Expand Down Expand Up @@ -1146,28 +1142,28 @@ def AssertConfig : Separate<["-"], "assert-config">,
def code_formatting_Group : OptionGroup<"<code formatting options>">;

def use_tabs : Flag<["-"], "use-tabs">, Group<code_formatting_Group>,
Flags<[NoInteractiveOption, NoBatchOption, SwiftIndentOption]>,
Flags<[NoInteractiveOption, NoBatchOption]>,
HelpText<"Use tabs for indentation.">;

def indent_switch_case : Flag<["-"], "indent-switch-case">,
Group<code_formatting_Group>,
Flags<[NoInteractiveOption, NoBatchOption, SwiftIndentOption]>,
Flags<[NoInteractiveOption, NoBatchOption]>,
HelpText<"Indent cases in switch statements.">;

def in_place : Flag<["-"], "in-place">, Group<code_formatting_Group>,
Flags<[NoInteractiveOption, NoBatchOption, SwiftIndentOption]>,
Flags<[NoInteractiveOption, NoBatchOption]>,
HelpText<"Overwrite input file with formatted file.">;

def tab_width : Separate<["-"], "tab-width">, Group<code_formatting_Group>,
Flags<[NoInteractiveOption, NoBatchOption, SwiftIndentOption]>,
Flags<[NoInteractiveOption, NoBatchOption]>,
HelpText<"Width of tab character.">, MetaVarName<"<n>">;

def indent_width : Separate<["-"], "indent-width">, Group<code_formatting_Group>,
Flags<[NoInteractiveOption, NoBatchOption, SwiftIndentOption]>,
Flags<[NoInteractiveOption, NoBatchOption]>,
HelpText<"Number of characters to indent.">, MetaVarName<"<n>">;

def line_range : Separate<["-"], "line-range">, Group<code_formatting_Group>,
Flags<[NoInteractiveOption, NoBatchOption, SwiftIndentOption]>,
Flags<[NoInteractiveOption, NoBatchOption]>,
HelpText<"<start line>:<end line>. Formats a range of lines (1-based). "
"Can only be used with one input file.">, MetaVarName<"<n:n>">;

Expand Down
2 changes: 0 additions & 2 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ void Driver::parseDriverKind(ArrayRef<const char *> Args) {
.Case("swift-dependency-tool", DriverKind::SwiftDependencyTool)
.Case("swift-llvm-opt", DriverKind::SwiftLLVMOpt)
.Case("swift-autolink-extract", DriverKind::AutolinkExtract)
.Case("swift-indent", DriverKind::SwiftIndent)
.Case("swift-symbolgraph-extract", DriverKind::SymbolGraph)
.Case("swift-api-digester", DriverKind::APIDigester)
.Case("swift-cache-tool", DriverKind::CacheTool)
Expand Down Expand Up @@ -3108,7 +3107,6 @@ void Driver::printHelp(bool ShowHidden) const {
case DriverKind::SwiftDependencyTool:
case DriverKind::SwiftLLVMOpt:
case DriverKind::AutolinkExtract:
case DriverKind::SwiftIndent:
case DriverKind::SymbolGraph:
case DriverKind::APIDigester:
case DriverKind::CacheTool:
Expand Down
8 changes: 0 additions & 8 deletions lib/DriverTool/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ extern int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
extern int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr);

/// Run 'swift-indent'
extern int swift_indent_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr);

/// Run 'swift-symbolgraph-extract'
extern int swift_symbolgraph_extract_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr);
Expand Down Expand Up @@ -389,10 +385,6 @@ static int run_driver(StringRef ExecName,
return autolink_extract_main(
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv),
argv[0], (void *)(intptr_t)getExecutablePath);
case Driver::DriverKind::SwiftIndent:
return swift_indent_main(
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv),
argv[0], (void *)(intptr_t)getExecutablePath);
case Driver::DriverKind::SymbolGraph:
return swift_symbolgraph_extract_main(TheDriver.getArgsWithoutProgramNameAndDriverMode(argv), argv[0], (void *)(intptr_t)getExecutablePath);
case Driver::DriverKind::APIDigester:
Expand Down
190 changes: 0 additions & 190 deletions lib/DriverTool/swift_indent_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,193 +76,3 @@ class FormatterDocument {
return *SM.getLLVMSourceMgr().getMemoryBuffer(BufferID);
}
};

class SwiftIndentInvocation {
private:
std::string MainExecutablePath;
std::string OutputFilename = "-";
std::vector<std::string> InputFilenames;
CodeFormatOptions FormatOptions;
bool InPlace = false;
std::vector<std::string> LineRanges;

bool parseLineRange(StringRef Input, unsigned &FromLine, unsigned &ToLine) {
std::pair<StringRef, StringRef> LineRange = Input.split(":");
return LineRange.first.getAsInteger(0, FromLine) ||
LineRange.second.getAsInteger(0, ToLine);
}

public:
SwiftIndentInvocation(const std::string &ExecPath)
: MainExecutablePath(ExecPath) {}

const std::string &getOutputFilename() { return OutputFilename; }

const std::vector<std::string> &getInputFilenames() { return InputFilenames; }

const std::vector<std::string> &getLineRanges() { return LineRanges; }

int parseArgs(ArrayRef<const char *> Args, DiagnosticEngine &Diags) {
using namespace options;

std::unique_ptr<llvm::opt::OptTable> Table = createSwiftOptTable();
unsigned MissingIndex;
unsigned MissingCount;
llvm::opt::InputArgList ParsedArgs =
Table->ParseArgs(Args, MissingIndex, MissingCount, SwiftIndentOption);
if (MissingCount) {
Diags.diagnose(SourceLoc(), diag::error_missing_arg_value,
ParsedArgs.getArgString(MissingIndex), MissingCount);
return 1;
}

if (ParsedArgs.getLastArg(OPT_use_tabs))
FormatOptions.UseTabs = true;

if (ParsedArgs.getLastArg(OPT_indent_switch_case))
FormatOptions.IndentSwitchCase = true;

if (ParsedArgs.getLastArg(OPT_in_place))
InPlace = true;

if (const Arg *A = ParsedArgs.getLastArg(OPT_tab_width))
if (StringRef(A->getValue()).getAsInteger(10, FormatOptions.TabWidth))
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(ParsedArgs), A->getValue());

if (const Arg *A = ParsedArgs.getLastArg(OPT_indent_width))
if (StringRef(A->getValue()).getAsInteger(10, FormatOptions.IndentWidth))
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(ParsedArgs), A->getValue());

for (const Arg *A : ParsedArgs.filtered(OPT_line_range))
LineRanges.push_back(A->getValue());

if (ParsedArgs.hasArg(OPT_UNKNOWN)) {
for (const Arg *A : ParsedArgs.filtered(OPT_UNKNOWN)) {
Diags.diagnose(SourceLoc(), diag::error_unknown_arg,
A->getAsString(ParsedArgs));
}
return true;
}

if (ParsedArgs.getLastArg(OPT_help)) {
std::string ExecutableName =
llvm::sys::path::stem(MainExecutablePath).str();
Table->printHelp(llvm::outs(), ExecutableName.c_str(),
"Swift Format Tool", options::SwiftIndentOption, 0,
/*ShowAllAliases*/false);
return 1;
}

for (const Arg *A : ParsedArgs.filtered(OPT_INPUT)) {
InputFilenames.push_back(A->getValue());
}

if (const Arg *A = ParsedArgs.getLastArg(OPT_o)) {
OutputFilename = A->getValue();
}

return 0;
}

/// Formats a filename and returns false if successful, true otherwise.
bool format(StringRef Filename, DiagnosticEngine &Diags) {
auto ErrOrBuf = llvm::MemoryBuffer::getFileOrSTDIN(Filename);
if (!ErrOrBuf) {
Diags.diagnose(SourceLoc(), diag::error_no_such_file_or_directory,
Filename);
return true;
}
std::unique_ptr<llvm::MemoryBuffer> Code = std::move(ErrOrBuf.get());
if (Code->getBufferSize() == 0) {
// Assume empty files are formatted successfully.
return false;
}
FormatterDocument Doc(std::move(Code));
if (LineRanges.empty()) {
LineRanges.push_back("1:" + std::to_string(UINT_MAX));
}

std::string Output = Doc.memBuffer().getBuffer().str();
for (unsigned Range = 0; Range < LineRanges.size(); ++Range) {
unsigned FromLine;
unsigned ToLine;
if (parseLineRange(LineRanges[Range], FromLine, ToLine)) {
Diags.diagnose(SourceLoc(), diag::error_formatting_invalid_range);
return true;
}
if (FromLine > ToLine) {
Diags.diagnose(SourceLoc(), diag::error_formatting_invalid_range);
return true;
}
for (unsigned Line = FromLine; Line <= ToLine; ++Line) {
size_t Offset = getOffsetOfLine(Line, Output);
ssize_t Length = getOffsetOfLine(Line + 1, Output) - 1 - Offset;
if (Length < 0)
break;

std::string Formatted =
Doc.reformat(LineRange(Line, 1), FormatOptions).second;
if (Formatted.find_first_not_of(" \t\v\f", 0) == StringRef::npos)
Formatted = "";

Output.replace(Offset, Length, Formatted);
Doc.updateCode(llvm::MemoryBuffer::getMemBufferCopy(Output));
}
if (Filename == "-" || (!InPlace && OutputFilename == "-")) {
llvm::outs() << Output;
return false;
}
std::error_code EC;
StringRef Destination;
if (InPlace)
Destination = Filename;
else
Destination = OutputFilename;
llvm::raw_fd_ostream out(Destination, EC, llvm::sys::fs::OF_None);
if (out.has_error() || EC) {
Diags.diagnose(SourceLoc(), diag::error_opening_output, Filename,
EC.message());
out.clear_error();
return true;
}
out << Output;
}
return false;
}
};

int swift_indent_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr) {
CompilerInstance Instance;
PrintingDiagnosticConsumer PDC;
Instance.addDiagnosticConsumer(&PDC);

SwiftIndentInvocation Invocation(
llvm::sys::fs::getMainExecutable(Argv0, MainAddr));

DiagnosticEngine &Diags = Instance.getDiags();
if (Invocation.parseArgs(Args, Diags) != 0)
return EXIT_FAILURE;

std::vector<std::string> InputFiles = Invocation.getInputFilenames();
unsigned NumInputFiles = InputFiles.size();
if (NumInputFiles == 0) {
// Read source code from standard input.
Invocation.format("-", Diags);
} else if (NumInputFiles == 1) {
Invocation.format(InputFiles[0], Diags);
} else {
if (!Invocation.getLineRanges().empty()) {
// We don't support formatting file ranges for multiple files.
Instance.getDiags().diagnose(SourceLoc(),
diag::error_formatting_multiple_file_ranges);
return EXIT_FAILURE;
}
for (unsigned i = 0; i < NumInputFiles; ++i)
Invocation.format(InputFiles[i], Diags);
}

return EXIT_SUCCESS;
}
1 change: 0 additions & 1 deletion lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,6 @@ static void printSingleFrontendOpt(llvm::opt::OptTable &table, options::ID id,
if (table.getOption(id).hasFlag(options::FrontendOption) ||
table.getOption(id).hasFlag(options::AutolinkExtractOption) ||
table.getOption(id).hasFlag(options::ModuleWrapOption) ||
table.getOption(id).hasFlag(options::SwiftIndentOption) ||
table.getOption(id).hasFlag(options::SwiftSymbolGraphExtractOption) ||
table.getOption(id).hasFlag(options::SwiftAPIDigesterOption)) {
auto name = StringRef(table.getOptionName(id));
Expand Down
Loading