Skip to content

Commit

Permalink
Add suppress warnings (#41)
Browse files Browse the repository at this point in the history
* Add suppressWarnings function
* Ensure suppressWarnings allows warnings from tools
* Should only suppress warnings from clang itself

Signed-off-by: Ajmal Sharif <asharif10@bloomberg.net>
  • Loading branch information
HaltCatchFire authored and ruoso committed Jul 31, 2019
1 parent b93f82f commit cbff9f8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
8 changes: 8 additions & 0 deletions include/clangmetatool/tool_application_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define INCLUDED_CLANGMETATOOL_MAIN_SUPPORT_H

#include <clang/Tooling/CompilationDatabase.h>
#include <clang/Tooling/Tooling.h>

#include <string>
#include <vector>
Expand All @@ -22,6 +23,13 @@ struct ToolApplicationSupport {
static void
verifyInstallation(const clang::tooling::CompilationDatabase &compilations,
const std::vector<std::string> &sourcePathList);

/**
* For the given tool, append an ArgumentsAdjuster that adds '-w',
* This will supress all of clang's warnings, but still allow the tool to
* output its own warnings.
*/
static void suppressWarnings(clang::tooling::ClangTool &tool);
};

} // namespace clangmetatool
Expand Down
10 changes: 10 additions & 0 deletions src/tool_application_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ void ToolApplicationSupport::verifyInstallation(
}
}

void ToolApplicationSupport::suppressWarnings(clang::tooling::ClangTool &tool) {
auto argAdjuster = [](const clang::tooling::CommandLineArguments &cliArgsIn,
llvm::StringRef unused) {
clang::tooling::CommandLineArguments cliArgsOut = cliArgsIn;
cliArgsOut.push_back("-w");
return cliArgsOut;
};
tool.appendArgumentsAdjuster(argAdjuster);
}

} // namespace clangmetatool

// ----------------------------------------------------------------------------
Expand Down
58 changes: 58 additions & 0 deletions t/029-tool-application-support.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
#include <gtest/gtest.h>

#include <clangmetatool/tool_application_support.h>
#include <clangmetatool/meta_tool_factory.h>
#include <clangmetatool/meta_tool.h>

#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/Tooling/Core/Replacement.h>
#include <clang/Tooling/Refactoring.h>
#include <clang/Frontend/TextDiagnosticBuffer.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/ErrorHandling.h>

Expand Down Expand Up @@ -115,3 +121,55 @@ TEST_F(ToolApplicationSupportTest, compilation_database)
"-p", buildDir}));
}

class MyTool {
private:
clang::CompilerInstance* ci;
clang::ast_matchers::MatchFinder *f;
public:
MyTool(clang::CompilerInstance* ci, clang::ast_matchers::MatchFinder *f)
:ci(ci), f(f) {}
void postProcessing
(std::map<std::string, clang::tooling::Replacements> &replacementsMap) {
auto &diagnosticsEngine = ci->getDiagnostics();
const auto id =
diagnosticsEngine.getCustomDiagID(clang::DiagnosticsEngine::Warning,
"oh noz, a thing happened!");
diagnosticsEngine.Report(id);
}
};

TEST(suppress_warnings, test)
{
llvm::cl::OptionCategory MyToolCategory("my-tool options");

int argc = 4;
const char* argv[] = {
"foo",
CMAKE_SOURCE_DIR "/t/data/029-tool-application-support/warnings.cpp",
"--",
"-xc++"
};

clang::tooling::CommonOptionsParser
optionsParser
( argc, argv,
MyToolCategory );
clang::tooling::RefactoringTool tool
( optionsParser.getCompilations(),
optionsParser.getSourcePathList());

clangmetatool::ToolApplicationSupport::suppressWarnings(tool);

clang::TextDiagnosticBuffer tdb;
tool.setDiagnosticConsumer(&tdb);

clangmetatool::MetaToolFactory< clangmetatool::MetaTool<MyTool> >
raf(tool.getReplacements());

int r = tool.runAndSave(&raf);

// Ensure we have the one warning output by the tool,
// but not the macro warning from clang
EXPECT_EQ(1, tdb.getNumWarnings());
EXPECT_EQ("oh noz, a thing happened!", tdb.warn_begin()->second);
}
1 change: 1 addition & 0 deletions t/data/029-tool-application-support/warnings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "warnings.h"
4 changes: 4 additions & 0 deletions t/data/029-tool-application-support/warnings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef MISSPELL_ME_FOR_WARNINGS
#define MISPELL_ME_FOR_WARNINGS

#endif // MIS(S)PELL_ME_FOR_WARNINGS
4 changes: 2 additions & 2 deletions t/include/clangmetatool-testconfig.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef INCLDUDED_CLANGMETATOOL_TESTCONFIG_H
#define INCLDUDED_CLANGMETATOOL_TESTCONFIG_H
#ifndef INCLUDED_CLANGMETATOOL_TESTCONFIG_H
#define INCLUDED_CLANGMETATOOL_TESTCONFIG_H

#define CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@"
#define CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@"
Expand Down

0 comments on commit cbff9f8

Please sign in to comment.