@@ -399,7 +399,8 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
399
399
};
400
400
401
401
// Returns true on error.
402
- static bool format (StringRef FileName, bool IsSTDIN) {
402
+ static bool format (StringRef FileName) {
403
+ const bool IsSTDIN = FileName == " -" ;
403
404
if (!OutputXML && Inplace && IsSTDIN) {
404
405
errs () << " error: cannot use -i when reading from stdin.\n " ;
405
406
return false ;
@@ -545,24 +546,25 @@ static void PrintVersion(raw_ostream &OS) {
545
546
}
546
547
547
548
// Dump the configuration.
548
- static int dumpConfig (bool IsSTDIN ) {
549
+ static int dumpConfig () {
549
550
std::unique_ptr<llvm::MemoryBuffer> Code;
550
-
551
- // `FileNames` must have at least "-" in it even if no file was specified.
552
- assert (!FileNames. empty ());
553
-
554
- // Read in the code in case the filename alone isn't enough to detect the
555
- // language.
556
- ErrorOr< std::unique_ptr<MemoryBuffer>> CodeOrErr =
557
- MemoryBuffer::getFileOrSTDIN (FileNames[ 0 ]) ;
558
- if (std::error_code EC = CodeOrErr. getError ()) {
559
- llvm::errs () << EC. message () << " \n " ;
560
- return 1 ;
551
+ // We can't read the code to detect the language if there's no file name.
552
+ if (!FileNames. empty ()) {
553
+ // Read in the code in case the filename alone isn't enough to detect the
554
+ // language.
555
+ ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
556
+ MemoryBuffer::getFileOrSTDIN (FileNames[ 0 ]);
557
+ if ( std::error_code EC = CodeOrErr. getError ()) {
558
+ llvm::errs () << EC. message () << " \n " ;
559
+ return 1 ;
560
+ }
561
+ Code = std::move (CodeOrErr. get ()) ;
561
562
}
562
- Code = std::move (CodeOrErr.get ());
563
-
564
563
llvm::Expected<clang::format::FormatStyle> FormatStyle =
565
- clang::format::getStyle (Style , IsSTDIN ? AssumeFileName : FileNames[0 ],
564
+ clang::format::getStyle (Style ,
565
+ FileNames.empty () || FileNames[0 ] == " -"
566
+ ? AssumeFileName
567
+ : FileNames[0 ],
566
568
FallbackStyle, Code ? Code->getBuffer () : " " );
567
569
if (!FormatStyle) {
568
570
llvm::errs () << llvm::toString (FormatStyle.takeError ()) << " \n " ;
@@ -682,11 +684,8 @@ int main(int argc, const char **argv) {
682
684
return 0 ;
683
685
}
684
686
685
- if (FileNames.empty ())
686
- FileNames.push_back (" -" );
687
-
688
687
if (DumpConfig)
689
- return dumpConfig (FileNames[ 0 ] == " - " );
688
+ return dumpConfig ();
690
689
691
690
if (!Files.empty ()) {
692
691
std::ifstream ExternalFileOfFiles{std::string (Files)};
@@ -699,7 +698,10 @@ int main(int argc, const char **argv) {
699
698
errs () << " Clang-formating " << LineNo << " files\n " ;
700
699
}
701
700
702
- if (FileNames.size () != 1 &&
701
+ if (FileNames.empty ())
702
+ return clang::format::format (" -" );
703
+
704
+ if (FileNames.size () > 1 &&
703
705
(!Offsets.empty () || !Lengths.empty () || !LineRanges.empty ())) {
704
706
errs () << " error: -offset, -length and -lines can only be used for "
705
707
" single file.\n " ;
@@ -709,14 +711,13 @@ int main(int argc, const char **argv) {
709
711
unsigned FileNo = 1 ;
710
712
bool Error = false ;
711
713
for (const auto &FileName : FileNames) {
712
- const bool IsSTDIN = FileName == " -" ;
713
- if (!IsSTDIN && isIgnored (FileName))
714
+ if (isIgnored (FileName))
714
715
continue ;
715
716
if (Verbose) {
716
717
errs () << " Formatting [" << FileNo++ << " /" << FileNames.size () << " ] "
717
718
<< FileName << " \n " ;
718
719
}
719
- Error |= clang::format::format (FileName, IsSTDIN );
720
+ Error |= clang::format::format (FileName);
720
721
}
721
722
return Error ? 1 : 0 ;
722
723
}
0 commit comments