13#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
14#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
19#include "clang/Basic/CharInfo.h"
20#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceLocation.h"
23#include "clang/Basic/SourceManager.h"
24#include "clang/Format/Format.h"
25#include "clang/Lex/HeaderSearch.h"
26#include "clang/Tooling/Core/Replacement.h"
27#include "clang/Tooling/Syntax/Tokens.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/StringSet.h"
30#include "llvm/Support/Error.h"
44std::optional<FileDigest>
digestFile(
const SourceManager &SM, FileID FID);
65 bool AllowColumnsBeyondLineLength =
true);
91SourceLocation
includeHashLoc(FileID IncludedFile,
const SourceManager &SM);
115 const LangOptions &LangOpts,
128llvm::StringRef
toSourceCode(
const SourceManager &SM, SourceRange R);
145std::pair<llvm::StringRef, llvm::StringRef>
151 const tooling::Replacements &Repls);
154 const LangOptions &L);
167 FileManager &FileMgr);
178 llvm::StringRef Content,
183llvm::Expected<tooling::Replacements>
185 const format::FormatStyle &Style);
195 Edit(llvm::StringRef Code, tooling::Replacements Reps)
199 llvm::Expected<std::string>
apply()
const;
221 const format::FormatStyle &Style);
225 llvm::StringRef Content,
226 const LangOptions &LangOpts);
259 static std::optional<SpelledWord>
touching(SourceLocation SpelledLoc,
260 const syntax::TokenBuffer &TB,
261 const LangOptions &LangOpts);
266bool isKeyword(llvm::StringRef TokenName,
const LangOptions &LangOpts);
289 const LangOptions &LangOpts);
309 llvm::StringRef FullyQualifiedName,
310 const LangOptions &LangOpts);
328 std::optional<LangOptions> LangOpts = std::nullopt);
331bool isProtoFile(SourceLocation
Loc,
const SourceManager &SourceMgr);
336 return Name.size() >= 2 &&
Name[0] ==
'_' &&
337 (isUppercase(
Name[1]) ||
Name[1] ==
'_');
343 const SourceManager &SM);
llvm::SmallString< 256U > Name
const google::protobuf::Message & M
std::optional< FixItHint > FixIt
Values in a Context are indexed by typed keys.
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
bool isValidFileRange(const SourceManager &Mgr, SourceRange R)
Returns true iff all of the following conditions hold:
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
std::optional< SourceRange > toHalfOpenFileRange(const SourceManager &SM, const LangOptions &LangOpts, SourceRange R)
Turns a token range into a half-open range and checks its correctness.
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
SourceLocation includeHashLoc(FileID IncludedFile, const SourceManager &SM)
Returns the #include location through which IncludedFIle was loaded.
llvm::Error applyChange(std::string &Contents, const TextDocumentContentChangeEvent &Change)
Apply an incremental update to a text document.
TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, const LangOptions &L)
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
size_t lspLength(llvm::StringRef Code)
bool isReservedName(llvm::StringRef Name)
Returns true if Name is reserved, like _Foo or __Vector_base.
Key< OffsetEncoding > kCurrentOffsetEncoding
FileDigest digest(llvm::StringRef Content)
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
llvm::StringMap< Edit > FileEdits
A mapping from absolute file path (the one used for accessing the underlying VFS) to edits.
llvm::Error reformatEdit(Edit &E, const format::FormatStyle &Style)
Formats the edits and code around it according to Style.
void unionRanges(Range &A, Range B)
std::array< uint8_t, 8 > FileDigest
SourceLocation translatePreamblePatchLocation(SourceLocation Loc, const SourceManager &SM)
Translates locations inside preamble patch to their main-file equivalent using presumed locations.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
llvm::StringMap< unsigned > collectIdentifiers(llvm::StringRef Content, const format::FormatStyle &Style)
Collects identifiers with counts in the source code.
std::optional< DefinedMacro > locateMacroAt(const syntax::Token &SpelledTok, Preprocessor &PP)
Gets the macro referenced by SpelledTok.
std::optional< FileDigest > digestFile(const SourceManager &SM, FileID FID)
std::vector< std::string > visibleNamespaces(llvm::StringRef Code, const LangOptions &LangOpts)
Heuristically determine namespaces visible at a point, without parsing Code.
std::optional< std::string > getCanonicalPath(const FileEntryRef F, FileManager &FileMgr)
Get the canonical path of F.
EligibleRegion getEligiblePoints(llvm::StringRef Code, llvm::StringRef FullyQualifiedName, const LangOptions &LangOpts)
Returns most eligible region to insert a definition for FullyQualifiedName in the Code.
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R)
Returns the source code covered by the source range.
std::vector< Range > collectIdentifierRanges(llvm::StringRef Identifier, llvm::StringRef Content, const LangOptions &LangOpts)
Collects all ranges of the given identifier in the source code.
llvm::StringSet collectWords(llvm::StringRef Content)
Collects words from the source code.
llvm::Expected< SourceLocation > sourceLocationInMainFile(const SourceManager &SM, Position P)
Return the file location, corresponding to P.
bool isKeyword(llvm::StringRef NewName, const LangOptions &LangOpts)
Return true if the TokenName is in the list of reversed keywords of the language.
llvm::Expected< tooling::Replacements > cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, const format::FormatStyle &Style)
Cleanup and format the given replacements.
std::pair< size_t, size_t > offsetToClangLineColumn(llvm::StringRef Code, size_t Offset)
bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM)
Returns true if the token at Loc is spelled in the source code.
clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset)
Returns the range starting at offset and spanning the whole line.
bool isProtoFile(SourceLocation Loc, const SourceManager &SM)
Returns true if the given location is in a generated protobuf file.
bool isHeaderFile(llvm::StringRef FileName, std::optional< LangOptions > LangOpts)
Infers whether this is a header from the FileName and LangOpts (if presents).
std::vector< TextEdit > replacementsToEdits(llvm::StringRef Code, const tooling::Replacements &Repls)
TextEdit replacementToEdit(llvm::StringRef Code, const tooling::Replacement &R)
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS, bool FormatFile)
Choose the clang-format style we should apply to a certain file.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
SourceLocation NameLoc
Location of the identifier that names the macro.
A set of edits generated for a single file.
tooling::Replacements Replacements
Edit(llvm::StringRef Code, tooling::Replacements Reps)
std::vector< TextEdit > asTextEdits() const
Represents Replacements as TextEdits that are available for use in LSP.
llvm::Expected< std::string > apply() const
Returns the file contents after changes are applied.
bool canApplyTo(llvm::StringRef Code) const
Checks whether the Replacements are applicable to given Code.
Represents locations that can accept a definition.
std::vector< Position > EligiblePoints
Offsets into the code marking eligible points to insert a function definition.
std::string EnclosingNamespace
Namespace that owns all of the EligiblePoints, e.g.
static std::optional< SpelledWord > touching(SourceLocation SpelledLoc, const syntax::TokenBuffer &TB, const LangOptions &LangOpts)
const syntax::Token * ExpandedToken
const syntax::Token * PartOfSpelledToken
const syntax::Token * SpelledToken