Re: [cfe-users] Redirecting clang tooling errors

2016-08-06 Thread Lucas Soltic via cfe-users
Considering that there is no other viable possibility, for now I have gone with 
the following:

using llvm::Twine;
using llvm::SmallString;
using llvm::IntrusiveRefCntPtr;
using llvm::MemoryBuffer;
using clang::FileManager;
using clang::PCHContainerOperations;
using clang::FileSystemOptions;
using clang::tooling::ToolInvocation;
using clang::tooling::FileContentMappings;

static std::vector
getSyntaxOnlyToolArgs(const Twine &ToolName,
  const std::vector &ExtraArgs,
  StringRef FileName) {
  std::vector Args;
  Args.push_back(ToolName.str());
  Args.push_back("-fsyntax-only");
  Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
  Args.push_back(FileName.str());
  return Args;
}

bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
   const Twine &FileName, clang::DiagnosticConsumer& 
diagConsumer)
{
  const std::vector args;
  const Twine toolName = "clang-tool";
  std::shared_ptr PCHContainerOps = 
std::make_shared();
  const FileContentMappings virtualMappedFiles = FileContentMappings();
  
  SmallString<16> fileNameStorage;
  StringRef fileNameRef = FileName.toNullTerminatedStringRef(fileNameStorage);
  IntrusiveRefCntPtr OverlayFileSystem
= new clang::vfs::OverlayFileSystem(clang::vfs::getRealFileSystem());
  IntrusiveRefCntPtr inMemoryFileSystem
= new clang::vfs::InMemoryFileSystem;
  OverlayFileSystem->pushOverlay(inMemoryFileSystem);
  IntrusiveRefCntPtr files(new FileManager(FileSystemOptions(), 
OverlayFileSystem));
  ToolInvocation invocation(getSyntaxOnlyToolArgs(toolName, args, fileNameRef),
ToolAction, files.get(), 
std::move(PCHContainerOps));
  invocation.setDiagnosticConsumer(&diagConsumer);
  
  SmallString<1024> codeStorage;
  inMemoryFileSystem->addFile(fileNameRef, 0,
  
MemoryBuffer::getMemBuffer(Code.toNullTerminatedStringRef(codeStorage)));
  
  for (auto &filenameWithContent : virtualMappedFiles) {
inMemoryFileSystem->addFile(filenameWithContent.first, 0,

MemoryBuffer::getMemBuffer(filenameWithContent.second));
  }
  
  return invocation.run();
}

It is basically a copy & past of runToolOnCode() but with the added parameter 
for diagnostic consumer. In the end it was not too much code to duplicate 
thanks to ToolInvocation::setDiagnosticConsumer() already existing.

Best regards,
L. Soltic

> Le 4 août 2016 à 20:55, Lucas Soltic via cfe-users  
> a écrit :
> 
> Hello,
> 
> I am trying to redirect the output emitted when running a tool through 
> clang::tooling::runToolOnCode() to a buffer or string instead of stderr 
> (llvm::errs()). I'm using clangTooling from release 3.9.
> 
> When looking at clangTooling code and following the execution flow, I have 
> found the following:
> clang::tooling::runToolOnCode()
> calls clang::tooling::runToolOnCodeWithArgs()
> which calls clang::tooling::ToolInvocation::run()
> which contains the following :
> TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
> DiagnosticsEngine Diagnostics(
> IntrusiveRefCntPtr(new DiagnosticIDs()), &*DiagOpts,
> DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
> 
> So at this point I guess I'm stuck because everything is redirected to stderr…
> Did I miss something or is there really this limitation?
> 
> I also thought of redirecting stderr to somewhere else but… I can't see how 
> it will fit my needs as in the end I want to call 
> clang::tooling::runToolOnCode() on different files in parallel, all of it in 
> the same process ; so I'll get stderr mixed with output from several 
> executions. The best solution would obviously being able to provide the 
> DiagnosticConsumer but at the moment everything looks hardcoded.
> 
> 
> Best regards,
> L. Soltic
> 
> ___
> cfe-users mailing list
> cfe-users@lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users

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


Re: [cfe-users] -Wunreachable-code warnings can no longer be suppressed?

2016-08-06 Thread David Blaikie via cfe-users
On Fri, Aug 5, 2016 at 3:56 PM Chris Peterson via cfe-users <
cfe-users@lists.llvm.org> wrote:

> I suppressed a -Wunreachable-code warning in Firefox earlier this year
> [1] by adding extra parentheses, as suggested by Xcode's clang on OS X:
>
> objdir-osx/dom/bindings/TestJSImplGenBinding.cpp:47639:20: note: silence
> by adding parentheses to mark code as explicitly dead
> if (false && !CallerSubsumes(temp)) {
>  ^
>  /* DISABLES CODE */ ()
>
> This is generated C++ code in Firefox, so changing the code generator to
> emit the extra parentheses was easier than complicating the code
> generator's logic for this particular case.
>
> Unfortunately, this Firefox warning is back [2] because clang 3.9 on
> Linux no longer recognizes the parentheses suppression. Is this an
> intentional change to -Wunreachable-code or a regression? I don't see
> the warning string "silence by adding parentheses to mark code as
> explicitly dead" in the clang code on GitHub [3], but I see a few clang
> tests that appear to expect that warning string.
>

The string is here:
https://github.com/llvm-mirror/clang/blob/master/include/clang/Basic/DiagnosticSemaKinds.td#L499

Should still work. Nearest I could test seems to:

blaikie@blaikie-linux:/tmp/dbginfo$ cat suppress.cpp
void f1();
bool f2(int);
void f3(int temp) {
  if (false && !f2(temp))
f1();
}
blaikie@blaikie-linux:/tmp/dbginfo$ clang++-tot suppress.cpp
-Wunreachable-code -fsyntax-only
suppress.cpp:5:5: warning: code will never be executed [-Wunreachable-code]
f1();
^~
suppress.cpp:4:16: note: silence by adding parentheses to mark code as
explicitly dead
  if (false && !f2(temp))
   ^
   /* DISABLES CODE */ ( )
suppress.cpp:4:17: warning: code will never be executed [-Wunreachable-code]
  if (false && !f2(temp))
^~
suppress.cpp:4:7: note: silence by adding parentheses to mark code as
explicitly dead
  if (false && !f2(temp))
  ^
  /* DISABLES CODE */ ( )
2 warnings generated.

Then successfully suppressed:

blaikie@blaikie-linux:/tmp/dbginfo$ cat suppress.cpp
void f1();
bool f2(int);
void f3(int temp) {
  if ((false) && !f2(temp))
f1();
}
blaikie@blaikie-linux:/tmp/dbginfo$ clang++-tot suppress.cpp
-Wunreachable-code -fsyntax-only
blaikie@blaikie-linux:/tmp/dbginfo$

That's with Clang from trunk/current SVN/git/etc. Could you provide a small
example that fails (warns/doesn't suppress) with 3.9/ToT but succeeds
(successfully suppresses the warning) with earlier?



>
> thanks,
> chris
>
> [1] https://bugzilla.mozilla.org/show_bug.cgi?id=1223265
> [2] https://bugzilla.mozilla.org/show_bug.cgi?id=1291397
> [3] https://github.com/llvm-mirror/clang
> ___
> cfe-users mailing list
> cfe-users@lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users