From f26827a6487449ec94e931b2d17ad5b97b634ea0 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Fri, 9 Nov 2018 13:19:34 +0330 Subject: init commit --- README.md | 2 +- cfe_extra.cpp | 166 ++++++++++++++++++++++++++++++++++++++++++++++ cfe_extra.h | 66 +++++++++++++++++++ compile_commands.json | 7 ++ makefile | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 417 insertions(+), 1 deletion(-) create mode 100644 cfe_extra.cpp create mode 100644 cfe_extra.h create mode 100644 compile_commands.json create mode 100644 makefile diff --git a/README.md b/README.md index a9666ed..f805d28 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # cfe-extra -a bunch of utility functions for clang tools which i usually end up using +A bunch of utility functions for clang tools which i usually end up using.
diff --git a/cfe_extra.cpp b/cfe_extra.cpp new file mode 100644 index 0000000..5b17fb4 --- /dev/null +++ b/cfe_extra.cpp @@ -0,0 +1,166 @@ + +/***************************************************Project Mutator****************************************************/ +//-*-c++-*- +/*first line intentionally left blank.*/ +/*Copyright (C) 2018 Farzad Sadeghi + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +/*********************************************************************************************************************/ +/*inclusion directives*/ +#include "cfe_extra.h" +#include +#include +#include +#include +#include +#include "clang/AST/AST.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Rewrite/Core/Rewriter.h" +/*********************************************************************************************************************/ +using namespace clang; +/*********************************************************************************************************************/ +namespace Devi { +/*a simple function that checks the sourcelocations for a macro expansion. returns the sourcelocation without +macro expansion address.*/ +#if 1 +SourceLocation SourceLocationHasMacro [[deprecated("doesnt work")]] (SourceLocation SL, Rewriter &Rewrite, std::string Kind) { + /*does the sourcelocation include a macro expansion?*/ + if ( SL.isMacroID()) { + /*get the expansion range which is startloc and endloc*/ +#if __clang_major__ <= 6 + std::pair expansionRange = Rewrite.getSourceMgr().getImmediateExpansionRange(SL); +#elif __clang_major__ >= 8 + CharSourceRange expansionRange = Rewrite.getSourceMgr().getImmediateExpansionRange(SL); +#endif + if (Kind == "start") { +#if __clang_major__ <= 6 + return expansionRange.first; +#elif __clang_major__ >= 8 + return expansionRange.getBegin(); +#endif + } else if (Kind == "end") { +#if __clang_major__ <= 6 + return expansionRange.second; +#elif __clang_major__ >= 8 + return expansionRange.getEnd(); +#endif + } else { + std::cout << "the third argument of Devi::SourceLocationHasMacro is invalid." << std::endl; + } + } else { + return (SL); + } + return (SL); +} +#endif + +SourceLocation SourceLocationHasMacro(SourceLocation __sl, Rewriter &__rewrite) { + if (__sl.isMacroID()) { + return __rewrite.getSourceMgr().getSpellingLoc(__sl); + } else { + return __sl; + } +} + +SourceLocation getSLSpellingLoc(SourceLocation __sl, Rewriter &__rewrite) { + if (__sl.isMacroID()) return __rewrite.getSourceMgr().getSpellingLoc(__sl); + else return __sl; +} +/*********************************************************************************************************************/ +/*********************************************************************************************************************/ +/*********************************************************************************************************************/ +/*the first argument is the option SysHeader from the mutator-lvl0 cl.*/ +bool IsTheMatchInSysHeader(bool SysHeaderFlag, const ast_matchers::MatchFinder::MatchResult &MR, SourceLocation SL) { + ASTContext *const ASTC = MR.Context; + const SourceManager &SM = ASTC->getSourceManager(); + + if (SM.isInSystemHeader(SL) && !SysHeaderFlag) { + return true; + } else { + return false; + } +} + +bool IsTheMatchInSysHeader(bool SysHeaderFlag, const SourceManager &SM, SourceLocation SL) { + if (SM.isInSystemHeader(SL) && !SysHeaderFlag) { + return true; + } else { + return false; + } +} + +bool IsTheMatchInSysHeader(bool SysHeaderFlag, bool SysHeader, SourceLocation SL) { + if (SysHeader && !SysHeaderFlag) { + return true; + } else { + return false; + } +} + +bool IsTheMatchInSysHeader(bool SysHeaderFlag, bool SysHeader) +{ + if (SysHeader && !SysHeaderFlag) { + return true; + } else { + return false; + } +} +/*********************************************************************************************************************/ +/*********************************************************************************************************************/ +/*********************************************************************************************************************/ +bool IsTheMatchInMainFile(bool MainFileFlag, const ast_matchers::MatchFinder::MatchResult &MR, SourceLocation SL) { + ASTContext *const ASTC = MR.Context; + const SourceManager &SM = ASTC->getSourceManager(); + if (SM.isInMainFile(SL) || (!SM.isInMainFile(SL) && !MainFileFlag)) { + return true; + } else { + return false; + } +} + +bool IsTheMatchInMainFile(bool MainFileFlag, const SourceManager &SM, SourceLocation SL) { + if (SM.isInMainFile(SL) || (!SM.isInMainFile(SL) && !MainFileFlag)) { + return true; + } else { + return false; + } +} + +bool IsTheMatchInMainFile(bool MainFileFlag, bool MainFile, SourceLocation SL) { + if (MainFile || (!MainFile && !MainFileFlag)) { + return true; + } else { + return false; + } +} + +bool IsTheMatchInMainFile(bool MainFileFlag, bool MainFile) { + if (MainFile || (!MainFile && !MainFileFlag)) { + return true; + } else { + return false; + } +} +/*********************************************************************************************************************/ +/*End of namespace Devi*/ +} +#pragma weak main +int main(int argc, char** argv) { + std::cout << "cfe_extra, version 1.0.0\n"; + return 0; +} +/*********************************************************************************************************************/ +/*last line intentionally left blank.*/ + diff --git a/cfe_extra.h b/cfe_extra.h new file mode 100644 index 0000000..9f22a5a --- /dev/null +++ b/cfe_extra.h @@ -0,0 +1,66 @@ + +/***************************************************Project Mutator****************************************************/ +//-*-c++-*- +/*********************************************************************************************************************/ +/*first line intentionally left blank*/ +/*Copyright (C) 2017 Farzad Sadeghi + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 3 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +/*********************************************************************************************************************/ +/*inclusion guard*/ +#ifndef MUTATOR_AUX_H +#define MUTATOR_AUX_H +/*********************************************************************************************************************/ +/*inclusion directives*/ +#include +#include +#include "clang/AST/AST.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Rewrite/Core/Rewriter.h" +/*********************************************************************************************************************/ +/*Macros and definitions*/ +#define CheckSLValidity(SL) \ + do {\ + if (!SL.isValid()) {return void();}}\ + while(0); +/*********************************************************************************************************************/ +using namespace clang; +/*********************************************************************************************************************/ +namespace Devi { +enum class NodeKind {NoValue, VarDecl, FieldDecl, RecordDecl, LabelDecl, FunctionDecl, TypedefDecl, ParmVarDecl, EnumDecl, EnumConstDecl}; +enum class Scope {NoValue, TU, Block}; +enum class FunctionDeclKind {NoValue, Definition, Declaration}; +/*********************************************************************************************************************/ +SourceLocation SourceLocationHasMacro(SourceLocation SL, Rewriter &Rewrite, std::string Kind); +SourceLocation SourceLocationHasMacro(SourceLocation __sl, Rewriter &__rewrite); +SourceLocation getSLSpellingLoc(SourceLocation __sl, Rewriter &__rewrite); +/*********************************************************************************************************************/ +bool IsTheMatchInSysHeader(bool SysHeaderFlag, const ast_matchers::MatchFinder::MatchResult &MR, SourceLocation SL); +bool IsTheMatchInSysHeader(bool SysHeaderFlag, const SourceManager &SM, SourceLocation SL); +bool IsTheMatchInSysHeader(bool SysHeaderFlag, bool SysHeader, SourceLocation SL); +bool IsTheMatchInSysHeader(bool SysHeaderFlag, bool SysHeader); +/*********************************************************************************************************************/ +bool IsTheMatchInMainFile(bool MainFileFlag, const ast_matchers::MatchFinder::MatchResult &MR, SourceLocation SL); +bool IsTheMatchInMainFile(bool MainFileFlag, const SourceManager &SM, SourceLocation SL); +bool IsTheMatchInMainFile(bool MainFileFlag, bool MainFile, SourceLocation SL); +bool IsTheMatchInMainFile(bool MainFileFlag, bool MainFile); +/*********************************************************************************************************************/ +/*end of namespace Devi*/ +} +#endif +/*********************************************************************************************************************/ +/*last line intentionally left blank.*/ + diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..00d5c01 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,7 @@ +[ + { + "command": "c++ -c -fpic -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wcovered-switch-default -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -ffunction-sections -fdata-sections -O2 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -stdlib=libstdc++ -std=c++17 -fexceptions -o cfe_extra.o cfe_extra.cpp", + "directory": "/home/bloodstalker/extra/cfe-extra", + "file": "/home/bloodstalker/extra/cfe-extra/cfe_extra.cpp" + } +] \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..1e2474f --- /dev/null +++ b/makefile @@ -0,0 +1,177 @@ +TARGET=cfe_extra +SHELL=bash +SHELL?=bash +CC=clang +CC?=clang +CFLAGS=-fpic -std=c11 +CXX=clang++ +CXX?=clang++ +CXX_FLAGS=-fpic +CXX_EXTRA?= +CTAGS_I_PATH?=./ +LD_FLAGS= +EXTRA_LD_FLAGS?= +ADD_SANITIZERS_CC= -g -fsanitize=address -fno-omit-frame-pointer +ADD_SANITIZERS_LD= -g -fsanitize=address +MEM_SANITIZERS_CC= -g -fsanitize=memory -fno-omit-frame-pointer +MEM_SANITIZERS_LD= -g -fsanitize=memory +UB_SANITIZERS_CC= -g -fsanitize=undefined -fno-omit-frame-pointer +UB_SANITIZERS_LD= -g -fsanitize=undefined +COV_CXX= -fprofile-instr-generate -fcoverage-mapping +COV_LD= -fprofile-instr-generate +# BUILD_MODES are=RELEASE(default), DEBUG,ADDSAN,MEMSAN,UBSAN +BUILD_MODE?=RELEASE +OBJ_LIST:=$(patsubst %.cpp, %.o, $(wildcard *.cpp)) +ASM_LIST:=$(patsubst %.cpp, %.dis, $(wildcard *.cpp)) + +LLVM_CONF?=llvm-config +LLVM_CXX_FLAGS=$(shell $(LLVM_CONF) --cxxflags) +LLVM_CXX_FLAGS+=-I$(shell $(LLVM_CONF) --src-root)/tools/clang/include\ + -I$(shell $(LLVM_CONF) --obj-root)/tools/clang/include\ + -stdlib=libstdc++ -std=c++17 -fexceptions +LLVM_LD_FLAGS=-Wl,--start-group -lclangAST -lclangAnalysis -lclangBasic\ + -lclangDriver -lclangEdit -lclangFrontend -lclangFrontendTool\ + -lclangLex -lclangParse -lclangSema -lclangEdit -lclangASTMatchers\ + -lclangRewrite -lclangRewriteFrontend -lclangStaticAnalyzerFrontend\ + -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore\ + -lclangSerialization -lclangToolingCore -lclangTooling -lstdc++\ + -lLLVMRuntimeDyld -lm -Wl,--end-group +LLVM_LD_FLAGS+=$(shell $(LLVM_CONF) --ldflags --libs --system-libs) + +CXX_FLAGS+=$(LLVM_CXX_FLAGS) +LD_FLAGS+=$(LLVM_LD_FLAGS) + +MAKEFLAGS+=--warn-undefined-variables +ifeq ($(BUILD_MODE), ADDSAN) +ifeq ($(CXX), g++) +$(error This build mode is only useable with clang++.) +endif +CXX_EXTRA+=$(ADD_SANITIZERS_CC) +EXTRA_LD_FLAGS+=$(ADD_SANITIZERS_LD) +endif + +ifeq ($(BUILD_MODE), MEMSAN) +ifeq ($(CXX), g++) +$(error This build mode is only useable with clang++.) +endif +CXX_EXTRA+=$(MEM_SANITIZERS_CC) +EXTRA_LD_FLAGS+=$(MEM_SANITIZERS_LD) +endif + +ifeq ($(BUILD_MODE), UBSAN) +ifeq ($(CXX), g++) +$(error This build mode is only useable with clang++.) +endif +CXX_EXTRA+=$(UB_SANITIZERS_CC) +EXTRA_LD_FLAGS+=$(UB_SANITIZERS_LD) +endif + +SRCS:=$(wildcard *.cpp) +HDRS:=$(wildcard *.hpp) +CXX_FLAGS+=$(CXX_EXTRA) +LD_FLAGS+=$(EXTRA_LD_FLAGS) + +.DEFAULT:all + +.PHONY:all clean help ASM SO TAGS + +all:$(TARGET) + +everything:$(TARGET) A ASM SO $(TARGET)-dbg TAGS $(TARGET)-cov + +depend:.depend + +.depend:$(SRCS) + rm -rf .depend + $(CXX) -MM $(CXX_FLAGS) $^ > ./.depend + echo $(patsubst %.o:, %.odbg:, $(shell $(CXX) -MM $(CXX_FLAGS) $^)) | sed -r 's/[A-Za-z0-9\-\_]+\.odbg/\n&/g' >> ./.depend + echo $(patsubst %.o:, %.ocov:, $(shell $(CXX) -MM $(CXX_FLAGS) $^)) | sed -r 's/[A-Za-z0-9\-\_]+\.ocov/\n&/g' >> ./.depend + +-include ./.depend + +.cpp.o: + $(CXX) $(CXX_FLAGS) -c $< -o $@ + +%.odbg:%.cpp + $(CXX) $(CXX_FLAGS) -g -c $< -o $@ + +%.ocov:%.cpp + $(CXX) $(CXX_FLAGS) $(COV_CXX) -c $< -o $@ + +$(TARGET): $(TARGET).o + $(CXX) $^ $(LD_FLAGS) -o $@ + +$(TARGET)-static: $(TARGET).o + $(CXX) $^ $(LD_FLAGS) -static -o $@ + +$(TARGET)-dbg: $(TARGET).odbg + $(CXX) $^ $(LD_FLAGS) -g -o $@ + +$(TARGET)-cov: $(TARGET).ocov + $(CXX) $^ $(LD_FLAGS) $(COV_LD) -o $@ + +cov: runcov + @llvm-profdata merge -sparse ./default.profraw -o ./default.profdata + @llvm-cov show $(TARGET)-cov -instr-profile=default.profdata -ignore-filename-regex=llvm clang + +covrep: runcov + @llvm-profdata merge -sparse ./default.profraw -o ./default.profdata + @llvm-cov report $(TARGET)-cov -instr-profile=default.profdata -ignore-filename-regex=llvm -ignore-filename-regex=clang -show-functions autosarpp.cpp + +ASM:$(ASM_LIST) + +SO:$(TARGET).so + +A:$(TARGET).a + +TAGS:tags + +tags:$(SRCS) + $(shell $(CXX) -c $(shell $(LLVM_CONF) --cxxflags) -I$(shell $(LLVM_CONF) --src-root)/tools/clang/include -I$(shell $(LLVM_CONF) --obj-root)/tools/clang/include -I $(CTAGS_I_PATH) -M $(SRCS)|\ + sed -e 's/[\\ ]/\n/g'|sed -e '/^$$/d' -e '/\.o:[ \t]*$$/d'|\ + ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q) + +%.dis: %.o + objdump -r -d -M intel -S $< > $@ + +$(TARGET).so: $(TARGET).o + $(CXX) $^ $(LD_FLAGS) -shared -o $@ + +$(TARGET).a: $(TARGET).o + ar rcs $(TARGET).a $(TARGET).o + +runcov: $(TARGET)-cov + $(TARGET)-cov ./cgrep.cpp -- + +test: $(TARGET) + $(TARGET) ./cgrep.cpp -- + +valgrind: $(TARGET) + - valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all $(TARGET) $(TARGET).cpp -- + +format: + - clang-format -i $(SRCS) $(HDRS) + +clean: + rm -f *.o *.dis *.odbg *.ocov *~ $(TARGET) $(TARGET).so $(TARGET)-static $(TARGET)-dbg $(TARGET).a $(TARGET)-cov ./keccak-tiny/*.o ./keccak-tiny/*.ocov ./keccak-tiny/*.odbg + +deepclean: clean + - rm tags + - rm .depend + +help: + @echo "--all is the default target, runs $(TARGET) target" + @echo "--everything will build everything" + @echo "--SO will generate the so" + @echo "--ASM will generate assembly files" + @echo "--TAGS will generate tags file" + @echo "--$(TARGET) builds the dynamically-linked executable" + @echo "--$(TARGET)-dbg will generate the debug build. BUILD_MODE should be set to DEBUG to work" + @echo "--$(TARGET)-static will statically link the executable to the libraries" + @echo "--$(TARGET)-cov is the coverage build" + @echo "--cov will print the line coverage report" + @echo "--covrep will print the coverage report" + @echo "--A will build the static library" + @echo "--TAGS will build the tags file" + @echo "--clean" + @echo "--deepclean will clean almost everything" -- cgit v1.2.3