Skip to content

Commit

Permalink
Clang implemented, need to pull out redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
jwgrenning committed Sep 11, 2019
1 parent 23000a4 commit ee40d31
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 28 deletions.
8 changes: 8 additions & 0 deletions fakes_cpp_globals
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@








28 changes: 23 additions & 5 deletions gen-xfakes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ makeCppGlobalFakes()

makeCFakes()
{
grep -v "(.*).*(.*)" | grep -v "::" | sed -e's|.*undefined reference to `|EXPLODING_FAKE_FOR(|' -e"s|\'|)|"
grep -v "(.*).*(.*)" | grep -v "::" | sed -e's|.*undefined reference to `|EXPLODING_FAKE_FOR(|' -e"s|\'|)|"
}

usage()
Expand Down Expand Up @@ -147,10 +147,17 @@ makeFakes()
cat $1 | removeNonLinkErrors | make$2Fakes | sort | uniq >> $3
}

makeFakes2()
{
cant_exist $4
preamble$3Fakes $4 $1
cat $2 | make$3Fakes2 >> $4
}


makeCFakes2()
{
grep -v "::" | grep -v "(.*)" | sed -e's/^/EXPLODING_FAKE_FOR(/' -e's/$/)/'
grep -v "::" | grep -v "(.*)" | grep -v "typeinfo" | sed -e's/^/EXPLODING_FAKE_FOR(/' -e's/$/)/'
}

makeCppFakes2()
Expand All @@ -171,13 +178,24 @@ gen_xfakes()

input_file=$1
must_exist $input_file
undefines=$(mktemp)
sorted_undefines=$(mktemp)

isolateUndefinedSymbolsGcc <$input_file >$undefines
isolateUndefinedSymbolsClang <$input_file >>$undefines
sort $undefines | uniq >$sorted_undefines


fakes_c=$2-c.c
fakes_cpp=$2-cpp.cpp
fakes_cpp_globals=$2-cpp-globals.cpp

makeFakes $input_file C $fakes_c
makeFakes $input_file Cpp $fakes_cpp
makeFakes $input_file CppGlobal $fakes_cpp_globals
makeFakes2 $input_file $sorted_undefines C $fakes_c
makeFakes2 $input_file $sorted_undefines Cpp $fakes_cpp
makeFakes2 $input_file $sorted_undefines CppGlobal $fakes_cpp_globals
rm $undefines
rm $sorted_undefines

}

if [[ "$(basename -- "$0")" == "gen-xfakes.sh" ]]; then
Expand Down
30 changes: 13 additions & 17 deletions test-gen-xfakes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ testFilterClangLinkerErrors()
cpp_function1=' "SomeClass::someFunction(Foo&)", referenced from:'
cpp_function2=' "someFunction(Foo*)", referenced from:'
cpp_global=' "SomeClass::someGlobal", referenced from:'
cpp_typeinfo="typeinfo for SomeClass'"
c_undefined=' "_someGlobal", referenced from:'
assertEquals 'SomeClass::someFunction(Foo&)' "$(echo $cpp_function1 | isolateUndefinedSymbolsClang)"
assertEquals 'someFunction(Foo*)' "$(echo $cpp_function2 | isolateUndefinedSymbolsClang)"
assertEquals 'SomeClass::someGlobal' "$(echo $cpp_global | isolateUndefinedSymbolsClang)"
assertEquals '' "$(echo $cpp_typeinfo | isolateUndefinedSymbolsClang)"
assertEquals 'someGlobal' "$(echo $c_undefined | isolateUndefinedSymbolsClang)"
}

Expand Down Expand Up @@ -114,34 +116,28 @@ testCommandLine()

diffWithGolden()
{
assertEquals "$1 is different than golden copy" "" "$(diff test/$1 test/golden/$1)"
assertEquals "$2 is different than golden copy" "" "$(diff test/$2 test/golden/$1-$2)"
}

testOutputSameAsGolden()
checkOutputSameAsGolden()
{
gen_xfakes test/example-gcc-link-errors.txt test/myfakes
diffWithGolden myfakes-c.c
diffWithGolden myfakes-cpp.cpp
diffWithGolden myfakes-cpp-globals.cpp
gen_xfakes test/example-$1-link-errors.txt test/xfakes
diffWithGolden $1 xfakes-c.c
diffWithGolden $1 xfakes-cpp.cpp
diffWithGolden $1 xfakes-cpp-globals.cpp
rm test/xfakes*.*
}

cleanupOutput()
testOutputSameAsGoldenGcc()
{
rm -f test/myfakes-c.c
rm -f test/myfakes-cpp*.cpp
checkOutputSameAsGolden gcc
}

oneTimeSetUp()
testOutputSameAsGoldenClang()
{
cleanupOutput
checkOutputSameAsGolden clang
}

oneTimeTearDown()
{
cleanupOutput
}


. $(dirname "$0")/gen-xfakes.sh
. ./shunit2/shunit2

Expand Down
2 changes: 1 addition & 1 deletion test/example-gcc-link-errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ HardConnect.cpp:(.text+0xe06): undefined reference to `AcmeRegistry::Write(char
HardConnect.cpp:(.text+0xe06): undefined reference to `AcmeRegistry::instance'
HardConnect.cpp:(.text+0xebb): undefined reference to `AcmeCrypto_RemoveKeyFile(char const*)'
HardConnect.cpp:(.text+0x24e3): undefined reference to `pthread_setname_np'
undefined reference to `myJSON_GetObjectItem'
HardConnect.cpp:(.text+0x24e3): undefined reference to `myJSON_GetObjectItem'
HardConnect.cpp:(.text._ZN32JsonHardConnectInitializeRequest15getNestedStringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_PS5_[_ZN32JsonHardConnectInitializeRequest15getNestedStringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_PS5_]+0x8c): undefined reference to `myJSON_IsString'
/tmp/xxxxxx.o: In function `JsonCloudserver1LinkRequest::JsonCloudserver1LinkRequest()':
HardConnect.cpp:(.text._ZN22JsonCloudserver1LinkRequestC2Ev[_ZN22JsonCloudserver1LinkRequestC5Ev]+0x18): undefined reference to `myJSON_CreateObject'
Expand Down
27 changes: 27 additions & 0 deletions test/golden/clang-xfakes-c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
File: xfakes-c.c From: example-clang-link-errors.txt
Generated file to help to quickly stub C-linkage unresolved external references
* When EXPLODING_FAKE_FOR is executed, a message is printed and the test run is existed
* You could customize EXPLODING_FAKE_FOR to only fail the test
* Add this file to your test build.
* Do not include the header files for the referenced functions. The C-linker does not care.
Note: a EXPLODING_FAKE_FOR() is generated for global variables too.
* They will explode upon write :-)
* You might want to resolve these a different way.
*/

#include <stdio.h>
#include <stdlib.h>

#define BOOM_MESSAGE printf("BOOM! time to write a better fake for %s\n", __func__)
#define EXPLODING_FAKE_FOR(f) void f(void); void f(void) { BOOM_MESSAGE; exit(1); }
#define NULL_VOID_FAKE_FOR(f) void f(void); void f(void) {}
#define NULL_VALUE_FAKE_FOR(value_type, f, result) value_type f(void); value_type f(void) { return result; }


EXPLODING_FAKE_FOR(eventList)
EXPLODING_FAKE_FOR(log_debug)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
File: myfakes-cpp-globals.cpp From: example-gcc-link-errors.txt
File: xfakes-cpp-globals.cpp From: example-clang-link-errors.txt
Generated file to help to quickly stub C++ undefined external globals.
Expand All @@ -14,5 +14,3 @@
*/

//cpp-global AcmeRegistry::instance;
//cpp-global AcmeWpa::clock;
48 changes: 48 additions & 0 deletions test/golden/clang-xfakes-cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
File: xfakes-cpp.cpp From: example-clang-link-errors.txt
Generated file to help to quickly stub C++ undefined external functions.
* Add this file to your test build.
* One at a time
* Uncomment an exploing fake function definition.
* Add needed the include file.
* Modify non-void function return types and propvide a return value.
* Fix errors.
* Work carefully. Use the compiler and link error output to test your changes.
* You could customize the BOOM macros to only fail the test, rather than exit the
test runner.
*/

#include <stdio.h>
#include <stdlib.h>

#define BOOM_MESSAGE printf("BOOM! time to write a better fake for %s\n", __func__)
#define BOOM_VOID_CPP BOOM_MESSAGE; exit(1);
#define BOOM_VALUE_CPP(result) BOOM_MESSAGE; exit(1); return result;

/*
* Production code header files
*/

// #include "your.h"

// void LightSwitch::LightSwitch() { BOOM_VOID_CPP }
// void LightSwitch::Off(int) { BOOM_VOID_CPP }
// void LightSwitch::On(int) { BOOM_VOID_CPP }
// void LightSwitch::~LightSwitch() { BOOM_VOID_CPP }
// void RandomMinuteGenerator::Get() { BOOM_VOID_CPP }
// void RandomMinuteGenerator::RandomMinuteGenerator(int, int) { BOOM_VOID_CPP }
// void RandomMinuteGenerator::~RandomMinuteGenerator() { BOOM_VOID_CPP }
// void Time::Time(Time const&) { BOOM_VOID_CPP }
// void Time::getDay() const { BOOM_VOID_CPP }
// void Time::getMinute() const { BOOM_VOID_CPP }
// void Time::~Time() { BOOM_VOID_CPP }
// void TimeService::TimeService() { BOOM_VOID_CPP }
// void TimeService::getTime() { BOOM_VOID_CPP }
// void TimeService::wakePeriodically(std::__1::unique_ptr<WakeUpAction, std::__1::default_delete<WakeUpAction> >) { BOOM_VOID_CPP }
// void TimeService::~TimeService() { BOOM_VOID_CPP }
// void WakeUpAction::WakeUpAction(int) { BOOM_VOID_CPP }
// void WakeUpAction::~WakeUpAction() { BOOM_VOID_CPP }
2 changes: 1 addition & 1 deletion test/golden/myfakes-c.c → test/golden/gcc-xfakes-c.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
File: myfakes-c.c From: example-gcc-link-errors.txt
File: xfakes-c.c From: example-gcc-link-errors.txt
Generated file to help to quickly stub C-linkage unresolved external references
Expand Down
18 changes: 18 additions & 0 deletions test/golden/gcc-xfakes-cpp-globals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
File: xfakes-cpp-globals.cpp From: example-gcc-link-errors.txt
Generated file to help to quickly stub C++ undefined external globals.
* One at a time
* Add the file containing the global definition to your build or
add the global data definition (and its declaratiob) to this file.
Adding the global to this file is probably not sustainable, but a
pragmatic choice until you decide how to better organioze your global
data.
* Add include files as neededd.
* Work carefully. Use the compiler and link error output to test your changes.
*/

// cpp-global AcmeRegistry::instance;
// cpp-global AcmeWpa::clock;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
File: myfakes-cpp.cpp From: example-gcc-link-errors.txt
File: xfakes-cpp.cpp From: example-gcc-link-errors.txt
Generated file to help to quickly stub C++ undefined external functions.
Expand Down

0 comments on commit ee40d31

Please sign in to comment.