clang 22.0.0git
clang::NoTrivialPPDirectiveTracer Class Reference

Consider the following code: More...

#include "clang/Lex/NoTrivialPPDirectiveTracer.h"

Inheritance diagram for clang::NoTrivialPPDirectiveTracer:
[legend]

Public Member Functions

 NoTrivialPPDirectiveTracer (Preprocessor &P)
bool hasSeenNoTrivialPPDirective () const
void LexedFileChanged (FileID FID, LexedFileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID, SourceLocation Loc) override
 Callback invoked whenever the Lexer moves to a different file for lexing.
void EmbedDirective (SourceLocation HashLoc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, const LexEmbedParametersResult &Params) override
 Callback invoked whenever an embed directive has been processed, regardless of whether the embed will actually find a file.
void InclusionDirective (SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule, bool ModuleImported, SrcMgr::CharacteristicKind FileType) override
 Callback invoked whenever an inclusion directive of any kind (#include, #import, etc.) has been processed, regardless of whether the inclusion will actually result in an inclusion.
void moduleImport (SourceLocation ImportLoc, ModuleIdPath Path, const Module *Imported) override
 Callback invoked whenever there was an explicit module-import syntax.
void EndOfMainFile () override
 Callback invoked when the end of the main file is reached.
void PragmaDirective (SourceLocation Loc, PragmaIntroducerKind Introducer) override
 Callback invoked when start reading any pragma directive.
void MacroExpands (const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
 Called by Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is found.
void MacroDefined (const Token &MacroNameTok, const MacroDirective *MD) override
 Hook called whenever a macro definition is seen.
void MacroUndefined (const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) override
 Hook called whenever a macro #undef is seen.
void Defined (const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range) override
 Hook called whenever the 'defined' operator is seen.
void If (SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue) override
 Hook called whenever an #if is seen.
void Elif (SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) override
 Hook called whenever an #elif is seen.
void Ifdef (SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
 Hook called whenever an #ifdef is seen.
void Elifdef (SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
 Hook called whenever an #elifdef branch is taken.
void Elifdef (SourceLocation Loc, SourceRange ConditionRange, SourceLocation IfLoc) override
 Hook called whenever an #elifdef is skipped.
void Ifndef (SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
 Hook called whenever an #ifndef is seen.
void Elifndef (SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
 Hook called whenever an #elifndef branch is taken.
void Elifndef (SourceLocation Loc, SourceRange ConditionRange, SourceLocation IfLoc) override
 Hook called whenever an #elifndef is skipped.
void Else (SourceLocation Loc, SourceLocation IfLoc) override
 Hook called whenever an #else is seen.
void Endif (SourceLocation Loc, SourceLocation IfLoc) override
 Hook called whenever an #endif is seen.
Public Member Functions inherited from clang::PPCallbacks
virtual ~PPCallbacks ()
virtual void FileChanged (SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID=FileID())
 Callback invoked whenever a source file is entered or exited.
virtual void FileSkipped (const FileEntryRef &SkippedFile, const Token &FilenameTok, SrcMgr::CharacteristicKind FileType)
 Callback invoked whenever a source file is skipped as the result of header guard optimization.
virtual bool EmbedFileNotFound (StringRef FileName)
 Callback invoked whenever the preprocessor cannot find a file for an embed directive.
virtual bool FileNotFound (StringRef FileName)
 Callback invoked whenever the preprocessor cannot find a file for an inclusion directive.
virtual void EnteredSubmodule (Module *M, SourceLocation ImportLoc, bool ForPragma)
 Callback invoked whenever a submodule was entered.
virtual void LeftSubmodule (Module *M, SourceLocation ImportLoc, bool ForPragma)
 Callback invoked whenever a submodule was left.
virtual void Ident (SourceLocation Loc, StringRef str)
 Callback invoked when a #ident or #sccs directive is read.
virtual void PragmaComment (SourceLocation Loc, const IdentifierInfo *Kind, StringRef Str)
 Callback invoked when a #pragma comment directive is read.
virtual void PragmaMark (SourceLocation Loc, StringRef Trivia)
 Callback invoked when a #pragma mark comment is read.
virtual void PragmaDetectMismatch (SourceLocation Loc, StringRef Name, StringRef Value)
 Callback invoked when a #pragma detect_mismatch directive is read.
virtual void PragmaDebug (SourceLocation Loc, StringRef DebugType)
 Callback invoked when a #pragma clang __debug directive is read.
virtual void PragmaMessage (SourceLocation Loc, StringRef Namespace, PragmaMessageKind Kind, StringRef Str)
 Callback invoked when a #pragma message directive is read.
virtual void PragmaDiagnosticPush (SourceLocation Loc, StringRef Namespace)
 Callback invoked when a #pragma gcc diagnostic push directive is read.
virtual void PragmaDiagnosticPop (SourceLocation Loc, StringRef Namespace)
 Callback invoked when a #pragma gcc diagnostic pop directive is read.
virtual void PragmaDiagnostic (SourceLocation Loc, StringRef Namespace, diag::Severity mapping, StringRef Str)
 Callback invoked when a #pragma gcc diagnostic directive is read.
virtual void PragmaOpenCLExtension (SourceLocation NameLoc, const IdentifierInfo *Name, SourceLocation StateLoc, unsigned State)
 Called when an OpenCL extension is either disabled or enabled with a pragma.
virtual void PragmaWarning (SourceLocation Loc, PragmaWarningSpecifier WarningSpec, ArrayRef< int > Ids)
virtual void PragmaWarningPush (SourceLocation Loc, int Level)
 Callback invoked when a #pragma warning(push) directive is read.
virtual void PragmaWarningPop (SourceLocation Loc)
 Callback invoked when a #pragma warning(pop) directive is read.
virtual void PragmaExecCharsetPush (SourceLocation Loc, StringRef Str)
 Callback invoked when a #pragma execution_character_set(push) directive is read.
virtual void PragmaExecCharsetPop (SourceLocation Loc)
 Callback invoked when a #pragma execution_character_set(pop) directive is read.
virtual void PragmaAssumeNonNullBegin (SourceLocation Loc)
 Callback invoked when a #pragma clang assume_nonnull begin directive is read.
virtual void PragmaAssumeNonNullEnd (SourceLocation Loc)
 Callback invoked when a #pragma clang assume_nonnull end directive is read.
virtual void HasEmbed (SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File)
 Hook called when a '__has_embed' directive is read.
virtual void HasInclude (SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, SrcMgr::CharacteristicKind FileType)
 Hook called when a '__has_include' or '__has_include_next' directive is read.
virtual void SourceRangeSkipped (SourceRange Range, SourceLocation EndifLoc)
 Hook called when a source range is skipped.

Additional Inherited Members

Public Types inherited from clang::PPCallbacks
enum  FileChangeReason { EnterFile , ExitFile , SystemHeaderPragma , RenameFile }
enum class  LexedFileChangeReason { EnterFile , ExitFile }
enum  PragmaMessageKind { PMK_Message , PMK_Warning , PMK_Error }
 Determines the kind of #pragma invoking a call to PragmaMessage. More...
enum  PragmaWarningSpecifier {
  PWS_Default , PWS_Disable , PWS_Error , PWS_Once ,
  PWS_Suppress , PWS_Level1 , PWS_Level2 , PWS_Level3 ,
  PWS_Level4
}
 Callback invoked when a #pragma warning directive is read. More...
enum  ConditionValueKind { CVK_NotEvaluated , CVK_False , CVK_True }

Detailed Description

Consider the following code:

1 FILE 1 3

export module a;

According to the wording in P1857R3:

A module directive may only appear as the first preprocessing tokens in a file (excluding the global module fragment.)

and the wording in [cpp.pre]: module-file: pp-global-module-fragment[opt] pp-module group[opt] pp-private-module-fragment[opt]

# is the first pp-token in the translation unit, and it was rejected by clang, but they really should be exempted from this rule. The goal is to not allow any preprocessor conditionals or most state changes, but these don't fit that.

State change would mean most semantically observable preprocessor state, particularly anything that is order dependent. Global flags like being a system header/module shouldn't matter.

We should exempt a brunch of directives, even though it violates the current standard wording.

This class used to trace 'no-trivial' pp-directives in main file, which may change the preprocessing state.

FIXME: Once the wording of the standard is revised, we need to follow the wording of the standard. Currently this is just a workaround

Definition at line 55 of file NoTrivialPPDirectiveTracer.h.

Constructor & Destructor Documentation

◆ NoTrivialPPDirectiveTracer()

clang::NoTrivialPPDirectiveTracer::NoTrivialPPDirectiveTracer ( Preprocessor & P)
inline

Definition at line 68 of file NoTrivialPPDirectiveTracer.h.

Member Function Documentation

◆ Defined()

void clang::NoTrivialPPDirectiveTracer::Defined ( const Token & MacroNameTok,
const MacroDefinition & MD,
SourceRange Range )
inlineoverridevirtual

Hook called whenever the 'defined' operator is seen.

Parameters
MDThe MacroDirective if the name was a macro, null otherwise.

Reimplemented from clang::PPCallbacks.

Definition at line 212 of file NoTrivialPPDirectiveTracer.h.

◆ Elif()

void clang::NoTrivialPPDirectiveTracer::Elif ( SourceLocation Loc,
SourceRange ConditionRange,
ConditionValueKind ConditionValue,
SourceLocation IfLoc )
inlineoverridevirtual

Hook called whenever an #elif is seen.

Parameters
Locthe source location of the directive.
ConditionRangeThe SourceRange of the expression being tested.
ConditionValueThe evaluated value of the condition.
IfLocthe source location of the #if/#ifdef/#ifndef directive.

Reimplemented from clang::PPCallbacks.

Definition at line 234 of file NoTrivialPPDirectiveTracer.h.

◆ Elifdef() [1/2]

void clang::NoTrivialPPDirectiveTracer::Elifdef ( SourceLocation Loc,
const Token & MacroNameTok,
const MacroDefinition & MD )
inlineoverridevirtual

Hook called whenever an #elifdef branch is taken.

Parameters
Locthe source location of the directive.
MacroNameTokInformation on the token being tested.
MDThe MacroDefinition if the name was a macro, null otherwise.

Reimplemented from clang::PPCallbacks.

Definition at line 252 of file NoTrivialPPDirectiveTracer.h.

◆ Elifdef() [2/2]

void clang::NoTrivialPPDirectiveTracer::Elifdef ( SourceLocation Loc,
SourceRange ConditionRange,
SourceLocation IfLoc )
inlineoverridevirtual

Hook called whenever an #elifdef is skipped.

Parameters
Locthe source location of the directive.
ConditionRangeThe SourceRange of the expression being tested.
IfLocthe source location of the #if/#ifdef/#ifndef directive.

Reimplemented from clang::PPCallbacks.

Definition at line 261 of file NoTrivialPPDirectiveTracer.h.

◆ Elifndef() [1/2]

void clang::NoTrivialPPDirectiveTracer::Elifndef ( SourceLocation Loc,
const Token & MacroNameTok,
const MacroDefinition & MD )
inlineoverridevirtual

Hook called whenever an #elifndef branch is taken.

Parameters
Locthe source location of the directive.
MacroNameTokInformation on the token being tested.
MDThe MacroDefinition if the name was a macro, null otherwise.

Reimplemented from clang::PPCallbacks.

Definition at line 279 of file NoTrivialPPDirectiveTracer.h.

◆ Elifndef() [2/2]

void clang::NoTrivialPPDirectiveTracer::Elifndef ( SourceLocation Loc,
SourceRange ConditionRange,
SourceLocation IfLoc )
inlineoverridevirtual

Hook called whenever an #elifndef is skipped.

Parameters
Locthe source location of the directive.
ConditionRangeThe SourceRange of the expression being tested.
IfLocthe source location of the #if/#ifdef/#ifndef directive.

Reimplemented from clang::PPCallbacks.

Definition at line 288 of file NoTrivialPPDirectiveTracer.h.

◆ Else()

void clang::NoTrivialPPDirectiveTracer::Else ( SourceLocation Loc,
SourceLocation IfLoc )
inlineoverridevirtual

Hook called whenever an #else is seen.

Parameters
Locthe source location of the directive.
IfLocthe source location of the #if/#ifdef/#ifndef directive.

Reimplemented from clang::PPCallbacks.

Definition at line 296 of file NoTrivialPPDirectiveTracer.h.

◆ EmbedDirective()

void clang::NoTrivialPPDirectiveTracer::EmbedDirective ( SourceLocation HashLoc,
StringRef FileName,
bool IsAngled,
OptionalFileEntryRef File,
const LexEmbedParametersResult & Params )
inlineoverridevirtual

Callback invoked whenever an embed directive has been processed, regardless of whether the embed will actually find a file.

Parameters
HashLocThe location of the '#' that starts the embed directive.
FileNameThe name of the file being included, as written in the source code.
IsAngledWhether the file name was enclosed in angle brackets; otherwise, it was enclosed in quotes.
FileThe actual file that may be included by this embed directive.
ParamsThe parameters used by the directive.

Reimplemented from clang::PPCallbacks.

Definition at line 105 of file NoTrivialPPDirectiveTracer.h.

References clang::File, and clang::FileName.

◆ Endif()

void clang::NoTrivialPPDirectiveTracer::Endif ( SourceLocation Loc,
SourceLocation IfLoc )
inlineoverridevirtual

Hook called whenever an #endif is seen.

Parameters
Locthe source location of the directive.
IfLocthe source location of the #if/#ifdef/#ifndef directive.

Reimplemented from clang::PPCallbacks.

Definition at line 303 of file NoTrivialPPDirectiveTracer.h.

◆ EndOfMainFile()

void clang::NoTrivialPPDirectiveTracer::EndOfMainFile ( )
inlineoverridevirtual

Callback invoked when the end of the main file is reached.

No subsequent callbacks will be made.

Reimplemented from clang::PPCallbacks.

Definition at line 182 of file NoTrivialPPDirectiveTracer.h.

◆ hasSeenNoTrivialPPDirective()

bool NoTrivialPPDirectiveTracer::hasSeenNoTrivialPPDirective ( ) const

Definition at line 1696 of file Preprocessor.cpp.

◆ If()

void clang::NoTrivialPPDirectiveTracer::If ( SourceLocation Loc,
SourceRange ConditionRange,
ConditionValueKind ConditionValue )
inlineoverridevirtual

Hook called whenever an #if is seen.

Parameters
Locthe source location of the directive.
ConditionRangeThe SourceRange of the expression being tested.
ConditionValueThe evaluated value of the condition.

Reimplemented from clang::PPCallbacks.

Definition at line 223 of file NoTrivialPPDirectiveTracer.h.

◆ Ifdef()

void clang::NoTrivialPPDirectiveTracer::Ifdef ( SourceLocation Loc,
const Token & MacroNameTok,
const MacroDefinition & MD )
inlineoverridevirtual

Hook called whenever an #ifdef is seen.

Parameters
Locthe source location of the directive.
MacroNameTokInformation on the token being tested.
MDThe MacroDefinition if the name was a macro, null otherwise.

Reimplemented from clang::PPCallbacks.

Definition at line 243 of file NoTrivialPPDirectiveTracer.h.

◆ Ifndef()

void clang::NoTrivialPPDirectiveTracer::Ifndef ( SourceLocation Loc,
const Token & MacroNameTok,
const MacroDefinition & MD )
inlineoverridevirtual

Hook called whenever an #ifndef is seen.

Parameters
Locthe source location of the directive.
MacroNameTokInformation on the token being tested.
MDThe MacroDefiniton if the name was a macro, null otherwise.

Reimplemented from clang::PPCallbacks.

Definition at line 270 of file NoTrivialPPDirectiveTracer.h.

◆ InclusionDirective()

void clang::NoTrivialPPDirectiveTracer::InclusionDirective ( SourceLocation HashLoc,
const Token & IncludeTok,
StringRef FileName,
bool IsAngled,
CharSourceRange FilenameRange,
OptionalFileEntryRef File,
StringRef SearchPath,
StringRef RelativePath,
const Module * SuggestedModule,
bool ModuleImported,
SrcMgr::CharacteristicKind FileType )
inlineoverridevirtual

Callback invoked whenever an inclusion directive of any kind (#include, #import, etc.) has been processed, regardless of whether the inclusion will actually result in an inclusion.

Parameters
HashLocThe location of the '#' that starts the inclusion directive.
IncludeTokThe token that indicates the kind of inclusion directive, e.g., 'include' or 'import'.
FileNameThe name of the file being included, as written in the source code.
IsAngledWhether the file name was enclosed in angle brackets; otherwise, it was enclosed in quotes.
FilenameRangeThe character range of the quotes or angle brackets for the written file name.
FileThe actual file that may be included by this inclusion directive.
SearchPathContains the search path which was used to find the file in the file system. If the file was found via an absolute include path, SearchPath will be empty. For framework includes, the SearchPath and RelativePath will be split up. For example, if an include of "Some/Some.h" is found via the framework path "path/to/Frameworks/Some.framework/Headers/Some.h", SearchPath will be "path/to/Frameworks/Some.framework/Headers" and RelativePath will be "Some.h".
RelativePathThe path relative to SearchPath, at which the include file was found. This is equal to FileName except for framework includes.
SuggestedModuleThe module suggested for this header, if any.
ModuleImportedWhether this include was translated into import of SuggestedModule.
FileTypeThe characteristic kind, indicates whether a file or directory holds normal user code, system code, or system code which is implicitly 'extern "C"' in C++ mode.

Reimplemented from clang::PPCallbacks.

Definition at line 154 of file NoTrivialPPDirectiveTracer.h.

References clang::File, and clang::FileName.

◆ LexedFileChanged()

void NoTrivialPPDirectiveTracer::LexedFileChanged ( FileID FID,
LexedFileChangeReason Reason,
SrcMgr::CharacteristicKind FileType,
FileID PrevFID,
SourceLocation Loc )
overridevirtual

Callback invoked whenever the Lexer moves to a different file for lexing.

Unlike FileChanged line number directives and other related pragmas do not trigger callbacks to LexedFileChanged.

Parameters
FIDThe FileID that the Lexer moved to.
ReasonWhether the Lexer entered a new file or exited one.
FileTypeThe CharacteristicKind of the file the Lexer moved to.
PrevFIDThe FileID the Lexer was using before the change.
LocThe location where the Lexer entered a new file from or the location that the Lexer moved into after exiting a file.

Reimplemented from clang::PPCallbacks.

Definition at line 1705 of file Preprocessor.cpp.

◆ MacroDefined()

void clang::NoTrivialPPDirectiveTracer::MacroDefined ( const Token & MacroNameTok,
const MacroDirective * MD )
inlineoverridevirtual

Hook called whenever a macro definition is seen.

Reimplemented from clang::PPCallbacks.

Definition at line 194 of file NoTrivialPPDirectiveTracer.h.

◆ MacroExpands()

void NoTrivialPPDirectiveTracer::MacroExpands ( const Token & MacroNameTok,
const MacroDefinition & MD,
SourceRange Range,
const MacroArgs * Args )
overridevirtual

Called by Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is found.

Reimplemented from clang::PPCallbacks.

Definition at line 1711 of file Preprocessor.cpp.

References clang::MacroDefinition::getMacroInfo(), and clang::MacroInfo::isBuiltinMacro().

◆ MacroUndefined()

void clang::NoTrivialPPDirectiveTracer::MacroUndefined ( const Token & MacroNameTok,
const MacroDefinition & MD,
const MacroDirective * Undef )
inlineoverridevirtual

Hook called whenever a macro #undef is seen.

Parameters
MacroNameTokThe active Token
MDA MacroDefinition for the named macro.
UndefNew MacroDirective if the macro was defined, null otherwise.

MD is released immediately following this callback.

Reimplemented from clang::PPCallbacks.

Definition at line 205 of file NoTrivialPPDirectiveTracer.h.

◆ moduleImport()

void clang::NoTrivialPPDirectiveTracer::moduleImport ( SourceLocation ImportLoc,
ModuleIdPath Path,
const Module * Imported )
inlineoverridevirtual

Callback invoked whenever there was an explicit module-import syntax.

Parameters
ImportLocThe location of import directive token.
PathThe identifiers (and their locations) of the module "path", e.g., "std.vector" would be split into "std" and "vector".
ImportedThe imported module; can be null if importing failed.

Reimplemented from clang::PPCallbacks.

Definition at line 174 of file NoTrivialPPDirectiveTracer.h.

◆ PragmaDirective()

void clang::NoTrivialPPDirectiveTracer::PragmaDirective ( SourceLocation Loc,
PragmaIntroducerKind Introducer )
inlineoverridevirtual

Callback invoked when start reading any pragma directive.

Reimplemented from clang::PPCallbacks.

Definition at line 185 of file NoTrivialPPDirectiveTracer.h.


The documentation for this class was generated from the following files: