Skip to content

Commit

Permalink
Merge pull request #6 from ahueck/devel
Browse files Browse the repository at this point in the history
Source Transformation Feature
  • Loading branch information
ahueck committed Oct 19, 2015
2 parents 235d204 + 97f41d9 commit f84ce98
Show file tree
Hide file tree
Showing 73 changed files with 1,116 additions and 402 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ src/core/configuration/JSONConfiguration.cpp
include/external/IncludeDirectives.cpp
include/external/ReplacementHandling.cpp
src/core/issue/IssueHandler.cpp
src/core/issue/filter/UniqueFilter.cpp
src/core/reporting/ConsoleReporter.cpp
src/core/reporting/CSVReporter.cpp
src/core/transformation/TransformationHandler.cpp
Expand All @@ -30,14 +31,15 @@ set(PSOURCES
src/AnalyzerFactory.cpp
src/ModuleConsumer.cpp
src/modules/ImplicitConditionMatcher.cpp
src/modules/FieldDeclCollector.cpp
#src/modules/FieldDeclCollector.cpp
src/modules/UnionMatcher.cpp
src/modules/ExplicitCast.cpp
src/modules/ConditionalAssgnMatcher.cpp
src/modules/ImplicitConversion.cpp
src/modules/ExplicitConstructor.cpp
src/modules/AllImplicitConversion.cpp
src/modules/GlobalScope.cpp
src/modules/IfElseAssign.cpp
)

add_library(libopov ${LSOURCES})
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The status of this software is alpha level.
License
------------

Distributed under the MIT License. For details refer to the [LICENSE file](LICENSE)
Distributed under the MIT License. For details refer to the [LICENSE](LICENSE)


Motivation
Expand All @@ -34,12 +34,13 @@ and result in compile time errors.
- “At most one user-defined conversion [...] is implicitly
applied to a single value.” – [§12.3-4, C++03 Standard]
- Subset:
- Boolean Conversions: i.e., conditional statements
- Boolean Conversions, i.e., conditional statements
- Unions
- Incompatible with complex data classes [§9.5-1, C++03 Standard]
- Explicit Conversions
- Cast operation from a user-defined type to a built-in is often not possible
- etc.
- Friend functions and the Scope Resolution Operator (**::**)
- A combination of these two C++ features can cause problems in certain circumstances


Goal
Expand Down Expand Up @@ -73,7 +74,7 @@ For Ubuntu/Debian, refer to the [Travis CI file](.travis.yml) for guidance.
In the root folder of the project:

mkdir build && cd build/
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake -DCMAKE_BUILD_TYPE=Release -DMAKE_TEST=FALSE ..
make

The binary should be created in the project bin/ folder.
The binary should be created in the project folder *bin/*.
8 changes: 7 additions & 1 deletion conf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"global" : {
"transform" : true,
"filter" : true,
"type" : "scalar"
}
},
"ExplicitCast" : {
"header" : "reCast.h",
"function" : "reCast"
}
}
1 change: 1 addition & 0 deletions include/core/AbstractModuleConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AbstractModuleConsumer : public clang::ASTConsumer {

public:
AbstractModuleConsumer(Module* module, ModuleContext* mcontext);
virtual void Initialize(clang::ASTContext& Context) override;
void HandleTranslationUnit(clang::ASTContext& Context) override;
virtual ~AbstractModuleConsumer();
};
Expand Down
7 changes: 7 additions & 0 deletions include/core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class IssueReporter;
class IssueHandler;
class TransformationHandler;
class Module;
namespace filter {
class IFilter;
}

class Application {
protected:
Expand All @@ -36,22 +39,26 @@ class Application {
std::unique_ptr<clang::tooling::FrontendActionFactory> actionFactory;
std::unique_ptr<IssueHandler> ihandler;
std::unique_ptr<TransformationHandler> thandler;
std::unique_ptr<filter::IFilter> filter;
std::vector<Module*> modules;

virtual void loadConfig() = 0;
virtual void createReporter() = 0;
virtual void createIssueHandler();
virtual void createFilter();
virtual void createTransformationHandler();
virtual void createFactory() = 0;
virtual void createActionFactory();
virtual void initModules() = 0;

public:
Application();
virtual void init();
virtual void cleanUp();
virtual int execute(const clang::tooling::CompilationDatabase& db, const std::vector<std::string>& sources);
virtual int executeOnCode(const std::string& source,
const std::vector<std::string>& args = std::vector<std::string>());
virtual void report();
virtual void addModule(Module* module);
virtual std::string getApplicationName();
virtual ~Application();
Expand Down
12 changes: 12 additions & 0 deletions include/core/issue/Issue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "map/PropertyMap.h"

#include <functional>

namespace opov {

class Issue {
Expand All @@ -19,9 +21,19 @@ class Issue {
public:
Issue() {
}

const property_map& properties() {
return _properties;
}

int hash() {
int hash = 7;
int pos_hash = getLineStart() ^ (getColumnStart() << 4) ^ (getLineEnd() << 8) ^ (getColumnEnd() << 16);
hash += std::hash<std::string>()(getFile());
hash = 17 * hash + std::hash<std::string>()(getModuleName());
return hash ^ pos_hash;
}

int IssueProperty(LineStart);
int IssueProperty(LineEnd);
int IssueProperty(ColumnStart);
Expand Down
5 changes: 3 additions & 2 deletions include/core/issue/IssueHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ class IssueHandler {
private:
std::string source;
TUIssuesMap issues;
clang::ASTContext* ac;

public:
IssueHandler();
void setSource(const std::string& source);
void init(clang::ASTContext* ac);
template <typename T>
void addIssue(const clang::SourceManager& sm, const clang::ASTContext& ac, T node, const std::string& module,
const std::string& module_descr, std::string message = "");
void addIssue(T node, const std::string& module, const std::string& module_descr, std::string message = "");
TUIssuesMap& getAllIssues();
void clear();
virtual ~IssueHandler();
Expand Down
8 changes: 4 additions & 4 deletions include/core/issue/IssueHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
#include <core/issue/Issue.h>
#include <core/utility/ClangUtil.h>

#include <clang/Basic/SourceManager.h>
#include <clang/AST/ASTContext.h>
#include <clang/Basic/SourceManager.h>

#include <memory>

namespace opov {

template <typename T>
void IssueHandler::addIssue(const clang::SourceManager& sm, const clang::ASTContext& ac, T node,
const std::string& module, const std::string& module_descr, std::string message) {
void IssueHandler::addIssue(T node, const std::string& module, const std::string& module_descr, std::string message) {
auto& sm = ac->getSourceManager();
std::shared_ptr<Issue> issue = std::make_shared<Issue>();
std::string issue_file = clutil::fileOriginOf(sm, node);
auto pos = clutil::posOf(sm, node);
std::string node_source = clutil::node2str(ac, node);
std::string node_source = clutil::node2str(*ac, node);
issue->setModuleName(module);
issue->setModuleDescription(module_descr);
issue->setDescription(message);
Expand Down
29 changes: 29 additions & 0 deletions include/core/issue/filter/FilterIssueStruct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef FILTER_ISSUE_STRUCT_H
#define FILTER_ISSUE_STRUCT_H

#include "../Issue.h"

#include <memory>
#include <map>
#include <vector>

namespace opov {
namespace filter {

struct IssuesFiltered {
std::shared_ptr<Issue> issue;
std::vector<std::string> tunits;
};

struct issue_compare {
bool operator() (const IssuesFiltered& a, const IssuesFiltered& b) {
return a.issue->hash() != b.issue->hash();
}
};

typedef std::map<int, IssuesFiltered> IssueSet;

}
}

#endif
22 changes: 22 additions & 0 deletions include/core/issue/filter/IFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef IFILTER_H
#define IFILTER_H

#include "../IssueHandlerStruct.h"
#include "FilterIssueStruct.h"
#include "../Issue.h"

namespace opov {
namespace filter {

class IFilter {
public:
virtual IssueSet apply(const TUIssuesMap& map) = 0;
virtual ~IFilter() {

}
};

}
}

#endif
20 changes: 20 additions & 0 deletions include/core/issue/filter/UniqueFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef UNIQUE_FILTER_H
#define UNIQUE_FILTER_H

#include "IFilter.h"

namespace opov {
namespace filter {

class UniqueFilter: public IFilter {
public:
UniqueFilter();
virtual ~UniqueFilter();

virtual IssueSet apply(const TUIssuesMap& map) override;
};

}
}

#endif
2 changes: 2 additions & 0 deletions include/core/module/AbstractModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Configuration;
class AbstractModule : public Module {
protected:
ModuleContext* context;
std::string type_s;
bool transform;

public:
AbstractModule();
Expand Down
4 changes: 3 additions & 1 deletion include/core/module/ModuleContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ class ModuleContext {
clang::ASTContext* context;
IssueHandler* ihandler;
TransformationHandler* thandler;
std::string current_src;

public:
ModuleContext(Configuration* config, IssueHandler* ihandler, TransformationHandler* thandler);
void setASTContext(clang::ASTContext* context);
void initContext(clang::ASTContext* context);
void setCurrentSource(const std::string& source);
clang::ASTContext& getASTContext();
clang::SourceManager& getSourceManager();
IssueHandler& getIssueHandler();
Expand Down
1 change: 1 addition & 0 deletions include/core/reporting/CSVReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CSVReporter : public opov::IssueReporter {
CSVReporter();
virtual void addIssue(const TranslationUnitIssues& issue) override;
virtual void addIssues(const TUIssuesMap& issues) override;
virtual void addIssues(const filter::IssueSet& issues) override;
virtual ~CSVReporter();

private:
Expand Down
1 change: 1 addition & 0 deletions include/core/reporting/ConsoleReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ConsoleReporter : public opov::IssueReporter {
ConsoleReporter();
virtual void addIssue(const TranslationUnitIssues& issue) override;
virtual void addIssues(const TUIssuesMap& issues) override;
virtual void addIssues(const filter::IssueSet& issues) override;
virtual ~ConsoleReporter();

private:
Expand Down
2 changes: 2 additions & 0 deletions include/core/reporting/IssueReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define ISSUEREPORTER_H_

#include "../issue/IssueHandlerStruct.h"
#include "../issue/filter/FilterIssueStruct.h"

#include <vector>
#include <string>
Expand All @@ -19,6 +20,7 @@ class IssueReporter {
public:
virtual void addIssue(const TranslationUnitIssues& issue) = 0;
virtual void addIssues(const TUIssuesMap& issues) = 0;
virtual void addIssues(const filter::IssueSet& issues) = 0;
virtual ~IssueReporter() {
}
};
Expand Down
15 changes: 14 additions & 1 deletion include/core/transformation/TransformationHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#ifndef TRANSFORMATIONHANDLER_H_
#define TRANSFORMATIONHANDLER_H_

#include <clang/Rewrite/Core/Rewriter.h>

#include <external/IncludeDirectives.h>

#include <llvm/ADT/StringMap.h>

#include <string>

namespace clang {
class SourceManager;
class LangOptions;
namespace tooling {
class Replacement;
class TranslationUnitReplacements;
Expand All @@ -30,15 +34,24 @@ class TransformationHandler {
TUReplacementsMap replacements;
std::string source;
std::unique_ptr<IncludeDirectives> includes;
clang::Rewriter rewriter;

public:
TransformationHandler();
void setSource(const std::string& current);
void initRewriter(clang::SourceManager& sm, const clang::LangOptions& langOpts);
void setIncludeDirectives(IncludeDirectives* include);

void addHeader(const std::string& header, clang::SourceLocation loc);
void addReplacements(const clang::tooling::Replacement& replacement);
void addReplacements(const std::vector<clang::tooling::Replacement>& replacements);
void addReplacements(const clang::FixItHint& Hint);
TUReplacementsMap& getAllReplacements();
void setIncludeDirectives(IncludeDirectives* include);



IncludeDirectives* getIncludeDirectives();
clang::Rewriter& getRewriter();
virtual ~TransformationHandler();
};

Expand Down
Loading

0 comments on commit f84ce98

Please sign in to comment.