[cfe-dev] how clang-tidy finds clang-format file

Adam Cieszkiel via cfe-dev cfe-dev at lists.llvm.org
Fri Jul 21 18:25:58 PDT 2017


Hello again,

I took a look at the code and based on my research in version 4.0 there is
no code to apply formatting for fixes.
It was added in the following commit:
*[clang-tidy] Format code around applied fixes*

Summary:
Add -format option (disabled by default for now) to trigger formatting
of replacements.


https://github.com/llvm-mirror/clang-tools-extra/commit/58b93f8657f1cce97417d954eacfaa8546aaa949

In this commit: *[clang-tidy] Make format style customizable:*
https://github.com/llvm-mirror/clang-tools-extra/commit/de666a7753f9e6d554e24ae46cf86173cad64314

There was added documentation which was misleading because format could not
be applied - it was only found but not used:

docs/clang-tidy/index.rst
<https://github.com/llvm-mirror/clang-tools-extra/commit/de666a7753f9e6d554e24ae46cf86173cad64314#diff-f0891010033dd964657f9771b68abf0a>
:
+    -style=<string>              -
+                                   Fallback style for reformatting after
inserting fixes
+                                   if there is no clang-format config file
found.

When I added the fragment of the missing code in version 4.0 and recompiled
from source it started to work as expected.

This was the missing part in 'clang-tidy/ClangTidy.cpp in finish method of
the ErrorReporter class':

+        if (llvm::Expected<tooling::Replacements> FormattedReplacements =
+                format::formatReplacements(Code, *Replacements, *Style)) {
+          Replacements = std::move(FormattedReplacements);
+          if (!Replacements)
+            llvm_unreachable("!Replacements");
+        } else {
+          llvm::errs() << llvm::toString(FormattedReplacements.takeError())
+                       << ". Skipping formatting.\n";
+        }

Please confirm my findings if you would be that kind.

Thank you in advance.

Best regards,
Adam.


On Fri, Jul 21, 2017 at 1:33 PM, Adam Cieszkiel <acieszkiel at gmail.com>
wrote:

> Hello,
>
> Thank you for the response.
>
> I tried 'run-clang-tidy.py' and it started to work but I had to add one
> additional line to the script: 'invocation.append('-style=file')' so it
> takes .clang-format from the current directory otherwise it used some
> default formatting:
>
>  84 def apply_fixes(args, tmpdir):
>  85   """Calls clang-apply-fixes on a given directory. Deletes the dir
> when done."""
>  86   invocation = [args.clang_apply_replacements_binary]
>  87   if args.format:
>  88     invocation.append('-format')
> * 89     invocation.append('-style=file')*
>  90   invocation.append(tmpdir)
>  91   subprocess.call(invocation)
>  92   shutil.rmtree(tmpdir)
>
> Thanks for that.
>
> Still...
> I do not know why it does not work just without using 'clang-apply-replacements'
> as an intermediate step and it does not apply .clang-format.
>
> Can it be a bug in the version 4.0?
> Do you have any other ideas how I could get it working? Or someone who
> could help?
>
> I will try maybe to build it from source on a different platform.
> In the last resort I will have to take a look at the source code and check.
>
> Thank you in advance.
>
> Best regards,
> Adam.
>
> On Fri, Jul 21, 2017 at 1:28 AM, Piotr Padlewski <
> piotr.padlewski at gmail.com> wrote:
>
>> This probably not gonna solve the issue, but have you tried
>> run-clang-tidy.py? (extra/clang-tidy/tools/run-clang-tidy.py). It export
>> the fixes and applies them automatically, and runs on multiple threads.
>>
>> Piotr
>>
>> 2017-07-20 14:47 GMT-07:00 Adam Cieszkiel <acieszkiel at gmail.com>:
>>
>>> Hello,
>>>
>>> Thank you for the answer.
>>>
>>> It was my first guess and I was surprised that it did not work.
>>>
>>> I have to use '-export-fixes=' so it works. Then the fixes are written
>>> into json file and 'clang-apply-replacements -format -style=file' does the
>>> job.
>>> But it does want to work without this exporting.
>>>
>>> In version 5.0 I just use option '-format-style=file' and it uses
>>> .clang-format file in the closest parent directory as described in the
>>> documentation:
>>> https://clang.llvm.org/extra/clang-tidy/index.html
>>>
>>> But in version 4.0 there is only option "-style" which seems to not
>>> offer such option and does not work with .clang-format from the folder I
>>> run clang-tidy.
>>>
>>> -style=<string>              -                                 Fallback style for reformatting after inserting fixes                                 if there is no clang-format config file found.
>>>
>>>
>>> I am not sure why it it like that.
>>>
>>> Best regards,
>>> Adam.
>>>
>>>
>>>
>>>
>>> On Thu, Jul 20, 2017 at 11:18 PM, Piotr Padlewski <
>>> piotr.padlewski at gmail.com> wrote:
>>>
>>>> Hi Adam,
>>>> it should look for ".clang-format" from the place where you run
>>>> clang-tidy. I hope it helps.
>>>>
>>>> Piotr
>>>>
>>>> 2017-07-20 13:25 GMT-07:00 Adam Cieszkiel via cfe-dev <
>>>> cfe-dev at lists.llvm.org>:
>>>>
>>>>> Hello,
>>>>>
>>>>> I have a question:
>>>>> Do you know maybe how to make clang-tidy in version 4.0 use formatting
>>>>> style for fixes (-fix) from .clang-format file?
>>>>>
>>>>> I tried in many different ways but I failed.
>>>>>
>>>>> Documentation mentions 'clang-format config file to be found' but
>>>>> anywhere I put it, it is still not used by clang-tidy also if I put in in
>>>>> location pointed by '-p' option.
>>>>>
>>>>> http://releases.llvm.org/4.0.0/tools/clang/tools/extra/docs/
>>>>> clang-tidy/index.html
>>>>>
>>>>> -style=<string>              -                                 Fallback style for reformatting after inserting fixes                                 if there is no clang-format config file found.
>>>>>
>>>>> I have no idea where I should put .clang-format file so it could be
>>>>> used by clang-tidy for applying clang-tidy fixes.
>>>>>
>>>>> Thank you in advance.
>>>>>
>>>>> _______________________________________________
>>>>> cfe-dev mailing list
>>>>> cfe-dev at lists.llvm.org
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>>>>
>>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170722/51892292/attachment.html>


More information about the cfe-dev mailing list