Skip to content

Commit

Permalink
Updates #69 by adding c++ warning
Browse files Browse the repository at this point in the history
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
bright-tools committed Dec 30, 2015
1 parent d7bf710 commit b52363d
Show file tree
Hide file tree
Showing 28 changed files with 441 additions and 261 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set(LLVM_USED_LIBS clangTooling clangBasic clangAST)
set(SOURCES
ccsm.cpp
ccsm_ver.h
CcsmOptionsHandler.hpp
CcsmOptionsHandler.cpp
MetricMatcher.hpp
MetricMatcher.cpp
MetricUnit.hpp
Expand Down
249 changes: 249 additions & 0 deletions src/CcsmOptionsHandler.cpp
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;
}
}
}
}
47 changes: 47 additions & 0 deletions src/CcsmOptionsHandler.hpp
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
4 changes: 2 additions & 2 deletions src/FunctionLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <iostream>

GlobalFunctionLocator::GlobalFunctionLocator(MetricOptions& p_options) : m_options(p_options)
GlobalFunctionLocator::GlobalFunctionLocator(const MetricOptions& p_options) : m_options(p_options)
{
}

Expand Down Expand Up @@ -68,7 +68,7 @@ GlobalFunctionLocator::~GlobalFunctionLocator()
}
}

TranslationUnitFunctionLocator::TranslationUnitFunctionLocator(MetricOptions& p_options) : m_options(p_options)
TranslationUnitFunctionLocator::TranslationUnitFunctionLocator(const MetricOptions& p_options) : m_options(p_options)
{
}

Expand Down
8 changes: 4 additions & 4 deletions src/FunctionLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class TranslationUnitFunctionLocator
/** Map of file IDs and associated function position/name data */
SrcStartToFunctionMap_t m_map;
/** Reference to the options selected by the user */
MetricOptions& m_options;
const MetricOptions& m_options;

public:
TranslationUnitFunctionLocator(MetricOptions& p_options);
TranslationUnitFunctionLocator(const MetricOptions& p_options);


void addFunctionLocation( const clang::ASTContext* const p_context, const std::string& p_name, const clang::FunctionDecl * const p_func );
Expand All @@ -79,10 +79,10 @@ class GlobalFunctionLocator
MainSrcToFnLocMap_t m_map;

/** Reference to the options selected by the user */
MetricOptions& m_options;
const MetricOptions& m_options;

public:
GlobalFunctionLocator(MetricOptions& p_options);
GlobalFunctionLocator(const MetricOptions& p_options);
TranslationUnitFunctionLocator* getLocatorFor(const std::string p_fileName);
virtual ~GlobalFunctionLocator();
void dump( std::ostream& p_out ) const;
Expand Down
8 changes: 4 additions & 4 deletions src/MetricASTConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "MetricASTConsumer.hpp"
#include "clang/Basic/SourceManager.h"

MetricASTConsumer::MetricASTConsumer(clang::CompilerInstance &CI, MetricUnit* p_topUnit, MetricOptions* p_options, GlobalFunctionLocator* p_fnLocator ) :
MetricASTConsumer::MetricASTConsumer(clang::CompilerInstance &CI, MetricUnit* p_topUnit, const MetricOptions& p_options, GlobalFunctionLocator* p_fnLocator ) :
m_compilerInstance( CI ),
m_options( p_options ),
m_topUnit( p_topUnit ),
Expand Down Expand Up @@ -59,8 +59,8 @@ void MetricASTConsumer::HandleTranslationUnit(clang::ASTContext &Context)
{
std::string fileName = it->first->getName();

if( SHOULD_INCLUDE_FILE( m_options, fileName ) &&
!(m_options->isDefFile( fileName )))
if( m_options.ShouldIncludeFile( fileName ) &&
!(m_options.isDefFile( fileName )))
{
MetricUnit* fileUnit = m_topUnit->getSubUnit(fileName, METRIC_UNIT_FILE);
fileUnit->setProcessed( METRIC_UNIT_PROCESS_AST);
Expand All @@ -70,7 +70,7 @@ void MetricASTConsumer::HandleTranslationUnit(clang::ASTContext &Context)
delete( visitor );
}

MetricPPConsumer::MetricPPConsumer(MetricUnit* p_topUnit, MetricOptions* p_options, GlobalFunctionLocator* p_fnLocator, const bool p_expanded)
MetricPPConsumer::MetricPPConsumer(MetricUnit* p_topUnit, const MetricOptions& p_options, GlobalFunctionLocator* p_fnLocator, const bool p_expanded)
: m_options( p_options ),
m_topUnit( p_topUnit ),
m_fnLocator( p_fnLocator ),
Expand Down
Loading

0 comments on commit b52363d

Please sign in to comment.