clang 22.0.0git
PPMacroExpansion.cpp File Reference
#include "clang/Basic/AttributeCommonInfo.h"
#include "clang/Basic/Attributes.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/CodeCompletionHandler.h"
#include "clang/Lex/DirectoryLookup.h"
#include "clang/Lex/ExternalPreprocessorSource.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/LexDiagnostic.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/MacroArgs.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorLexer.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <optional>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include "clang/Basic/Features.def"
#include "clang/Basic/TokenKinds.def"
#include "clang/Basic/BuiltinTemplates.inc"

Go to the source code of this file.

Macros

#define FEATURE(Name, Predicate)
#define EXTENSION(Name, Predicate)
#define TYPE_TRAIT_1(Spelling, Name, Key)
#define TYPE_TRAIT_2(Spelling, Name, Key)
#define TYPE_TRAIT_N(Spelling, Name, Key)
#define ARRAY_TYPE_TRAIT(Spelling, Name, Key)
#define EXPRESSION_TRAIT(Spelling, Name, Key)
#define TRANSFORM_TYPE_TRAIT_DEF(K, Spelling)
#define BuiltinTemplate(BTName)

Enumerations

enum  Bracket { Brace , Paren }

Functions

static bool isTrivialSingleTokenExpansion (const MacroInfo *MI, const IdentifierInfo *MacroIdent, Preprocessor &PP)
 isTrivialSingleTokenExpansion - Return true if MI, which has a single token in its expansion, currently expands to that token literally.
static bool CheckMatchedBrackets (const SmallVectorImpl< Token > &Tokens)
 CheckMatchedBrackets - Returns true if the braces and parentheses in the token vector are properly nested.
static bool GenerateNewArgTokens (Preprocessor &PP, SmallVectorImpl< Token > &OldTokens, SmallVectorImpl< Token > &NewTokens, unsigned &NumArgs, SmallVectorImpl< SourceRange > &ParenHints, SmallVectorImpl< SourceRange > &InitLists)
 GenerateNewArgTokens - Returns true if OldTokens can be converted to a new vector of tokens in NewTokens.
static void ComputeDATE_TIME (SourceLocation &DATELoc, SourceLocation &TIMELoc, Preprocessor &PP)
 ComputeDATE_TIME - Compute the current time, enter it into the specified scratch buffer, then return DATELoc/TIMELoc locations with the position of the identifier tokens inserted.
static bool HasFeature (const Preprocessor &PP, StringRef Feature)
 HasFeature - Return true if we recognize and implement the feature specified by the identifier as a standard language feature.
static bool HasExtension (const Preprocessor &PP, StringRef Extension)
 HasExtension - Return true if we recognize and implement the feature specified by the identifier, either as an extension or a standard language feature.
static bool EvaluateHasIncludeCommon (Token &Tok, IdentifierInfo *II, Preprocessor &PP, ConstSearchDirIterator LookupFrom, const FileEntry *LookupFromFile)
 EvaluateHasIncludeCommon - Process a '__has_include("path")' or '__has_include_next("path")' expression.
static void EvaluateFeatureLikeBuiltinMacro (llvm::raw_svector_ostream &OS, Token &Tok, IdentifierInfo *II, Preprocessor &PP, bool ExpandArgs, llvm::function_ref< int(Token &Tok, bool &HasLexedNextTok)> Op)
 Process single-argument builtin feature-like macros that return integer values.
static IdentifierInfoExpectFeatureIdentifierInfo (Token &Tok, Preprocessor &PP, signed DiagID)
 Helper function to return the IdentifierInfo structure of a Token or generate a diagnostic if none available.
static bool isTargetArch (const TargetInfo &TI, const IdentifierInfo *II)
 Implements the __is_target_arch builtin macro.
static bool isTargetVendor (const TargetInfo &TI, const IdentifierInfo *II)
 Implements the __is_target_vendor builtin macro.
static bool isTargetOS (const TargetInfo &TI, const IdentifierInfo *II)
 Implements the __is_target_os builtin macro.
static bool isTargetEnvironment (const TargetInfo &TI, const IdentifierInfo *II)
 Implements the __is_target_environment builtin macro.
static bool isTargetVariantOS (const TargetInfo &TI, const IdentifierInfo *II)
 Implements the __is_target_variant_os builtin macro.
static bool isTargetVariantEnvironment (const TargetInfo &TI, const IdentifierInfo *II)
 Implements the __is_target_variant_environment builtin macro.
static bool IsBuiltinTrait (Token &Tok)

Macro Definition Documentation

◆ ARRAY_TYPE_TRAIT

#define ARRAY_TYPE_TRAIT ( Spelling,
Name,
Key )
Value:
case tok::kw_##Spelling: \
return true;

◆ BuiltinTemplate

#define BuiltinTemplate ( BTName)
Value:
.Case(#BTName, getLangOpts().CPlusPlus)
@ CPlusPlus

◆ EXPRESSION_TRAIT

#define EXPRESSION_TRAIT ( Spelling,
Name,
Key )
Value:
case tok::kw_##Spelling: \
return true;

◆ EXTENSION

#define EXTENSION ( Name,
Predicate )
Value:
.Case(#Name, Predicate)

◆ FEATURE

#define FEATURE ( Name,
Predicate )
Value:
.Case(#Name, Predicate)

◆ TRANSFORM_TYPE_TRAIT_DEF

#define TRANSFORM_TYPE_TRAIT_DEF ( K,
Spelling )
Value:
case tok::kw___##Spelling: \
return true;

◆ TYPE_TRAIT_1

#define TYPE_TRAIT_1 ( Spelling,
Name,
Key )
Value:
case tok::kw_##Spelling: \
return true;

◆ TYPE_TRAIT_2

#define TYPE_TRAIT_2 ( Spelling,
Name,
Key )
Value:
case tok::kw_##Spelling: \
return true;

◆ TYPE_TRAIT_N

#define TYPE_TRAIT_N ( Spelling,
Name,
Key )
Value:
case tok::kw_##Spelling: \
return true;

Enumeration Type Documentation

◆ Bracket

enum Bracket
Enumerator
Brace 
Paren 

Definition at line 582 of file PPMacroExpansion.cpp.

Function Documentation

◆ CheckMatchedBrackets()

bool CheckMatchedBrackets ( const SmallVectorImpl< Token > & Tokens)
static

CheckMatchedBrackets - Returns true if the braces and parentheses in the token vector are properly nested.

Definition at line 589 of file PPMacroExpansion.cpp.

References Brace, and Paren.

Referenced by GenerateNewArgTokens().

◆ ComputeDATE_TIME()

void ComputeDATE_TIME ( SourceLocation & DATELoc,
SourceLocation & TIMELoc,
Preprocessor & PP )
static

ComputeDATE_TIME - Compute the current time, enter it into the specified scratch buffer, then return DATELoc/TIMELoc locations with the position of the identifier tokens inserted.

Definition at line 1043 of file PPMacroExpansion.cpp.

References clang::Preprocessor::CreateString(), clang::Token::getLocation(), clang::Preprocessor::getPreprocessorOpts(), clang::PreprocessorOptions::SourceDateEpoch, and clang::Token::startToken().

◆ EvaluateFeatureLikeBuiltinMacro()

void EvaluateFeatureLikeBuiltinMacro ( llvm::raw_svector_ostream & OS,
Token & Tok,
IdentifierInfo * II,
Preprocessor & PP,
bool ExpandArgs,
llvm::function_ref< int(Token &Tok, bool &HasLexedNextTok)> Op )
static

Process single-argument builtin feature-like macros that return integer values.

Definition at line 1341 of file PPMacroExpansion.cpp.

References clang::Preprocessor::Diag(), Diag(), clang::Token::getIdentifierInfo(), clang::Token::getKind(), clang::Token::getLocation(), clang::Preprocessor::Lex(), clang::Preprocessor::LexUnexpandedToken(), and Tok.

◆ EvaluateHasIncludeCommon()

◆ ExpectFeatureIdentifierInfo()

IdentifierInfo * ExpectFeatureIdentifierInfo ( Token & Tok,
Preprocessor & PP,
signed DiagID )
static

Helper function to return the IdentifierInfo structure of a Token or generate a diagnostic if none available.

Definition at line 1451 of file PPMacroExpansion.cpp.

References clang::Preprocessor::Diag(), and Tok.

◆ GenerateNewArgTokens()

bool GenerateNewArgTokens ( Preprocessor & PP,
SmallVectorImpl< Token > & OldTokens,
SmallVectorImpl< Token > & NewTokens,
unsigned & NumArgs,
SmallVectorImpl< SourceRange > & ParenHints,
SmallVectorImpl< SourceRange > & InitLists )
static

GenerateNewArgTokens - Returns true if OldTokens can be converted to a new vector of tokens in NewTokens.

The new number of arguments will be placed in NumArgs and the ranges which need to surrounded in parentheses will be in ParenHints. Returns false if the token stream cannot be changed. If this is because of an initializer list starting a macro argument, the range of those initializer lists will be place in InitLists.

Definition at line 618 of file PPMacroExpansion.cpp.

References clang::Braces, CheckMatchedBrackets(), clang::Preprocessor::getLocForEndOfToken(), clang::Token::setKind(), clang::Token::setLength(), clang::Token::setLocation(), and clang::Token::startToken().

◆ HasExtension()

bool HasExtension ( const Preprocessor & PP,
StringRef Extension )
static

HasExtension - Return true if we recognize and implement the feature specified by the identifier, either as an extension or a standard language feature.

Definition at line 1108 of file PPMacroExpansion.cpp.

References clang::diag::Error, clang::Preprocessor::getDiagnostics(), clang::DiagnosticsEngine::getExtensionHandlingBehavior(), clang::Preprocessor::getLangOpts(), and HasFeature().

◆ HasFeature()

bool HasFeature ( const Preprocessor & PP,
StringRef Feature )
static

HasFeature - Return true if we recognize and implement the feature specified by the identifier as a standard language feature.

Definition at line 1090 of file PPMacroExpansion.cpp.

References Feature, and clang::Preprocessor::getLangOpts().

Referenced by clang::SemaRISCV::handleInterruptAttr(), HasExtension(), and hasFeature().

◆ IsBuiltinTrait()

bool IsBuiltinTrait ( Token & Tok)
static

Definition at line 1555 of file PPMacroExpansion.cpp.

References Tok.

◆ isTargetArch()

bool isTargetArch ( const TargetInfo & TI,
const IdentifierInfo * II )
static

Implements the __is_target_arch builtin macro.

Definition at line 1463 of file PPMacroExpansion.cpp.

References Arch, clang::IdentifierInfo::getName(), and clang::TargetInfo::getTriple().

◆ isTargetEnvironment()

bool isTargetEnvironment ( const TargetInfo & TI,
const IdentifierInfo * II )
static

Implements the __is_target_environment builtin macro.

Definition at line 1502 of file PPMacroExpansion.cpp.

References clang::IdentifierInfo::getName(), and clang::TargetInfo::getTriple().

◆ isTargetOS()

bool isTargetOS ( const TargetInfo & TI,
const IdentifierInfo * II )
static

Implements the __is_target_os builtin macro.

Definition at line 1492 of file PPMacroExpansion.cpp.

References clang::IdentifierInfo::getName(), and clang::TargetInfo::getTriple().

◆ isTargetVariantEnvironment()

bool isTargetVariantEnvironment ( const TargetInfo & TI,
const IdentifierInfo * II )
static

Implements the __is_target_variant_environment builtin macro.

Definition at line 1531 of file PPMacroExpansion.cpp.

References clang::TargetInfo::getDarwinTargetVariantTriple(), clang::IdentifierInfo::getName(), and clang::TargetInfo::getTriple().

◆ isTargetVariantOS()

bool isTargetVariantOS ( const TargetInfo & TI,
const IdentifierInfo * II )
static

Implements the __is_target_variant_os builtin macro.

Definition at line 1514 of file PPMacroExpansion.cpp.

References clang::TargetInfo::getDarwinTargetVariantTriple(), clang::IdentifierInfo::getName(), and clang::TargetInfo::getTriple().

◆ isTargetVendor()

bool isTargetVendor ( const TargetInfo & TI,
const IdentifierInfo * II )
static

Implements the __is_target_vendor builtin macro.

Definition at line 1484 of file PPMacroExpansion.cpp.

References clang::IdentifierInfo::getName(), and clang::TargetInfo::getTriple().

◆ isTrivialSingleTokenExpansion()

bool isTrivialSingleTokenExpansion ( const MacroInfo * MI,
const IdentifierInfo * MacroIdent,
Preprocessor & PP )
static