簡體   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