clang 19.0.0git
Classes | Public Member Functions | Static Public Attributes | List of all members
clang::tooling::HeaderIncludes Class Reference

Generates replacements for inserting or deleting #include directives in a file. More...

#include "clang/Tooling/Inclusions/HeaderIncludes.h"

Public Member Functions

 HeaderIncludes (llvm::StringRef FileName, llvm::StringRef Code, const IncludeStyle &Style)
 
std::optional< tooling::Replacementinsert (llvm::StringRef Header, bool IsAngled, IncludeDirective Directive) const
 Inserts an #include or #import directive of Header into the code.
 
tooling::Replacements remove (llvm::StringRef Header, bool IsAngled) const
 Removes all existing #includes and #imports of Header quoted with <> if IsAngled is true or "" if IsAngled is false.
 

Static Public Attributes

static const llvm::Regex IncludeRegex
 

Detailed Description

Generates replacements for inserting or deleting #include directives in a file.

Definition at line 52 of file HeaderIncludes.h.

Constructor & Destructor Documentation

◆ HeaderIncludes()

clang::tooling::HeaderIncludes::HeaderIncludes ( llvm::StringRef  FileName,
llvm::StringRef  Code,
const IncludeStyle Style 
)

Member Function Documentation

◆ insert()

std::optional< tooling::Replacement > clang::tooling::HeaderIncludes::insert ( llvm::StringRef  Header,
bool  IsAngled,
IncludeDirective  Directive 
) const

Inserts an #include or #import directive of Header into the code.

If IsAngled is true, Header will be quoted with <> in the directive; otherwise, it will be quoted with "".

When searching for points to insert new header, this ignores #include's after the #include block(s) in the beginning of a file to avoid inserting headers into code sections where new #include's should not be added by default. These code sections include:

  • raw string literals (containing #include).
  • if blocks.
  • Special #include's among declarations (e.g. functions).

Returns a replacement that inserts the new header into a suitable #include block of the same category. This respects the order of the existing #includes in the block; if the existing #includes are not already sorted, this will simply insert the #include in front of the first #include of the same category in the code that should be sorted after IncludeName. If IncludeName already exists (with exactly the same spelling), this returns std::nullopt.

Definition at line 360 of file HeaderIncludes.cpp.

References clang::FileName, clang::tooling::IncludeCategoryManager::getIncludePriority(), clang::tooling::Include, Iter, and Priority.

◆ remove()

tooling::Replacements clang::tooling::HeaderIncludes::remove ( llvm::StringRef  Header,
bool  IsAngled 
) const

Removes all existing #includes and #imports of Header quoted with <> if IsAngled is true or "" if IsAngled is false.

This doesn't resolve the header file path; it only deletes #includes and #imports with exactly the same spelling.

Definition at line 405 of file HeaderIncludes.cpp.

References clang::FileName, Iter, and clang::Result.

Member Data Documentation

◆ IncludeRegex

const llvm::Regex clang::tooling::HeaderIncludes::IncludeRegex
static

Definition at line 87 of file HeaderIncludes.h.

Referenced by HeaderIncludes(), and clang::format::sortCppIncludes().


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