Skip to content

Commit

Permalink
Closes #73 by finishing adding file redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
bright-tools committed Mar 2, 2016
1 parent f8d429a commit 7a58c2b
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 201 deletions.
245 changes: 123 additions & 122 deletions src/CcsmOptionsHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
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"
/*
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"
#include "clang/Basic/Version.h"

#include <iostream>

#include "clang/Basic/Version.h"

#include <iostream>

// Set up the command line options
static llvm::cl::OptionCategory CCSMToolCategory("ccsm options");

Expand Down Expand Up @@ -164,41 +164,42 @@ static llvm::cl::opt<std::string> OutputToFile(
llvm::cl::Optional,
llvm::cl::cat(CCSMToolCategory));

static llvm::cl::extrahelp MoreHelp("\nVersion: " GEN_VER_VERSION_STRING);

static llvm::cl::extrahelp MoreHelp("\nVersion: " GEN_VER_VERSION_STRING);

static void PrintVersion() {
std::cout << "CCSM (https://github.com/bright-tools/ccsm):" << '\n'
<< " CCSM version " << GEN_VER_VERSION_STRING << '\n'
<< " Built " << __DATE__ << " (" << __TIME__ << ").\n"
<< "Using " << clang::getClangFullVersion() << '\n';
}

CcsmOptionsHandler::CcsmOptionsHandler() : m_optionsParser(NULL), m_metricOptions( NULL )
{
llvm::cl::SetVersionPrinter(PrintVersion);
}

CcsmOptionsHandler::~CcsmOptionsHandler()
{
delete(m_optionsParser);
delete(m_metricOptions);
}

void CcsmOptionsHandler::ParseOptions(int argc,
const char ** const argv )
{
}

CcsmOptionsHandler::CcsmOptionsHandler() : m_optionsParser(NULL), m_metricOptions( NULL )
{
llvm::cl::SetVersionPrinter(PrintVersion);
}

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_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->setExcludeStdHeaders(ExcludeStandardHeaders);
m_metricOptions->setOutputFile(OutputToFile);

m_metricOptions->setPrototypesAreFileScope(PrototypesAreFileScope);
m_metricOptions->setOutputFormat(OutputFormat);
m_metricOptions->setExcludeStdHeaders(ExcludeStandardHeaders);
// TODO: Handle failure
m_metricOptions->setOutputFile(OutputToFile);

m_metricOptions->setOutputMetric(METRIC_UNIT_METHOD, !NoMethod);
m_metricOptions->setOutputMetric(METRIC_UNIT_FUNCTION, !NoFunction);
m_metricOptions->setOutputMetric(METRIC_UNIT_FILE, !NoFile);
Expand All @@ -207,72 +208,72 @@ void CcsmOptionsHandler::ParseOptions(int argc,
m_metricOptions->setDumpFnMap(DumpFnMap);

checkCompilerArgs(argv[0]);
}

const MetricOptions* CcsmOptionsHandler::getMetricOptions() const
{
return m_metricOptions;
}

clang::tooling::CommonOptionsParser* CcsmOptionsHandler::getOptionsParser() const
{
return m_optionsParser;
}

const std::set<std::string> c99_std_headers = { "assert.h", "inttypes.h", "signal.h", "stdlib.h",
"complex.h", "iso646.h", "stdarg.h", "string.h",
"ctype.h", "limits.h", "stdbool.h", "tgmath.h",
"errno.h", "locale.h", "stddef.h", "time.h",
"fenv.h", "math.h", "stdint.h", "wchar.h",
"float.h", "setjmp.h", "stdio.h", "wctype.h"};

const std::set<std::string> c11_std_headers = { "assert.h", "math.h", "stdlib.h",
"complex.h", "setjmp.h", "stdnoreturn.h",
"ctype.h", "signal.h", "string.h",
"errno.h", "stdalign.h", "tgmath.h",
"fenv.h", "stdarg.h", "threads.h",
"float.h", "stdatomic.h", "time.h",
"inttypes.h", "stdbool.h", "uchar.h",
"iso646.h", "stddef.h", "wchar.h",
"limits.h", "stdint.h", "wctype.h",
"locale.h", "stdio.h" };

const std::set<std::string> cpp_std_headers = { "algorithm", "fstream", "list", "regex", "tuple",
"array", "functional", "locale", "scoped_allocator", "type_traits",
"atomic", "future", "map", "set", "typeindex",
"bitset", "initializer_list", "memory", "sstream", "typeinfo",
"chrono", "iomanip", "mutex", "stack", "unordered_map",
"codecvt", "ios", "new", "stdexcept", "unordered_set",
"complex", "iosfwd", "numeric", "streambuf", "utility",
"condition_variable", "iostream", "ostream", "string", "valarray",
"deque", "istream", "queue", "strstream", "vector",
"exception", "iterator", "random", "system_error",
"forward_list", "limits", "ratio", "thread",
/* C++ headers for C library functions */
"cassert", "cinttypes", "csignal", "cstdio", "cwchar",
"ccomplex", "ciso646", "cstdalign", "cstdlib", "cwctype",
"cctype", "climits", "cstdarg", "cstring",
"cerrno", "clocale", "cstdbool", "ctgmath",
"cfenv", "cmath", "cstddef", "ctime",
"cfloat", "csetjmp", "cstdint", "cuchar"
};


}

const MetricOptions* CcsmOptionsHandler::getMetricOptions() const
{
return m_metricOptions;
}

clang::tooling::CommonOptionsParser* CcsmOptionsHandler::getOptionsParser() const
{
return m_optionsParser;
}

const std::set<std::string> c99_std_headers = { "assert.h", "inttypes.h", "signal.h", "stdlib.h",
"complex.h", "iso646.h", "stdarg.h", "string.h",
"ctype.h", "limits.h", "stdbool.h", "tgmath.h",
"errno.h", "locale.h", "stddef.h", "time.h",
"fenv.h", "math.h", "stdint.h", "wchar.h",
"float.h", "setjmp.h", "stdio.h", "wctype.h"};

const std::set<std::string> c11_std_headers = { "assert.h", "math.h", "stdlib.h",
"complex.h", "setjmp.h", "stdnoreturn.h",
"ctype.h", "signal.h", "string.h",
"errno.h", "stdalign.h", "tgmath.h",
"fenv.h", "stdarg.h", "threads.h",
"float.h", "stdatomic.h", "time.h",
"inttypes.h", "stdbool.h", "uchar.h",
"iso646.h", "stddef.h", "wchar.h",
"limits.h", "stdint.h", "wctype.h",
"locale.h", "stdio.h" };

const std::set<std::string> cpp_std_headers = { "algorithm", "fstream", "list", "regex", "tuple",
"array", "functional", "locale", "scoped_allocator", "type_traits",
"atomic", "future", "map", "set", "typeindex",
"bitset", "initializer_list", "memory", "sstream", "typeinfo",
"chrono", "iomanip", "mutex", "stack", "unordered_map",
"codecvt", "ios", "new", "stdexcept", "unordered_set",
"complex", "iosfwd", "numeric", "streambuf", "utility",
"condition_variable", "iostream", "ostream", "string", "valarray",
"deque", "istream", "queue", "strstream", "vector",
"exception", "iterator", "random", "system_error",
"forward_list", "limits", "ratio", "thread",
/* C++ headers for C library functions */
"cassert", "cinttypes", "csignal", "cstdio", "cwchar",
"ccomplex", "ciso646", "cstdalign", "cstdlib", "cwctype",
"cctype", "climits", "cstdarg", "cstring",
"cerrno", "clocale", "cstdbool", "ctgmath",
"cfenv", "cmath", "cstddef", "ctime",
"cfloat", "csetjmp", "cstdint", "cuchar"
};


void CcsmOptionsHandler::checkCompilerArgs(const char* const exeName)
{
analyseCompilerArgs(exeName);

if (m_usesCpp)
{
std::cerr << "WARNING: Proper support for C++ language constructs is not currently implemented";

if (m_usesCpp)
{
std::cerr << "WARNING: Proper support for C++ language constructs is not currently implemented";

}

if (m_metricOptions->getExcludeStdHeaders())
{
if (m_usesCpp)
{
ExcludeFileList.insert(ExcludeFileList.end(), cpp_std_headers.begin(), cpp_std_headers.end());
if (m_metricOptions->getExcludeStdHeaders())
{
if (m_usesCpp)
{
ExcludeFileList.insert(ExcludeFileList.end(), cpp_std_headers.begin(), cpp_std_headers.end());
}

if (m_usesC99)
Expand All @@ -286,7 +287,7 @@ void CcsmOptionsHandler::checkCompilerArgs(const char* const exeName)
}
}
}


#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Compilation.h"
Expand Down Expand Up @@ -343,19 +344,19 @@ void CcsmOptionsHandler::analyseCompilerArgs(const char* const exeName)
const_cast<const char **>(CCArgs.data()) +
CCArgs.size(),
Diags);

if ((*CI).getLangOpts()->CPlusPlus)
{
m_usesCpp = true;
}
else if ((*CI).getLangOpts()->C99)
{
m_usesC99 = true;
}
else if ((*CI).getLangOpts()->C11)
{
m_usesC11 = true;
}

if ((*CI).getLangOpts()->CPlusPlus)
{
m_usesCpp = true;
}
else if ((*CI).getLangOpts()->C99)
{
m_usesC99 = true;
}
else if ((*CI).getLangOpts()->C11)
{
m_usesC11 = true;
}
}
}
}
12 changes: 6 additions & 6 deletions src/FunctionLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TranslationUnitFunctionLocator* GlobalFunctionLocator::getLocatorFor( const std:
return ret_val;
}

void GlobalFunctionLocator::dump( std::ostream& p_out ) const
void GlobalFunctionLocator::dump() const
{
MainSrcToFnLocMap_t::const_iterator it;

Expand All @@ -59,8 +59,8 @@ void GlobalFunctionLocator::dump( std::ostream& p_out ) const
{
fileName = makeRelative(fileName);
}
p_out << "Translation Unit: " << fileName << std::endl;
it->second->dump( p_out );
m_options.getOutput() << "Translation Unit: " << fileName << std::endl;
it->second->dump();
}
}

Expand Down Expand Up @@ -108,22 +108,22 @@ void TranslationUnitFunctionLocator::addFunctionLocation(const clang::ASTContext
m_map[ hashVal ][ startLoc ] = endNamePair;
}

void TranslationUnitFunctionLocator::dump( std::ostream& p_out ) const
void TranslationUnitFunctionLocator::dump() const
{
SrcStartToFunctionMap_t::const_iterator it;

for( it = m_map.begin();
it != m_map.end();
it++ )
{
p_out << " File ID: " << it->first << std::endl;
m_options.getOutput() << " File ID: " << it->first << std::endl;
StartEndPair_t::const_iterator pit;

for( pit = it->second.begin();
pit != it->second.end();
pit++ )
{
p_out << " Function Definition: " << pit->second.second << " (" << pit->first.getRawEncoding() << "-" << pit->second.first.getRawEncoding() << ")" << std::endl;
m_options.getOutput() << " Function Definition: " << pit->second.second << " (" << pit->first.getRawEncoding() << "-" << pit->second.first.getRawEncoding() << ")" << std::endl;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/FunctionLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TranslationUnitFunctionLocator

std::string FindFunction(const clang::SourceManager& p_SourceManager, clang::SourceLocation& p_loc, clang::SourceLocation* p_end = NULL) const;

void dump( std::ostream& p_out ) const;
void dump() const;
};

/** Supports finding the location of functions across a number of translation units
Expand All @@ -85,7 +85,7 @@ class GlobalFunctionLocator
GlobalFunctionLocator(const MetricOptions& p_options);
TranslationUnitFunctionLocator* getLocatorFor(const std::string p_fileName);
virtual ~GlobalFunctionLocator();
void dump( std::ostream& p_out ) const;
void dump() const;
};

#endif
5 changes: 3 additions & 2 deletions src/MetricDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ void MetricDumper::dumpMetric(std::ostream& out, const MetricUnit* const p_unit,
}
}

void MetricDumper::dump(std::ostream& out, const MetricUnit* const p_topLevel, const MetricOptions& p_options)
void MetricDumper::dump(const MetricUnit* const p_topLevel, const MetricOptions& p_options)
{
std::string sep;
const MetricUnitType_e metricUnitType = p_topLevel->GetType();
const bool useShortNames = p_options.getUseShortNames();
const MetricDumpFormat_e p_fmt = p_options.getOutputFormat();
std::ostream& out = p_options.getOutput();

if (p_options.getOutputMetric(metricUnitType))
{
Expand Down Expand Up @@ -187,7 +188,7 @@ void MetricDumper::dump(std::ostream& out, const MetricUnit* const p_topLevel, c
unitIt != subUnits->end();
++unitIt)
{
dump(out, (*unitIt).second, p_options);
dump((*unitIt).second, p_options);
}
}

2 changes: 1 addition & 1 deletion src/MetricDumper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MetricDumper

static void dumpMetric(std::ostream& out, const MetricUnit* const p_unit, const MetricType_e p_metric, const MetricDumpFormat_e p_fmt, const std::string& p_sep, const bool p_recurse, const std::string& p_suffix, const bool p_useShortNames);
public:
static void dump(std::ostream& out, const MetricUnit* const p_topLevel, const MetricOptions& p_options );
static void dump(const MetricUnit* const p_topLevel, const MetricOptions& p_options );
};

#endif
Loading

0 comments on commit 7a58c2b

Please sign in to comment.