Skip to content

Commit 3791b3f

Browse files
authored
[clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (#74794)
1 parent 9810fe1 commit 3791b3f

File tree

2 files changed

+65
-70
lines changed

2 files changed

+65
-70
lines changed

clang/lib/Format/Format.cpp

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3955,10 +3955,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39553955
StringRef FallbackStyleName,
39563956
StringRef Code, llvm::vfs::FileSystem *FS,
39573957
bool AllowUnknownOptions) {
3958-
if (!FS)
3959-
FS = llvm::vfs::getRealFileSystem().get();
39603958
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
3961-
39623959
FormatStyle FallbackStyle = getNoStyle();
39633960
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
39643961
return make_string_error("Invalid fallback style: " + FallbackStyleName);
@@ -3974,14 +3971,18 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39743971
AllowUnknownOptions)) {
39753972
return make_string_error("Error parsing -style: " + ec.message());
39763973
}
3977-
if (Style.InheritsParentConfig) {
3978-
ChildFormatTextToApply.emplace_back(
3979-
llvm::MemoryBuffer::getMemBuffer(StyleName, Source, false));
3980-
} else {
3974+
3975+
if (!Style.InheritsParentConfig)
39813976
return Style;
3982-
}
3977+
3978+
ChildFormatTextToApply.emplace_back(
3979+
llvm::MemoryBuffer::getMemBuffer(StyleName, Source, false));
39833980
}
39843981

3982+
if (!FS)
3983+
FS = llvm::vfs::getRealFileSystem().get();
3984+
assert(FS);
3985+
39853986
// User provided clang-format file using -style=file:path/to/format/file.
39863987
if (!Style.InheritsParentConfig &&
39873988
StyleName.starts_with_insensitive("file:")) {
@@ -4015,18 +4016,12 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40154016
return Style;
40164017
}
40174018

4018-
// Reset possible inheritance
4019-
Style.InheritsParentConfig = false;
4020-
4021-
// Look for .clang-format/_clang-format file in the file's parent directories.
4022-
SmallString<128> UnsuitableConfigFiles;
40234019
SmallString<128> Path(FileName);
40244020
if (std::error_code EC = FS->makeAbsolute(Path))
40254021
return make_string_error(EC.message());
40264022

4027-
llvm::SmallVector<std::string, 2> FilesToLookFor;
4028-
FilesToLookFor.push_back(".clang-format");
4029-
FilesToLookFor.push_back("_clang-format");
4023+
// Reset possible inheritance
4024+
Style.InheritsParentConfig = false;
40304025

40314026
auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
40324027

@@ -4040,9 +4035,14 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40404035
}
40414036
};
40424037

4038+
// Look for .clang-format/_clang-format file in the file's parent directories.
4039+
llvm::SmallVector<std::string, 2> FilesToLookFor;
4040+
FilesToLookFor.push_back(".clang-format");
4041+
FilesToLookFor.push_back("_clang-format");
4042+
4043+
SmallString<128> UnsuitableConfigFiles;
40434044
for (StringRef Directory = Path; !Directory.empty();
40444045
Directory = llvm::sys::path::parent_path(Directory)) {
4045-
40464046
auto Status = FS->status(Directory);
40474047
if (!Status ||
40484048
Status->getType() != llvm::sys::fs::file_type::directory_file) {
@@ -4055,50 +4055,51 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40554055
llvm::sys::path::append(ConfigFile, F);
40564056
LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
40574057

4058-
Status = FS->status(ConfigFile.str());
4059-
4060-
if (Status &&
4061-
(Status->getType() == llvm::sys::fs::file_type::regular_file)) {
4062-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
4063-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
4064-
if (auto EC = Text.getError()) {
4065-
if (EC == ParseError::Unsuitable) {
4066-
if (!UnsuitableConfigFiles.empty())
4067-
UnsuitableConfigFiles.append(", ");
4068-
UnsuitableConfigFiles.append(ConfigFile);
4069-
continue;
4070-
}
4058+
Status = FS->status(ConfigFile);
4059+
if (!Status ||
4060+
Status->getType() != llvm::sys::fs::file_type::regular_file) {
4061+
continue;
4062+
}
4063+
4064+
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
4065+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
4066+
if (auto EC = Text.getError()) {
4067+
if (EC != ParseError::Unsuitable) {
40714068
return make_string_error("Error reading " + ConfigFile + ": " +
40724069
EC.message());
40734070
}
4074-
LLVM_DEBUG(llvm::dbgs()
4075-
<< "Using configuration file " << ConfigFile << "\n");
4071+
if (!UnsuitableConfigFiles.empty())
4072+
UnsuitableConfigFiles.append(", ");
4073+
UnsuitableConfigFiles.append(ConfigFile);
4074+
continue;
4075+
}
40764076

4077-
if (!Style.InheritsParentConfig) {
4078-
if (ChildFormatTextToApply.empty())
4079-
return Style;
4077+
LLVM_DEBUG(llvm::dbgs()
4078+
<< "Using configuration file " << ConfigFile << "\n");
40804079

4080+
if (!Style.InheritsParentConfig) {
4081+
if (!ChildFormatTextToApply.empty()) {
40814082
LLVM_DEBUG(llvm::dbgs() << "Applying child configurations\n");
40824083
applyChildFormatTexts(&Style);
4083-
4084-
return Style;
40854084
}
4085+
return Style;
4086+
}
40864087

4087-
LLVM_DEBUG(llvm::dbgs() << "Inherits parent configuration\n");
4088+
LLVM_DEBUG(llvm::dbgs() << "Inherits parent configuration\n");
40884089

4089-
// Reset inheritance of style
4090-
Style.InheritsParentConfig = false;
4090+
// Reset inheritance of style
4091+
Style.InheritsParentConfig = false;
40914092

4092-
ChildFormatTextToApply.emplace_back(std::move(*Text));
4093+
ChildFormatTextToApply.emplace_back(std::move(*Text));
40934094

4094-
// Breaking out of the inner loop, since we don't want to parse
4095-
// .clang-format AND _clang-format, if both exist. Then we continue the
4096-
// inner loop (parent directories) in search for the parent
4097-
// configuration.
4098-
break;
4099-
}
4095+
// Breaking out of the inner loop, since we don't want to parse
4096+
// .clang-format AND _clang-format, if both exist. Then we continue the
4097+
// outer loop (parent directories) in search for the parent
4098+
// configuration.
4099+
break;
41004100
}
41014101
}
4102+
41024103
if (!UnsuitableConfigFiles.empty()) {
41034104
return make_string_error("Configuration file(s) do(es) not support " +
41044105
getLanguageName(Style.Language) + ": " +

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
398398
};
399399

400400
// Returns true on error.
401-
static bool format(StringRef FileName) {
402-
if (!OutputXML && Inplace && FileName == "-") {
401+
static bool format(StringRef FileName, bool IsSTDIN) {
402+
if (!OutputXML && Inplace && IsSTDIN) {
403403
errs() << "error: cannot use -i when reading from stdin.\n";
404404
return false;
405405
}
@@ -423,7 +423,7 @@ static bool format(StringRef FileName) {
423423
if (InvalidBOM) {
424424
errs() << "error: encoding with unsupported byte order mark \""
425425
<< InvalidBOM << "\" detected";
426-
if (FileName != "-")
426+
if (!IsSTDIN)
427427
errs() << " in file '" << FileName << "'";
428428
errs() << ".\n";
429429
return true;
@@ -432,7 +432,7 @@ static bool format(StringRef FileName) {
432432
std::vector<tooling::Range> Ranges;
433433
if (fillRanges(Code.get(), Ranges))
434434
return true;
435-
StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
435+
StringRef AssumedFileName = IsSTDIN ? AssumeFileName : FileName;
436436
if (AssumedFileName.empty()) {
437437
llvm::errs() << "error: empty filenames are not allowed\n";
438438
return true;
@@ -544,28 +544,23 @@ static void PrintVersion(raw_ostream &OS) {
544544
}
545545

546546
// Dump the configuration.
547-
static int dumpConfig() {
548-
StringRef FileName;
547+
static int dumpConfig(bool IsSTDIN) {
549548
std::unique_ptr<llvm::MemoryBuffer> Code;
550-
if (FileNames.empty()) {
551-
// We can't read the code to detect the language if there's no
552-
// file name, so leave Code empty here.
553-
FileName = AssumeFileName;
554-
} else {
555-
// Read in the code in case the filename alone isn't enough to
556-
// detect the language.
549+
// We can't read the code to detect the language if there's no file name.
550+
if (!IsSTDIN) {
551+
// Read in the code in case the filename alone isn't enough to detect the
552+
// language.
557553
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
558554
MemoryBuffer::getFileOrSTDIN(FileNames[0]);
559555
if (std::error_code EC = CodeOrErr.getError()) {
560556
llvm::errs() << EC.message() << "\n";
561557
return 1;
562558
}
563-
FileName = (FileNames[0] == "-") ? AssumeFileName : FileNames[0];
564559
Code = std::move(CodeOrErr.get());
565560
}
566561
llvm::Expected<clang::format::FormatStyle> FormatStyle =
567-
clang::format::getStyle(Style, FileName, FallbackStyle,
568-
Code ? Code->getBuffer() : "");
562+
clang::format::getStyle(Style, IsSTDIN ? AssumeFileName : FileNames[0],
563+
FallbackStyle, Code ? Code->getBuffer() : "");
569564
if (!FormatStyle) {
570565
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
571566
return 1;
@@ -596,8 +591,11 @@ int main(int argc, const char **argv) {
596591
return 0;
597592
}
598593

594+
if (FileNames.empty())
595+
FileNames.push_back("-");
596+
599597
if (DumpConfig)
600-
return dumpConfig();
598+
return dumpConfig(FileNames[0] == "-");
601599

602600
if (!Files.empty()) {
603601
std::ifstream ExternalFileOfFiles{std::string(Files)};
@@ -610,11 +608,6 @@ int main(int argc, const char **argv) {
610608
errs() << "Clang-formating " << LineNo << " files\n";
611609
}
612610

613-
bool Error = false;
614-
if (FileNames.empty()) {
615-
Error = clang::format::format("-");
616-
return Error ? 1 : 0;
617-
}
618611
if (FileNames.size() != 1 &&
619612
(!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
620613
errs() << "error: -offset, -length and -lines can only be used for "
@@ -623,12 +616,13 @@ int main(int argc, const char **argv) {
623616
}
624617

625618
unsigned FileNo = 1;
619+
bool Error = false;
626620
for (const auto &FileName : FileNames) {
627621
if (Verbose) {
628622
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
629623
<< FileName << "\n";
630624
}
631-
Error |= clang::format::format(FileName);
625+
Error |= clang::format::format(FileName, FileName == "-");
632626
}
633627
return Error ? 1 : 0;
634628
}

0 commit comments

Comments
 (0)