-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-factor main() so that all options are handled by new class CcsmOptionsHandler Re-factor existing classes so that const reference to options object is used & retire macros SHOULD_INCLUDE_FUNCTION, etc. Fully encapsulate all options into MetricOptions class
- Loading branch information
1 parent
d7bf710
commit b52363d
Showing
28 changed files
with
441 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
/* | ||
Copyright 2015 John Bailey | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#include "CcsmOptionsHandler.hpp" | ||
#include "ccsm_ver.h" | ||
#include "MetricUnit.hpp" | ||
|
||
#include "llvm/Support/CommandLine.h" | ||
|
||
// Set up the command line options | ||
static llvm::cl::OptionCategory CCSMToolCategory("ccsm options"); | ||
|
||
static std::vector<std::string> ExcludeFunctionList; | ||
|
||
static llvm::cl::list<std::string, std::vector<std::string>> ExcludeFunctions( | ||
"exclude-function", | ||
llvm::cl::desc("Exclude specified function from the metrics"), | ||
llvm::cl::CommaSeparated, | ||
llvm::cl::ZeroOrMore, | ||
llvm::cl::location(ExcludeFunctionList)); | ||
|
||
static std::vector<std::string> ExcludeFileList; | ||
|
||
static llvm::cl::list<std::string, std::vector<std::string>> ExcludeFiles( | ||
"exclude-file", | ||
llvm::cl::desc("Exclude specified file from the metrics"), | ||
llvm::cl::CommaSeparated, | ||
llvm::cl::ZeroOrMore, | ||
llvm::cl::location(ExcludeFileList), | ||
llvm::cl::cat(CCSMToolCategory)); | ||
|
||
std::vector<std::string> IncludeInParentFileList; | ||
|
||
static llvm::cl::list<std::string, std::vector<std::string>> IncludeInParentFiles( | ||
"def-file", | ||
llvm::cl::desc("Specifies files whose contents should not be individually counted and should be included in the including file. Intended to help support the def file idiom"), | ||
llvm::cl::CommaSeparated, | ||
llvm::cl::ZeroOrMore, | ||
llvm::cl::location(IncludeInParentFileList), | ||
llvm::cl::cat(CCSMToolCategory)); | ||
|
||
static llvm::cl::opt<bool> UseShortNames( | ||
"output-short-names", | ||
llvm::cl::desc("Use short metric names in the output"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> UseAbsoluteFileNames( | ||
"output-absolute-fn", | ||
llvm::cl::desc("Use absolute filenames in the output"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> DumpTokens( | ||
"dump-tokens", | ||
llvm::cl::desc("Dump tokens as they are lexed"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> DumpAST( | ||
"dump-ast", | ||
llvm::cl::desc("Dump AST when it is processed"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> DumpFnMap( | ||
"dump-function-map", | ||
llvm::cl::desc("Dump the mapping of files/functions/locations"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> NoGlobal( | ||
"disable-global", | ||
llvm::cl::desc("Disable output of stats at the global level"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> NoFile( | ||
"disable-file", | ||
llvm::cl::desc("Disable output of stats at the file level"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> NoFunction( | ||
"disable-function", | ||
llvm::cl::desc("Disable output of stats at the function level"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> NoMethod( | ||
"disable-method", | ||
llvm::cl::desc("Disable output of stats at the method level"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<MetricDumpFormat_e> OutputFormat( | ||
"output-format", | ||
llvm::cl::desc("Format of output"), | ||
llvm::cl::values( | ||
clEnumValN(METRIC_DUMP_FORMAT_TSV, "tsv", "Tab Seperated Values"), | ||
clEnumValN(METRIC_DUMP_FORMAT_CSV, "csv", "Comma Separated Values"), | ||
clEnumValN(METRIC_DUMP_FORMAT_SPARSE_TREE, "sparsetree", "Sparse Tree (zero value nodes omitted)"), | ||
clEnumValN(METRIC_DUMP_FORMAT_TREE, "tree", "Tree Structure"), | ||
clEnumValEnd | ||
), | ||
llvm::cl::init(METRIC_DUMP_FORMAT_TREE), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
static llvm::cl::opt<bool> PrototypesAreFileScope( | ||
"prototypes-are-filescope", | ||
llvm::cl::desc("The prototype part of a function declaration should be included in the file scope metrics, not the function scope"), | ||
llvm::cl::init(false), | ||
llvm::cl::cat(CCSMToolCategory) | ||
); | ||
|
||
std::vector<std::string> OutputMetricList; | ||
|
||
static llvm::cl::list<std::string, std::vector<std::string>> OutputOnlyMetrics( | ||
"output-metrics", | ||
llvm::cl::desc("Only output the specified metrics"), | ||
llvm::cl::CommaSeparated, | ||
llvm::cl::ZeroOrMore, | ||
llvm::cl::location(OutputMetricList), | ||
llvm::cl::cat(CCSMToolCategory)); | ||
|
||
static llvm::cl::extrahelp MoreHelp("\nVersion: " GEN_VER_VERSION_STRING); | ||
|
||
CcsmOptionsHandler::CcsmOptionsHandler() : m_optionsParser(NULL), m_metricOptions( NULL ) | ||
{ | ||
} | ||
|
||
CcsmOptionsHandler::~CcsmOptionsHandler() | ||
{ | ||
delete(m_optionsParser); | ||
delete(m_metricOptions); | ||
} | ||
|
||
void CcsmOptionsHandler::ParseOptions(int argc, | ||
const char ** const argv ) | ||
{ | ||
m_metricOptions = new MetricOptions(&ExcludeFileList, &ExcludeFunctionList, &OutputMetricList, &IncludeInParentFileList); | ||
m_optionsParser = new clang::tooling::CommonOptionsParser(argc, argv, CCSMToolCategory); | ||
|
||
m_metricOptions->setDumpTokens(DumpTokens); | ||
m_metricOptions->setDumpAST(DumpAST); | ||
m_metricOptions->setUseShortNames(UseShortNames); | ||
m_metricOptions->setUseAbsoluteFileNames(UseAbsoluteFileNames); | ||
m_metricOptions->setPrototypesAreFileScope(PrototypesAreFileScope); | ||
m_metricOptions->setOutputFormat(OutputFormat); | ||
|
||
m_metricOptions->setOutputMetric(METRIC_UNIT_METHOD, !NoMethod); | ||
m_metricOptions->setOutputMetric(METRIC_UNIT_FUNCTION, !NoFunction); | ||
m_metricOptions->setOutputMetric(METRIC_UNIT_FILE, !NoFile); | ||
m_metricOptions->setOutputMetric(METRIC_UNIT_GLOBAL, !NoGlobal); | ||
|
||
m_metricOptions->setDumpFnMap(DumpFnMap); | ||
} | ||
|
||
const MetricOptions* CcsmOptionsHandler::getMetricOptions() const | ||
{ | ||
return m_metricOptions; | ||
} | ||
|
||
clang::tooling::CommonOptionsParser* CcsmOptionsHandler::getOptionsParser() const | ||
{ | ||
return m_optionsParser; | ||
} | ||
|
||
#include "clang/Frontend/TextDiagnosticPrinter.h" | ||
#include "clang/Driver/Driver.h" | ||
#include "clang/Driver/Compilation.h" | ||
#include "clang/Driver/Types.h" | ||
#include "clang/Basic/SourceManager.h" | ||
#include "clang/Tooling/CompilationDatabase.h" | ||
#include "clang/Tooling/CommonOptionsParser.h" | ||
#include "clang/Frontend/CompilerInstance.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "clang/Tooling/Tooling.h" | ||
#include <iostream> | ||
|
||
using namespace clang; | ||
using namespace clang::tooling; | ||
|
||
void processArgs(clang::tooling::CompilationDatabase& db, llvm::ArrayRef<std::string> SourcePaths, int argc, const char **argv) | ||
{ | ||
bool cppWarningDone = false; | ||
std::string Path = llvm::sys::fs::getMainExecutable(argv[0], (void*)(intptr_t)processArgs); | ||
std::string TripleStr = llvm::sys::getProcessTriple(); | ||
llvm::Triple T(TripleStr); | ||
for (const auto &SourcePath : SourcePaths) { | ||
std::string File(getAbsolutePath(SourcePath)); | ||
std::vector<CompileCommand> CompileCommandsForFile = db.getCompileCommands(File); | ||
for (CompileCommand &CompileCommand : CompileCommandsForFile) { | ||
std::vector<std::string> CommandLine = CompileCommand.CommandLine; | ||
std::vector<const char*> Args; | ||
for (const std::string &Str : CommandLine) | ||
{ | ||
Args.push_back(Str.c_str()); | ||
} | ||
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); | ||
TextDiagnosticPrinter *DiagClient = | ||
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); | ||
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); | ||
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); | ||
clang::driver::Driver TheDriver(Path, T.str(), Diags); | ||
TheDriver.setCheckInputsExist(false); | ||
std::unique_ptr<clang::driver::Compilation> C(TheDriver.BuildCompilation(Args)); | ||
const driver::JobList &Jobs = C->getJobs(); | ||
const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin()); | ||
const driver::ArgStringList &CCArgs = Cmd.getArguments(); | ||
std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation); | ||
CompilerInvocation::CreateFromArgs(*CI, | ||
const_cast<const char **>(CCArgs.data()), | ||
const_cast<const char **>(CCArgs.data()) + | ||
CCArgs.size(), | ||
Diags); | ||
|
||
if ((*CI).getLangOpts()->CPlusPlus && !cppWarningDone) | ||
{ | ||
std::cerr << "WARNING: Proper support for C++ language constructs is not currently implemented"; | ||
cppWarningDone = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
@file | ||
@brief | ||
@author John Bailey | ||
@copyright Copyright 2015 John Bailey | ||
@section LICENSE | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#if !defined( CCSM_OPTIONS_HANDLER_HPP ) | ||
#define CCSM_OPTIONS_HANDLER_HPP | ||
|
||
#include "clang/Tooling/CommonOptionsParser.h" | ||
#include "MetricOptions.hpp" | ||
|
||
class CcsmOptionsHandler { | ||
|
||
private: | ||
clang::tooling::CommonOptionsParser* m_optionsParser; | ||
MetricOptions* m_metricOptions; | ||
|
||
public: | ||
|
||
CcsmOptionsHandler(); | ||
|
||
void ParseOptions(int argc, const char ** const argv); | ||
|
||
const MetricOptions* getMetricOptions() const; | ||
clang::tooling::CommonOptionsParser* getOptionsParser() const; | ||
|
||
virtual ~CcsmOptionsHandler(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.