@@ -3955,10 +3955,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
3955
3955
StringRef FallbackStyleName,
3956
3956
StringRef Code, llvm::vfs::FileSystem *FS,
3957
3957
bool AllowUnknownOptions) {
3958
- if (!FS)
3959
- FS = llvm::vfs::getRealFileSystem ().get ();
3960
3958
FormatStyle Style = getLLVMStyle (guessLanguage (FileName, Code));
3961
-
3962
3959
FormatStyle FallbackStyle = getNoStyle ();
3963
3960
if (!getPredefinedStyle (FallbackStyleName, Style .Language , &FallbackStyle))
3964
3961
return make_string_error (" Invalid fallback style: " + FallbackStyleName);
@@ -3974,14 +3971,18 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
3974
3971
AllowUnknownOptions)) {
3975
3972
return make_string_error (" Error parsing -style: " + ec.message ());
3976
3973
}
3977
- if (Style .InheritsParentConfig ) {
3978
- ChildFormatTextToApply.emplace_back (
3979
- llvm::MemoryBuffer::getMemBuffer (StyleName, Source, false ));
3980
- } else {
3974
+
3975
+ if (!Style .InheritsParentConfig )
3981
3976
return Style ;
3982
- }
3977
+
3978
+ ChildFormatTextToApply.emplace_back (
3979
+ llvm::MemoryBuffer::getMemBuffer (StyleName, Source, false ));
3983
3980
}
3984
3981
3982
+ if (!FS)
3983
+ FS = llvm::vfs::getRealFileSystem ().get ();
3984
+ assert (FS);
3985
+
3985
3986
// User provided clang-format file using -style=file:path/to/format/file.
3986
3987
if (!Style .InheritsParentConfig &&
3987
3988
StyleName.starts_with_insensitive (" file:" )) {
@@ -4015,18 +4016,12 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
4015
4016
return Style ;
4016
4017
}
4017
4018
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;
4023
4019
SmallString<128 > Path (FileName);
4024
4020
if (std::error_code EC = FS->makeAbsolute (Path))
4025
4021
return make_string_error (EC.message ());
4026
4022
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 ;
4030
4025
4031
4026
auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
4032
4027
@@ -4040,9 +4035,14 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
4040
4035
}
4041
4036
};
4042
4037
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;
4043
4044
for (StringRef Directory = Path; !Directory.empty ();
4044
4045
Directory = llvm::sys::path::parent_path (Directory)) {
4045
-
4046
4046
auto Status = FS->status (Directory);
4047
4047
if (!Status ||
4048
4048
Status->getType () != llvm::sys::fs::file_type::directory_file) {
@@ -4055,50 +4055,51 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
4055
4055
llvm::sys::path::append (ConfigFile, F);
4056
4056
LLVM_DEBUG (llvm::dbgs () << " Trying " << ConfigFile << " ...\n " );
4057
4057
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) {
4071
4068
return make_string_error (" Error reading " + ConfigFile + " : " +
4072
4069
EC.message ());
4073
4070
}
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
+ }
4076
4076
4077
- if (!Style .InheritsParentConfig ) {
4078
- if (ChildFormatTextToApply.empty ())
4079
- return Style ;
4077
+ LLVM_DEBUG (llvm::dbgs ()
4078
+ << " Using configuration file " << ConfigFile << " \n " );
4080
4079
4080
+ if (!Style .InheritsParentConfig ) {
4081
+ if (!ChildFormatTextToApply.empty ()) {
4081
4082
LLVM_DEBUG (llvm::dbgs () << " Applying child configurations\n " );
4082
4083
applyChildFormatTexts (&Style );
4083
-
4084
- return Style ;
4085
4084
}
4085
+ return Style ;
4086
+ }
4086
4087
4087
- LLVM_DEBUG (llvm::dbgs () << " Inherits parent configuration\n " );
4088
+ LLVM_DEBUG (llvm::dbgs () << " Inherits parent configuration\n " );
4088
4089
4089
- // Reset inheritance of style
4090
- Style .InheritsParentConfig = false ;
4090
+ // Reset inheritance of style
4091
+ Style .InheritsParentConfig = false ;
4091
4092
4092
- ChildFormatTextToApply.emplace_back (std::move (*Text));
4093
+ ChildFormatTextToApply.emplace_back (std::move (*Text));
4093
4094
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 ;
4100
4100
}
4101
4101
}
4102
+
4102
4103
if (!UnsuitableConfigFiles.empty ()) {
4103
4104
return make_string_error (" Configuration file(s) do(es) not support " +
4104
4105
getLanguageName (Style .Language ) + " : " +
0 commit comments