clang 23.0.0git
AnnexKDetection.cpp
Go to the documentation of this file.
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
15
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("__STDC_LIB_EXT1__") ||
28 !PP->isMacroDefined("__STDC_WANT_LIB_EXT1__"))
29 return false;
30
31 const auto *MI =
32 PP->getMacroInfo(PP->getIdentifierInfo("__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
Defines the clang::LangOptions interface.
Defines the clang::Preprocessor interface.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
bool isMacroDefined(StringRef Id)
Token - This structure provides full information about a lexed token.
Definition Token.h:36
bool isAnnexKAvailable(Preprocessor *PP, const LangOptions &LO)
Calculates whether Annex K is available for the current translation unit based on the macro definitio...
const FunctionProtoType * T