繁体   English   中英

VSCode:为 static 分析设置 C/C++ 预处理器宏

[英]VSCode: Set C/C++ preprocessor macro for static analysis

我正在开发一个库,它允许用户设置一个关键的类型别名,或者通过预处理器指令来完成。 这种类型的别名(或指令)在库中未按设计声明。 因此,在开发我的代码时,我会收到烦人的错误消息和这种未声明类型的曲线。 如果我在某处为它声明一个临时类型,则可以避免这种情况。 但是,我不喜欢在使用代码时声明它,然后在发布时将其删除。 它也容易出错,因为我很容易忘记删除它。

我的问题是:我可以为 VS Code 的 static 分析(IntelliSense?C/C++ 扩展)定义预处理器指令吗?

这将让我将分析视为定义明确的类型别名会产生什么。 并避免烦人的错误消息/曲线。


最小的可重现示例:

在线编译器示例

tCore.hpp

#pragma once

#include <string>

// User is responsible of declaring the tCore type

// tCore interface methods 
template<typename TCore>
std::string printImpl();

tPrint.hpp

#pragma once

#include <iostream>

class tPrint {
  public:
    tPrint() = default;
      
    void print() const {
        std::cout << printImpl<tCore>() << std::endl; // <-- Static analysis error here!
    }
};

tFoo.hpp - tCore 候选

#pragma once

#include <string>
#include "tCore.hpp"

struct tFoo {};

template<>
std::string printImpl<tFoo>() {
    return "tFoo";
}

主文件

#include <tFoo.hpp>

using tCore = tFoo;

int main() {
    tPrint p{};
    p.print();  // -> "tFoo"
    return 0;
}

我发现是IntelliSense通过C/C++ Extension导致了错误。

我还找到了将编译器 arguments 添加到IntelliSense的选项,这正是我想要的。

通过 UI:

按 F1 -> > C/C++: Edit Configurations (UI) -> 向下滚动到Defines

或通过 JSON:

c_cpp_properties.json配置有一个字段defines ,其中包含任何编译器 arguments。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM