我第一次尝试将 C++ 中的覆盖范围与 clang 6 集成,并且一直在遵循本指南。
\n\n我成功编译了二进制文件,生成了一个.profraw文件,并.profdata按照步骤 1、2 和 3a 中所述生成了一个文件。但是,当我尝试创建 3b 中所述的面向行的覆盖率报告时,我收到以下消息:
error: build/debug/dane: Failed to load coverage: No coverage data found\nRun Code Online (Sandbox Code Playgroud)\n\n检查.profraw文件后,我发现它是空的。我尝试稍微更改一下代码并再次运行,但生成的代码.profraw始终为空。
我的main.cpp文件:
error: build/debug/dane: Failed to load coverage: No coverage data found\nRun Code Online (Sandbox Code Playgroud)\n\n我的SConstruct文件:
#include <iostream>\n\nint main(int argc, char **argv) {\n std::cout << "Hello, World!" << std::endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n命令行输出:
\n\n\nenv = Environment(CXX=\'clang++\', CXXFLAGS=[\'-Wall\', \'-g\', …Run Code Online (Sandbox Code Playgroud) 我尝试使用 libclang 解析 C++ 标头,但解析器仅解析类名 - 并将其类型显示为 VarDec1。当文件扩展名从 .h 更改为 .cpp 时,它就可以正常工作。经过几天的搜索找不到答案,有人可以帮我解决这个问题吗?
以下是parser.cpp:
#include <iostream>
#include <clang-c/Index.h> // This is libclang.
using namespace std;
ostream& operator<<(ostream& stream, const CXString& str)
{
stream << clang_getCString(str);
clang_disposeString(str);
return stream;
}
int main()
{
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
"tt.h", nullptr, 0,
nullptr, 0,
CXTranslationUnit_None);
if (unit == nullptr)
{
cerr << "Unable to parse translation unit. Quitting." << endl;
exit(-1);
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(
cursor, …Run Code Online (Sandbox Code Playgroud) 想象一下下面的代码:
for (int i = 0; i < 8; ++i) {
// ... some code
}
Run Code Online (Sandbox Code Playgroud)
我希望这个循环在 MSVC 中展开。在 CLang 中我可以添加#pragma unrollbefore 循环。但是在 MSVC 中如何做同样的事情呢?
我知道无论如何编译器通常会为我展开这个循环,即使没有任何编译指示。但我想真正确定这一点,我想总是展开它。
当然,强制展开的一种方法是使用传入函子的模板化展开函数的递归调用,如以下代码所示:
template <int N, int I = 0, typename F>
inline void Unroll(F const & f) {
if constexpr(I < N) {
f.template operator() <I> ();
Unroll<N, I + 1>(f);
}
}
void f_maybe_not_unrolled() {
int volatile x = 0;
for (int i = 0; i < 8; ++i)
x = …Run Code Online (Sandbox Code Playgroud) 我偶然发现了一个非常奇怪的 struct 成员,它封装在 32 位 clang 中。这是编译器资源管理器实验的链接。
本质上,我有以下结构:
struct _8Bytes
{
uint64_t _8bytes;
};
struct _16Bytes : _8Bytes
{
uint32_t _4bytes;
};
struct Test : _16Bytes
{
uint8_t test;
};
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,它sizeof(_16Bytes)是 16,但是offsetof(Test, test)它是 12,因为编译器决定在 后立即打包它_16Bytes::_4bytes。这非常烦人,我想首先禁用此行为,但就这样吧。
让我困惑的是,如果我_16Bytes按如下方式更改结构:
struct _16Bytes
{
// Same as Test1::_16Bytes, but 8 bytes is now a member
uint64_t _8bytes;
uint32_t _4bytes;
};
Run Code Online (Sandbox Code Playgroud)
然后突然offsetof(Test, test)变成了 16。这对我来说绝对是零——有人能解释一下发生了什么吗?
更重要的是,有没有办法禁用这种恼人的打包行为?
我operator ->和Objective-C 有问题.
我想在ObjC类上使用C++包装器.
所以我创建了我的课程:
@interface User : NSObject
@property (nonatomic, copy) NSString *name;
@end
Run Code Online (Sandbox Code Playgroud)
还有包装类:
class UserWrapper {
User *_user;
// ctors, accessors, etc.
operator User *(){
return _user;
}
User *operator->(){
return _user;
}
};
Run Code Online (Sandbox Code Playgroud)
当我尝试通过operator User*它访问后备对象时,效果很好:
UserWrapper wrapper([User new]);
[wrapper setName:@"alex"];
NSLog(@"%@", [wrapper name]);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试通过访问此对象时 operator ->
UserWrapper wrapper([User new]);
[wrapper setName:@"alex"];
NSLog(@"%@", wrapper->name);
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
Property 'name' found on object of type 'User *'; did you mean to access it with the "." …Run Code Online (Sandbox Code Playgroud) #include <iostream>
int foo(int i){
return foo(i + 1);
}
int main(int argc,char * argv[]){
if(argc != 2){
return 1;
}
std::cout << foo(std::atoi(argv[1])) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
%clang ++ -O2 test.cc
%time./ a.out 42
1490723512
./a.out 42 0.00s用户0.00s系统69%cpu 0.004总计
%time./ a.out 42
1564058296
./a.out 42 0.00s用户0.00s系统56%cpu 0.006总计
%g ++ -O2 test.cc
%./ a.out 42 #infinte递归
^ C
% clang++ --version
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
% g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build …Run Code Online (Sandbox Code Playgroud) 以下代码
#include <vector>
#include <complex>
#include <algorithm>
template<class K>
inline void conjVec(int m, K* const in) {
static_assert(std::is_same<K, double>::value || std::is_same<K, std::complex<double>>::value, "");
if(!std::is_same<typename std::remove_pointer<K>::type, double>::value)
#ifndef OK
std::for_each(in, in + m, [](K& z) { z = std::conj(z); });
#else
std::for_each(reinterpret_cast<std::complex<double>*>(in), reinterpret_cast<std::complex<double>*>(in) + m, [](std::complex<double>& z) { z = std::conj(z); });
#endif
}
int main(int argc, char* argv[]) {
std::vector<double> nums;
nums.emplace_back(1.0);
conjVec(nums.size(), nums.data());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用Linux编译好
在Mac OS X上
但没有
除非 …
我试图编译这段代码(在test.cpp文件中)
#include<tuple>
int main(){
auto [c,d] = make_tuple(3.1,2.3);
}
Run Code Online (Sandbox Code Playgroud)
使用
g++ -std=c++17 test.cpp -o test
,以及
clang++ -std=c++1z test.cpp -o test
两者都会打印错误信息:
test.cpp: In function ‘int main()’:
test.cpp:3:7: error: expected unqualified-id before ‘[’ token
auto [c,d] = make_tuple(3.1,2.3);
Run Code Online (Sandbox Code Playgroud)
使用g ++(Ubuntu 5.4.0-6ubuntu1~16.04.9)5.4.0 20160609和clang版本3.8.0-2ubuntu4(使用Ubuntu 16.04.09)我缺少什么?
我正在尝试 make_pair使用下面示例中的unique_ptr
(该代码是Facade Design模式的C++实现)
#include <unordered_map>
#include <utility>
#include <functional>
using namespace std;
// Step 1 : Design the Interface
class IAccount {
protected:
int accountNo;
int cash;
public:
virtual void deposit(float amount);
virtual void withdraw(float amount);
virtual void transfer(float amount);
virtual int getAccountNumber() = 0;
};
// Step 2 : Implement the interface with one or more subtypes
class Current : public IAccount{
public:
Current(int amt) { cash = amt; }
int getAccountNumber() {
return accountNo;
}
}; …Run Code Online (Sandbox Code Playgroud) 我从clang9.0.0版发出了一个令人讨厌的警告。
function '(anonymous namespace)::B::B' has internal linkage but is not defined [-Wundefined-internal]
Run Code Online (Sandbox Code Playgroud)
(g++给我类似的警告“ X已使用但从未定义”)
其他问题正在谈论inline或static起作用,但这不是我的情况。
这是一个最小的无效示例:
::::::::::::::: A.cpp :::::::::::::::
#include "B.hpp"
namespace {
class A {
public:
bool foo(int& bar) {
B* b = new B(&bar);
return 0;
}
};
}
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
::::::::::::::: B.cpp :::::::::::::::
#include "B.hpp"
namespace {
B::B(int* b) : b(b) {};
}
Run Code Online (Sandbox Code Playgroud)
::::::::::::::: B.hpp :::::::::::::::
#ifndef B_HPP
#define B_HPP
#pragma once
namespace {
class …Run Code Online (Sandbox Code Playgroud) clang++ ×10
c++ ×9
clang ×4
g++ ×2
c ×1
c++11 ×1
c++17 ×1
icc ×1
libclang ×1
llvm-clang ×1
objective-c ×1
visual-c++ ×1