1//==- AnnexKDetection.cpp - Annex K availability detection -------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of utilities for detecting C11 Annex K
10// (Bounds-checking interfaces) availability.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/AnnexKDetection.h"
15
16#include "clang/Basic/LangOptions.h"
17#include "clang/Lex/Preprocessor.h"
18
19namespace clang::analysis {
20
21[[nodiscard]] bool isAnnexKAvailable(Preprocessor *PP, const LangOptions &LO) {
22 if (!LO.C11)
23 return false;
24
25 assert(PP && "No Preprocessor registered.");
26
27 if (!PP->isMacroDefined(Id: "__STDC_LIB_EXT1__") ||
28 !PP->isMacroDefined(Id: "__STDC_WANT_LIB_EXT1__"))
29 return false;
30
31 const auto *MI =
32 PP->getMacroInfo(II: PP->getIdentifierInfo(Name: "__STDC_WANT_LIB_EXT1__"));
33 if (!MI || MI->tokens_empty())
34 return false;
35
36 const Token &T = MI->tokens().back();
37 if (!T.isLiteral() || !T.getLiteralData())
38 return false;
39
40 return StringRef(T.getLiteralData(), T.getLength()) == "1";
41}
42
43} // namespace clang::analysis
44