[cfe-dev] Fwd: Running Clang-Tidy on a Large Project

Ahmad Nouralizadeh via cfe-dev cfe-dev at lists.llvm.org
Mon Jul 23 13:07:56 PDT 2018


Hi
This is what I did, and it worked:
--- ClangTidy.cpp 2017-05-17 19:09:47.000000000 +0430
+++ ClangTidyRevised.cpp 2018-07-24 00:31:31.219938476 +0430
@@ -575,9 +575,44 @@
                         raw_ostream &OS) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
-  for (const auto &Error : Errors) {
-    tooling::Diagnostic Diag = Error;
+  FileManager *Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem &FileSystem = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+    llvm::report_fatal_error("Cannot get current working path.");
+
+  for (const ClangTidyError &Error : Errors) {
+    if (!Error.BuildDirectory.empty()) {
+      FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+    }
+
+    ClangTidyError AbsoluteError = Error;
+    SmallString<128> ErrorAbsoluteFilePath =
(StringRef)Error.Message.FilePath;
+    Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+    AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+    AbsoluteError.Fix.clear();
+
+    for (const auto &FileAndReplacements : Error.Fix) {
+      StringRef File = FileAndReplacements.first();
+      Replacements Repls;
+
+      for (const auto &Repl : FileAndReplacements.second) {
+        SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+        Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+        tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+                                   Repl.getLength(),
+                                   Repl.getReplacementText());
+        Repls.add(R);
+      }
+
AbsoluteError.Fix.insert(std::pair<StringRef,Replacements>(File,Repls));
+    }
+
+    tooling::Diagnostic Diag = AbsoluteError;
     TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
+
+    // Return to the initial directory to correctly resolve next Error.
+    FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
   }

   yaml::Output YAML(OS);


On Fri, Jul 20, 2018 at 10:26 PM, Ahmad Nouralizadeh <
ahmadnouralizadeh at gmail.com> wrote:

> I revised the compile commands database. But there is still problem with
> header files. Their path is relative in yaml file. I will try to change
> clang-tidy source code in order to fix the problem.
>
> On Fri, Jul 20, 2018 at 7:59 PM, Ahmad Nouralizadeh <
> ahmadnouralizadeh at gmail.com> wrote:
>
>> Many Thanks! You helped me pinpoint the problem.
>> The problem is with the compile commands database. In fact, clang-tidy
>> expects the database format to be a little bit restricted (which is the
>> real cause of the problem). The input paths to build commands in MPlayer
>> are all relative (determined by the Makefile) as can be seen here:
>> ...
>>             "-I/usr/local/lib/clang/3.9.0/include/",
>>             "-fpie",
>>             "-DPIC",
>>             "-D_REENTRANT",
>>             "-DZLIB_CONST",
>>             "-o",
>>             "libavformat/cafdec.o",
>>             "libavformat/cafdec.c"
>>         ],
>>         "directory": "/home/ahmad/Programs/MPlayer-1.3.0/ffmpeg",
>>         "file": "/home/ahmad/Programs/MPlayer-
>> 1.3.0/ffmpeg/libavformat/cafdec.c"
>> ...
>> This is part of the commands database generated using Bear with the file
>> entry revised to be absolute (I did that). The problem is still there.
>> Because the last two lines of the compile command arguments are relative
>> paths:
>> "libavformat/cafdec.o"
>> "libavformat/cafdec.c"
>> which should be changed to:
>> "/home/ahmad/Programs/MPlayer-1.3.0/ffmpeg/libavformat/cafdec.o"
>> "/home/ahmad/Programs/MPlayer-1.3.0/ffmpeg/libavformat/cafdec.c"
>> It seems that the fastest way to fix the problem is to postprocess the
>> commands database and make all paths absolute.
>>
>>
>> On Fri, Jul 20, 2018 at 11:53 AM, Miklos Vajna <vmiklos at vmiklos.hu>
>> wrote:
>>
>>> Hi,
>>>
>>> On Thu, Jul 19, 2018 at 11:04:21PM +0430, Ahmad Nouralizadeh <
>>> ahmadnouralizadeh at gmail.com> wrote:
>>> > path is recreated in that case. It can be seen at
>>> "clang-extra-tools-source/
>>> > clang-tidy/ClangTidy.cpp:line131" (clang-extra-tools-6.0.0):
>>> > ...
>>> >
>>> > SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
>>> >
>>> > Files.makeAbsolutePath(FixAbsoluteFilePath);
>>> > ...
>>> > But the same operation is not performed when the information is
>>> exported to
>>> > yaml, which is done when I run "run-clang-tidy" script with "-fix"
>>> option.
>>>
>>> Sounds like if you submit a patch to fix this, it would makes sense. (I
>>> haven't tried run-clang-tidy with -fix myself; what I usually do is
>>> invoking clang-tidy directly with -export-fixes=, then
>>> clang-apply-replacements, but in theory this is the same.)
>>>
>>> Perhaps the reason why nobody hit this before is the usage of relative
>>> paths; I think cmake just writes absolute paths in the compile commands
>>> db, so you don't hit the codepath where making the paths absolute
>>> matters.
>>>
>>> Regards,
>>>
>>> Miklos
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180724/7f4e26a0/attachment.html>


More information about the cfe-dev mailing list