@@ -399,7 +399,8 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
399399};
400400
401401// Returns true on error.
402- static bool format (StringRef FileName, bool IsSTDIN) {
402+ static bool format (StringRef FileName) {
403+ const bool IsSTDIN = FileName == " -" ;
403404 if (!OutputXML && Inplace && IsSTDIN) {
404405 errs () << " error: cannot use -i when reading from stdin.\n " ;
405406 return false ;
@@ -545,24 +546,25 @@ static void PrintVersion(raw_ostream &OS) {
545546}
546547
547548// Dump the configuration.
548- static int dumpConfig (bool IsSTDIN ) {
549+ static int dumpConfig () {
549550 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 ()) ;
561562 }
562- Code = std::move (CodeOrErr.get ());
563-
564563 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 ],
566568 FallbackStyle, Code ? Code->getBuffer () : " " );
567569 if (!FormatStyle) {
568570 llvm::errs () << llvm::toString (FormatStyle.takeError ()) << " \n " ;
@@ -682,11 +684,8 @@ int main(int argc, const char **argv) {
682684 return 0 ;
683685 }
684686
685- if (FileNames.empty ())
686- FileNames.push_back (" -" );
687-
688687 if (DumpConfig)
689- return dumpConfig (FileNames[ 0 ] == " - " );
688+ return dumpConfig ();
690689
691690 if (!Files.empty ()) {
692691 std::ifstream ExternalFileOfFiles{std::string (Files)};
@@ -699,7 +698,10 @@ int main(int argc, const char **argv) {
699698 errs () << " Clang-formating " << LineNo << " files\n " ;
700699 }
701700
702- if (FileNames.size () != 1 &&
701+ if (FileNames.empty ())
702+ return clang::format::format (" -" );
703+
704+ if (FileNames.size () > 1 &&
703705 (!Offsets.empty () || !Lengths.empty () || !LineRanges.empty ())) {
704706 errs () << " error: -offset, -length and -lines can only be used for "
705707 " single file.\n " ;
@@ -709,14 +711,13 @@ int main(int argc, const char **argv) {
709711 unsigned FileNo = 1 ;
710712 bool Error = false ;
711713 for (const auto &FileName : FileNames) {
712- const bool IsSTDIN = FileName == " -" ;
713- if (!IsSTDIN && isIgnored (FileName))
714+ if (isIgnored (FileName))
714715 continue ;
715716 if (Verbose) {
716717 errs () << " Formatting [" << FileNo++ << " /" << FileNames.size () << " ] "
717718 << FileName << " \n " ;
718719 }
719- Error |= clang::format::format (FileName, IsSTDIN );
720+ Error |= clang::format::format (FileName);
720721 }
721722 return Error ? 1 : 0 ;
722723}
0 commit comments