Merged
llvm与gcc的兼容性分析--implementation defined behavior #49
AtomGit-BotAdd on Sep 28, 2024
llvm与gcc的兼容性分析--implementation defined behavior #49
Merged
Merged fromrefs/pull/49/headintomaster
Total: 3 files changed+2452-0
| @@ -0,0 +1,168 @@ | |||
| 1 | +## Implementation-defined Behaviors | ||
| 2 | + | ||
| 3 | +## 1. Conditionally-Supported Behavior | ||
| 4 | + | ||
| 5 | +Each implementation shall include documentation that identifies all conditionally-supported constructs that it does not support. | ||
| 6 | + | ||
| 7 | +故为了本文档与GCC文档保持一致,仅实验GCC文档中提及的implementation-defined Behaviors. | ||
| 8 | + | ||
| 9 | +### 1.1 Whether an argument of class type with a non-trivial copy constructor or destructor can be passed to ...(C++0x 5.2.2). | ||
| 10 | + | ||
| 11 | +- 具有非平凡复制构造函数或析构函数的类类型的参数是否可以传递给... | ||
| 12 | + | ||
| 13 | +GCC可以,LLVM不行,与GCC不一致 | ||
| 14 | + | ||
| 15 | +```cpp | ||
| 16 | +#include <iostream> | ||
| 17 | +#include <cstdarg> | ||
| 18 | + | ||
| 19 | +class NonTrivialClass { | ||
| 20 | + public: | ||
| 21 | + int value; | ||
| 22 | + | ||
| 23 | + NonTrivialClass(int v) : value(v) { std::cout << "Constructor called\n"; } | ||
| 24 | + | ||
| 25 | + // 自定义复制构造函数,使类成为非平凡类 | ||
| 26 | + NonTrivialClass(const NonTrivialClass &other) { | ||
| 27 | + value = other.value; | ||
| 28 | + std::cout << "Copy constructor called\n"; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + // 自定义析构函数 | ||
| 32 | + ~NonTrivialClass() { | ||
| 33 | + std::cout << "Destructor called\n"; | ||
| 34 | + } | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +void variadicFunction(int num, ...) { | ||
| 38 | + va_list args; | ||
| 39 | + va_start(args, num); | ||
| 40 | + | ||
| 41 | + for (int i = 0; i < num; ++i) { | ||
| 42 | + // 尝试提取 NonTrivialClass 类型的参数 | ||
| 43 | + NonTrivialClass obj = va_arg(args, NonTrivialClass); | ||
| 44 | + std::cout << "Extracted value: " << obj.value << "\n"; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + va_end(args); | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +int main() { | ||
| 51 | + NonTrivialClass obj1(1); | ||
| 52 | + variadicFunction(1, obj1); | ||
| 53 | + return 0; | ||
| 54 | +} | ||
| 55 | +``` | ||
| 56 | + | ||
| 57 | +```bash | ||
| 58 | +$ make gcc | ||
| 59 | +g++ --std=c++11 -Wall -Wno-varargs ./test.cpp -o target_gcc | ||
| 60 | +echo //target_gcc > output_gcc | ||
| 61 | +./target_gcc>>output_gcc | ||
| 62 | + | ||
| 63 | +$ make clang | ||
| 64 | +clang++ --std=c++11 -Wall -Wnon-pod-varargs ./test.cpp -o target_clang | ||
| 65 | +./test.cpp:28:44: error: second argument to 'va_arg' is of non-POD type 'NonTrivialClass' [-Wnon-pod-varargs] | ||
| 66 | + NonTrivialClass obj = va_arg(args, NonTrivialClass); | ||
| 67 | + ^~~~~~~~~~~~~~~ | ||
| 68 | +/usr/lib/llvm-14/lib/clang/14.0.0/include/stdarg.h:19:50: note: expanded from macro 'va_arg' | ||
| 69 | +#define va_arg(ap, type) __builtin_va_arg(ap, type) | ||
| 70 | + ^~~~ | ||
| 71 | +./test.cpp:37:25: error: cannot pass object of non-trivial type 'NonTrivialClass' through variadic function; call will abort at runtime [-Wnon-pod-varargs] | ||
| 72 | + variadicFunction(1, obj1); | ||
| 73 | + ^ | ||
| 74 | +2 errors generated. | ||
| 75 | +make: *** [Makefile:21: clang] Error 1 | ||
| 76 | +``` | ||
| 77 | + | ||
| 78 | +```cpp | ||
| 79 | +//target_gcc | ||
| 80 | +Constructor called | ||
| 81 | +Copy constructor called | ||
| 82 | +Copy constructor called | ||
| 83 | +Extracted value: 1 | ||
| 84 | +Destructor called | ||
| 85 | +Destructor called | ||
| 86 | +Destructor called | ||
| 87 | +``` | ||
| 88 | + | ||
| 89 | +在本实验环境中,GCC可以,LLVM不行,与GCC一致。 | ||
| 90 | + | ||
| 91 | +## 2. Exception Handling | ||
| 92 | + | ||
| 93 | +### 1. In the situation where no matching handler is found, it is implementation-defined whether or not the stack is unwound before std::terminate() is called.(C++98 15.5.1). | ||
| 94 | + | ||
| 95 | +- 在未找到匹配的处理程序的情况下,在调用 std::terminate() 之前是否展开堆栈由实现定义。 | ||
| 96 | + | ||
| 97 | +```cpp | ||
| 98 | +#include <iostream> | ||
| 99 | +#include <exception> | ||
| 100 | + | ||
| 101 | +class TestClass { | ||
| 102 | + public: | ||
| 103 | + TestClass() { | ||
| 104 | + std::cout << "TestClass Constructor\n"; | ||
| 105 | + } | ||
| 106 | + ~TestClass() { | ||
| 107 | + std::cout << "TestClass Destructor\n"; | ||
| 108 | + } | ||
| 109 | +}; | ||
| 110 | + | ||
| 111 | +void throwException() { | ||
| 112 | + TestClass obj; | ||
| 113 | + std::cout << "Throwing exception...\n"; | ||
| 114 | + throw std::runtime_error("Uncaught exception"); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +int main() { | ||
| 118 | + try { | ||
| 119 | + throwException(); | ||
| 120 | + } catch (int e) { | ||
| 121 | + std::cout << "Caught an integer exception\n"; | ||
| 122 | + } | ||
| 123 | + // No catch for std::runtime_error, std::terminate will be called | ||
| 124 | + return 0; | ||
| 125 | +} | ||
| 126 | +``` | ||
| 127 | + | ||
| 128 | +```bash | ||
| 129 | +$ make gcc | ||
| 130 | +g++ -std=c++11 -Wall ./test.cpp -o target_gcc | ||
| 131 | +echo //target_gcc > output_gcc | ||
| 132 | +./target_gcc>>output_gcc | ||
| 133 | +terminate called after throwing an instance of 'std::runtime_error' | ||
| 134 | + what(): Uncaught exception | ||
| 135 | +Aborted | ||
| 136 | +make: *** [Makefile:18: gcc] Error 134 | ||
| 137 | + | ||
| 138 | +$ make clang | ||
| 139 | +clang++ -std=c++11 -Wall ./test.cpp -o target_clang | ||
| 140 | +./echo //target_clang > output_clang | ||
| 141 | +./target_clang>>output_clang | ||
| 142 | +terminate called after throwing an instance of 'std::runtime_error' | ||
| 143 | + what(): Uncaught exception | ||
| 144 | +Aborted | ||
| 145 | +make: *** [Makefile:23: clang] Error 134 | ||
| 146 | + | ||
| 147 | +$ ./target_gcc | ||
| 148 | +TestClass Constructor | ||
| 149 | +Throwing exception... | ||
| 150 | +terminate called after throwing an instance of 'std::runtime_error' | ||
| 151 | + what(): Uncaught exception | ||
| 152 | +Aborted | ||
| 153 | + | ||
| 154 | +$ ./target_clang | ||
| 155 | +TestClass Constructor | ||
| 156 | +Throwing exception... | ||
| 157 | +terminate called after throwing an instance of 'std::runtime_error' | ||
| 158 | + what(): Uncaught exception | ||
| 159 | +Aborted | ||
| 160 | +``` | ||
| 161 | + | ||
| 162 | +栈不会展开,和GCC一致。 | ||
| 163 | + | ||
| 164 | + | ||
| 165 | + | ||
| 166 | +## 3. 总结 | ||
| 167 | + | ||
| 168 | +对于所有实现定义行为而言,GCC和LLVM行为均一致。 | ||
| @@ -0,0 +1,99 @@ | |||
| 1 | +llvm和gcc是当下主流的C、C++编译器。应用使用不同编译器编译构建,可能遇到编译或者运行的兼容性问题。产生问题的主要原因有二种:1. 代码存在未定义行为,未遵循C/C++语言标准;2. 编译器实现的差别,又分为unspecified behavior 和 implementation defined behavior两种。 unspecified behavior:2种及以上的实现方式,编译器可以选择一种实现; implementation defined behavior 没有明确规定的实现方式,由编译器自定义实现。 | ||
| 2 | + | ||
| 3 | +C/C++语言标准里已明确定义和列举了undefined behavior、unspecified behavior、implementation defined behavior,明确llvm和gcc在此类行为方面的不同,就能提前预警编译器间的兼容性问题。 | ||
| 4 | + | ||
| 5 | + | ||
| 6 | + | ||
| 7 | +测试环境为Ubuntu 22.04.4 LTS,GCC版本为11.4.0,LLVM版本为14.0.0,Clang版本为14.0.0-1ubuntu1.1,glibc版本为2.35。 | ||
请在readme中写一下,做这件事情的背景和目的 ![]() ![]() | |||
| 8 | + | ||
| 9 | +对于implementation-defined behaviours而言,有很多是由the implementation of the C library以及ABI决定的,在本梳理中选择跳过处理。 | ||
| 10 | + | ||
| 11 | +测试C所用的`Makefile`如下: | ||
| 12 | + | ||
| 13 | +```makefile | ||
| 14 | +TESTFILE = ./test.c | ||
| 15 | + | ||
| 16 | +TARGET_GCC = target_gcc | ||
| 17 | +TARGET_CLANG = target_clang | ||
| 18 | +OUTPUT_GCC = output_gcc | ||
| 19 | +OUTPUT_CLANG = output_clang | ||
| 20 | + | ||
| 21 | +CAT = cat | ||
| 22 | +GXX = gcc | ||
| 23 | +CLANG = clang | ||
| 24 | +CFLAGS = --std=c11 #-fsanitize=shift | ||
| 25 | +FOLLOW_OPTION = #-lm | ||
| 26 | +PRE_PROCESS_CFLAGS = $(CFLAGS) -E | ||
| 27 | + | ||
| 28 | +gcc:$(TESTFILE) | ||
| 29 | + $(GXX) $(CFLAGS) $(TESTFILE) -o $(TARGET_GCC) $(FOLLOW_OPTION) | ||
| 30 | + echo //$(TARGET_GCC) > $(OUTPUT_GCC) | ||
| 31 | + ./$(TARGET_GCC)>>$(OUTPUT_GCC) | ||
| 32 | + | ||
| 33 | +clang:$(TESTFILE) | ||
| 34 | + $(CLANG) $(CFLAGS) $(TESTFILE) -o $(TARGET_CLANG) $(FOLLOW_OPTION) | ||
| 35 | + echo //$(TARGET_CLANG) > $(OUTPUT_CLANG) | ||
| 36 | + ./$(TARGET_CLANG)>>$(OUTPUT_CLANG) | ||
| 37 | + | ||
| 38 | +preprocess_gcc:$(TESTFILE) | ||
| 39 | + $(GXX) $(PRE_PROCESS_CFLAGS) $(TESTFILE) -o $(TARGET_GCC) $(FOLLOW_OPTION) | ||
| 40 | + $(CAT) $(TARGET_GCC) > $(OUTPUT_GCC) | ||
| 41 | + | ||
| 42 | +preprocess_clang:$(TESTFILE) | ||
| 43 | + $(CLANG) $(PRE_PROCESS_CFLAGS) $(TESTFILE) -o $(TARGET_CLANG) $(FOLLOW_OPTION) | ||
| 44 | + $(CAT) $(TARGET_CLANG) > $(OUTPUT_CLANG) | ||
| 45 | + | ||
| 46 | +clean: | ||
| 47 | + rm -f $(TARGET_GCC) $(TARGET_CLANG) $(OUTPUT_GCC) $(OUTPUT_CLANG) | ||
| 48 | + | ||
| 49 | +test: clean gcc clang | ||
| 50 | +preprocess_test: preprocess_gcc preprocess_clang | ||
| 51 | + | ||
| 52 | +.PHONY: clean test preprocess_test | ||
| 53 | +``` | ||
| 54 | + | ||
| 55 | +测试CPP所用的`Makefile`如下: | ||
| 56 | + | ||
| 57 | + | ||
| 58 | +```makefile | ||
| 59 | +TESTFILE = ./test.c | ||
| 60 | + | ||
| 61 | +TARGET_GCC = target_gcc | ||
| 62 | +TARGET_CLANG = target_clang | ||
| 63 | +OUTPUT_GCC = output_gcc | ||
| 64 | +OUTPUT_CLANG = output_clang | ||
| 65 | + | ||
| 66 | +CAT = cat | ||
| 67 | +GXX = g++ | ||
| 68 | +CLANG = clang++ | ||
| 69 | +CFLAGS = --std=c++11 -Wall -Wno-varargs | ||
| 70 | +FOLLOW_OPTION = #-lm | ||
| 71 | +PRE_PROCESS_CFLAGS = $(CFLAGS) -E | ||
| 72 | + | ||
| 73 | +gcc:$(TESTFILE) | ||
| 74 | + $(GXX) $(CFLAGS) $(TESTFILE) -o $(TARGET_GCC) $(FOLLOW_OPTION) | ||
| 75 | + echo //$(TARGET_GCC) > $(OUTPUT_GCC) | ||
| 76 | + ./$(TARGET_GCC)>>$(OUTPUT_GCC) | ||
| 77 | + | ||
| 78 | +clang:$(TESTFILE) | ||
| 79 | + $(CLANG) $(CFLAGS) $(TESTFILE) -o $(TARGET_CLANG) $(FOLLOW_OPTION) | ||
| 80 | + echo //$(TARGET_CLANG) > $(OUTPUT_CLANG) | ||
| 81 | + ./$(TARGET_CLANG)>>$(OUTPUT_CLANG) | ||
| 82 | + | ||
| 83 | +preprocess_gcc:$(TESTFILE) | ||
| 84 | + $(GXX) $(PRE_PROCESS_CFLAGS) $(TESTFILE) -o $(TARGET_GCC) $(FOLLOW_OPTION) | ||
| 85 | + $(CAT) $(TARGET_GCC) > $(OUTPUT_GCC) | ||
| 86 | + | ||
| 87 | +preprocess_clang:$(TESTFILE) | ||
| 88 | + $(CLANG) $(PRE_PROCESS_CFLAGS) $(TESTFILE) -o $(TARGET_CLANG) $(FOLLOW_OPTION) | ||
| 89 | + $(CAT) $(TARGET_CLANG) > $(OUTPUT_CLANG) | ||
| 90 | + | ||
| 91 | +clean: | ||
| 92 | + rm -f $(TARGET_GCC) $(TARGET_CLANG) $(OUTPUT_GCC) $(OUTPUT_CLANG) | ||
| 93 | + | ||
| 94 | +test: clean gcc clang | ||
| 95 | +preprocess_test: preprocess_gcc preprocess_clang | ||
| 96 | + | ||
| 97 | +.PHONY: clean test preprocess_test | ||
| 98 | +``` | ||
| 99 | + | ||


可以在结尾总结一下,有差异的都有哪些