@@ -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 ) + " : " +
0 commit comments