[PATCH] D49890: Clang-Tidy Export Problem

2018-12-07 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad added a comment.

Hi @alexfh,
I have attached the compilation database for `MPlayer-1.3.0`.F7659818: 
compile_commands.json 
As can be seen, the paths are relative. To answer your comments I need more 
time. I do not remember the exact details. I will prepare a test as soon as 
possible. I did not use the test infrastructure, before. Therefore, I should 
learn about it.
Regards.


Repository:
  rCTE Clang Tools Extra

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D49890/new/

https://reviews.llvm.org/D49890



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-08-14 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad added a comment.

In https://reviews.llvm.org/D49890#1182556, @alexfh wrote:

> Could you describe the specific problem you're solving and provide an 
> example? As mentioned by others, a test would be very welcome as well.


Sorry for so much delay,  @alexfh. I didn't see your comment. I will describe 
in detail:
I wanted to do a source to source transformation on `MPlayer-1.3.0` source 
code. The transformation may require modification of many files and possibly 
repeated modifications in the headers files included in multiple `.c` files. 
Therefore, the changes should be serialized for each translation unit and 
stored in a `YAML` file. At the end, `clang-apply-replacements` will be called 
and transform the entire source code.
The problem is that `clang-tidy` expects a limited format for the compilation 
database. This is the format typically used when the build system generating 
the compilation database is `CMAKE`.  But `MPlayer` uses `Makefile`. Therefore, 
I had to use an external database generator, `Bear`.  In this case, the 
contents of the `YAML` files are OK. But it is not what is expected by 
`clang-tidy`.  `clang-tidy` requires every file path to be absolute, even the 
header files.
The problem (i.e., using relative paths) only arises when the fixes are 
`exported`. Not when they are applied `in-place`. I reused some of the code for 
the in-place case and did some modifications to it. The code is OK, at least 
for my case with `MPlayer`. A small change is still needed to support `merge 
conflicts` which can be brought from the `in-place fix` stuff. It seems that at 
the end the commanlities of the two cases should be put in a function. Then 
this function can be called from both places (i.e., the `in-place fix` and the 
`export fix`).
I am new to `Clang` and do not know what is needed for tests. I am looking 
forward to your reply.
Regards.




Comment at: ClangTidy.cpp:614
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)

JonasToth wrote:
> TheAhmad wrote:
> > Eugene.Zelenko wrote:
> > > Type is not obvious, so please don't use auto.
> > Hi, Eugene. Why line 352 uses auto?
> He means line 615 (`InitialWorkingDir`). The type of the variable can not be 
> deduced from reading the code.
> 
> The rule is, to write the type once. E.g. `llvm::make_unique(args)` 
> makes it clear, that the type is `MyType`, so you can use `auto` for the 
> variable.
> This is not the case for `InitialWorkingDir`.
Right. I agree. So `line 352` should not use `auto` either.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-07-28 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad updated this revision to Diff 157846.
TheAhmad marked 4 inline comments as done.
TheAhmad added a comment.

auto used at line 612.
auto removed at line 614.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890

Files:
  ClangTidy.cpp


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,56 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  auto Files = make_unique(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  llvm::ErrorOr InitialWorkingDir =
+  FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath =
+static_cast(Error.Message.FilePath);
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::make_pair(ErrorAbsoluteFilePath, 
AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::make_pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,56 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  auto Files = make_unique(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  llvm::ErrorOr InitialWorkingDir =
+  FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath =
+static_cast(Error.Message.FilePath);
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::make_pair(ErrorAbsoluteFilePath, AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::make_pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[PATCH] D49890: Clang-Tidy Export Problem

2018-07-27 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad added a comment.

> Also, tests.

What should I provide for tests?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-07-27 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad added a comment.

The most comprehensive fix is to support all possible valid compilation 
databases. The current fix is the fastest one.

> Can you just make clang-tidy respect the directory value?

This is exactly what I did. I prepended the directory value to the file value 
and stored the result in the exported file.
There was also problem with merges to the same location. What I did is similar 
to the code for `inplace fix` (starting at `line 133` of `ClangTidy.cpp`), 
except that I have not added the code for merge conflicts. The ultimate fix 
requires adding a new function and moving the shared code there. Then `inplace 
fix` and `export` will use that function.




Comment at: ClangTidy.cpp:614
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)

Eugene.Zelenko wrote:
> Type is not obvious, so please don't use auto.
Hi, Eugene. Why line 352 uses auto?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-07-27 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad updated this revision to Diff 157676.
TheAhmad marked 4 inline comments as done.
TheAhmad added a comment.

Used make_pair instead of constructor call.
Used unique_ptr.
Used named cast.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890

Files:
  ClangTidy.cpp


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,56 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  std::unique_ptr Files =
+  make_unique(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath =
+static_cast(Error.Message.FilePath);
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::make_pair(ErrorAbsoluteFilePath, 
AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::make_pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,56 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  std::unique_ptr Files =
+  make_unique(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath =
+static_cast(Error.Message.FilePath);
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::make_pair(ErrorAbsoluteFilePath, AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::make_pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 
___
cfe-commits mailing list

[PATCH] D49890: Clang-Tidy Export Problem

2018-07-27 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad updated this revision to Diff 157667.
TheAhmad marked an inline comment as done.
TheAhmad added a comment.

Added auto at line 612.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890

Files:
  ClangTidy.cpp


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,55 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  auto Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath = (StringRef)Error.Message.FilePath;
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::pair(
+  ErrorAbsoluteFilePath, AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,55 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  auto Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath = (StringRef)Error.Message.FilePath;
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::pair(
+  ErrorAbsoluteFilePath, AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-07-26 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad accepted this revision.
TheAhmad marked 3 inline comments as done.
TheAhmad added a comment.
This revision is now accepted and ready to land.

Thanks Eugene!


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-07-26 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad updated this revision to Diff 157623.
TheAhmad added a comment.

Ran Clang Format.
Redundant Braces Removed.
Used auto.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890

Files:
  ClangTidy.cpp


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,55 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  FileManager *Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath = (StringRef)Error.Message.FilePath;
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::pair(
+  ErrorAbsoluteFilePath, AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,55 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
+
+  FileManager *Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
   for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+if (!Error.BuildDirectory.empty())
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+
+SmallString<128> ErrorAbsoluteFilePath = (StringRef)Error.Message.FilePath;
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end()) {
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::pair(
+  ErrorAbsoluteFilePath, AbsoluteError));
+}
+
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+   Repl.getLength(), Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError AbsoluteError = SingleErrors.find(File)->second;
+if (SingleErrors.find(File) == SingleErrors.end())
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+
+AbsoluteError.Fix.insert(std::pair(File, Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49890: Clang-Tidy Export Problem

2018-07-26 Thread Ahmad Nouralizadeh via Phabricator via cfe-commits
TheAhmad created this revision.
TheAhmad added a reviewer: alexfh.
TheAhmad added a project: clang-tools-extra.

Hi.
Clang tidy has problem with compile command databases that do not use 
necassarily absolute file paths. This is common in many projects that use 
makefiles as the build system, whose databases are usually generated using 
Bear. Therefore, I reused some of the code of the in-place fix and revised it 
to generate a YAML export file. The diff is OK, except that the merge conflicts 
are not added, right now.
Thanks!


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49890

Files:
  ClangTidy.cpp


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,58 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
-  for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+
+  FileManager *Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
+  for (const ClangTidyError  : Errors) {
+if (!Error.BuildDirectory.empty()) {
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+}
+
+SmallString<128> ErrorAbsoluteFilePath = (StringRef)Error.Message.FilePath;
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end())
+{
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  
SingleErrors.insert(std::pair(ErrorAbsoluteFilePath, 
AbsoluteError));
+}
+   
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+ Repl.getLength(),
+ Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();
+Replacements Repls = FileAndReplacements.second;
+
+ClangTidyError  AbsoluteError = SingleErrors.find(File)->second;   
+if (SingleErrors.find(File) == SingleErrors.end())
+{
+  llvm::report_fatal_error("Cannot find the containing ClangTidyError.");
+}
+AbsoluteError.Fix.insert(std::pair(File,Repls));
+
+tooling::Diagnostic Diag = AbsoluteError;
 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
   }
 


Index: ClangTidy.cpp
===
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -608,8 +608,58 @@
 raw_ostream ) {
   TranslationUnitDiagnostics TUD;
   TUD.MainSourceFile = MainFilePath;
-  for (const auto  : Errors) {
-tooling::Diagnostic Diag = Error;
+
+  FileManager *Files = new FileManager(FileSystemOptions());
+  vfs::FileSystem  = *Files->getVirtualFileSystem();
+  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
+  if (!InitialWorkingDir)
+llvm::report_fatal_error("Cannot get current working path.");
+
+  llvm::StringMap FileReplacements;
+  llvm::StringMap SingleErrors;
+  for (const ClangTidyError  : Errors) {
+if (!Error.BuildDirectory.empty()) {
+  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
+}
+
+SmallString<128> ErrorAbsoluteFilePath = (StringRef)Error.Message.FilePath;
+Files->makeAbsolutePath(ErrorAbsoluteFilePath);
+if (SingleErrors.find(ErrorAbsoluteFilePath) == SingleErrors.end())
+{
+  ClangTidyError AbsoluteError = Error;
+  AbsoluteError.Message.FilePath = ErrorAbsoluteFilePath.str();
+  AbsoluteError.Fix.clear();
+  SingleErrors.insert(std::pair(ErrorAbsoluteFilePath, AbsoluteError));
+}
+	
+for (const auto  : Error.Fix) {
+  for (const auto  : FileAndReplacements.second) {
+SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
+Files->makeAbsolutePath(FixAbsoluteFilePath);
+
+tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
+		  Repl.getLength(),
+		  Repl.getReplacementText());
+
+FileReplacements[R.getFilePath()].add(R);
+  }
+}
+
+FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
+  }
+
+  for (const auto  : FileReplacements) {
+StringRef File = FileAndReplacements.first();