Go to the documentation of this file.
13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Format/Format.h"
24 #include "clang/Tooling/Core/Replacement.h"
25 #include "clang/Tooling/Syntax/Tokens.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSet.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/SHA1.h"
42 Optional<FileDigest>
digestFile(
const SourceManager &SM, FileID FID);
61 llvm::Expected<size_t>
63 bool AllowColumnsBeyondLineLength =
true);
89 SourceLocation
includeHashLoc(FileID IncludedFile,
const SourceManager &SM);
113 const LangOptions &LangOpts,
126 llvm::StringRef
toSourceCode(
const SourceManager &SM, SourceRange R);
140 std::pair<llvm::StringRef, llvm::StringRef>
146 const tooling::Replacements &Repls);
149 const LangOptions &L);
170 llvm::StringRef Content,
174 llvm::Expected<tooling::Replacements>
184 Edit(llvm::StringRef
Code, tooling::Replacements Reps)
188 llvm::Expected<std::string>
apply()
const;
210 llvm::StringRef Content,
211 const LangOptions &LangOpts);
218 llvm::StringSet<>
collectWords(llvm::StringRef Content);
244 static llvm::Optional<SpelledWord>
touching(SourceLocation SpelledLoc,
245 const syntax::TokenBuffer &TB,
246 const LangOptions &LangOpts);
270 const LangOptions &LangOpts);
290 llvm::StringRef FullyQualifiedName,
291 const LangOptions &LangOpts);
309 llvm::Optional<LangOptions> LangOpts =
llvm::None);
bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM)
Returns true if the token at Loc is spelled in the source code.
llvm::StringMap< unsigned > collectIdentifiers(llvm::StringRef Content, const format::FormatStyle &Style)
Collects identifiers with counts in the source code.
llvm::Optional< DefinedMacro > locateMacroAt(const syntax::Token &SpelledTok, Preprocessor &PP)
Gets the macro referenced by SpelledTok.
FileDigest digest(llvm::StringRef Content)
A set of edits generated for a single file.
bool canApplyTo(llvm::StringRef Code) const
Checks whether the Replacements are applicable to given Code.
llvm::Optional< SourceRange > toHalfOpenFileRange(const SourceManager &SM, const LangOptions &LangOpts, SourceRange R)
Turns a token range into a half-open range and checks its correctness.
llvm::Expected< std::string > apply() const
Returns the file contents after changes are applied.
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"(
Style for formatting code around applied fixes:
- 'none' (default) turns off formatting
- 'file' (literally 'file', not a placeholder)
uses .clang-format file in the closest parent
directory
- '{ <json> }' specifies options inline, e.g.
-format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
- 'llvm', 'google', 'webkit', 'mozilla'
See clang-format documentation for the up-to-date
information about formatting styles and options.
This option overrides the 'FormatStyle` option in
.clang-tidy file, if any.
)"), cl::init("none"), cl::cat(ClangTidyCategory))
std::vector< TextEdit > replacementsToEdits(llvm::StringRef Code, const tooling::Replacements &Repls)
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
std::vector< std::string > visibleNamespaces(llvm::StringRef Code, const LangOptions &LangOpts)
Heuristically determine namespaces visible at a point, without parsing Code.
size_t lspLength(llvm::StringRef Code)
llvm::StringSet collectWords(llvm::StringRef Content)
Collects words from the source code.
llvm::Error reformatEdit(Edit &E, const format::FormatStyle &Style)
Formats the edits and code around it according to Style.
TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, const LangOptions &L)
Documents should not be synced at all.
llvm::SourceMgr * SourceMgr
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
TextEdit replacementToEdit(llvm::StringRef Code, const tooling::Replacement &R)
std::string EnclosingNamespace
Namespace that owns all of the EligiblePoints, e.g.
llvm::Optional< FixItHint > FixIt
bool isValidFileRange(const SourceManager &Mgr, SourceRange R)
Returns true iff all of the following conditions hold:
llvm::Expected< tooling::Replacements > cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, const format::FormatStyle &Style)
Cleanup and format the given replacements.
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Represents locations that can accept a definition.
const syntax::Token * ExpandedToken
llvm::Optional< std::string > getCanonicalPath(const FileEntry *F, const SourceManager &SourceMgr)
Get the canonical path of F.
std::vector< Position > EligiblePoints
Offsets into the code marking eligible points to insert a function definition.
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
static llvm::Optional< SpelledWord > touching(SourceLocation SpelledLoc, const syntax::TokenBuffer &TB, const LangOptions &LangOpts)
std::vector< Range > collectIdentifierRanges(llvm::StringRef Identifier, llvm::StringRef Content, const LangOptions &LangOpts)
Collects all ranges of the given identifier in the source code.
tooling::Replacements Replacements
EligibleRegion getEligiblePoints(llvm::StringRef Code, llvm::StringRef FullyQualifiedName, const LangOptions &LangOpts)
Returns most eligible region to insert a definition for FullyQualifiedName in the Code.
Values in a Context are indexed by typed keys.
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS)
Choose the clang-format style we should apply to a certain file.
bool isProtoFile(SourceLocation Loc, const SourceManager &SM)
Returns true if the given location is in a generated protobuf file.
bool isHeaderFile(llvm::StringRef FileName, llvm::Optional< LangOptions > LangOpts)
Infers whether this is a header from the FileName and LangOpts (if presents).
const syntax::Token * SpelledToken
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Key< OffsetEncoding > kCurrentOffsetEncoding
llvm::Optional< FileDigest > digestFile(const SourceManager &SM, FileID FID)
std::array< uint8_t, 8 > FileDigest
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< TextEdit > asTextEdits() const
Represents Replacements as TextEdits that are available for use in LSP.
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.
SourceLocation NameLoc
Location of the identifier that names the macro.
SourceLocation Loc
'#' location in the include directive
SourceLocation includeHashLoc(FileID IncludedFile, const SourceManager &SM)
Returns the #include location through which IncludedFIle was loaded.
llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R)
Returns the source code covered by the source range.
llvm::Expected< SourceLocation > sourceLocationInMainFile(const SourceManager &SM, Position P)
Return the file location, corresponding to P.
Edit(llvm::StringRef Code, tooling::Replacements Reps)
const syntax::Token * PartOfSpelledToken
std::pair< size_t, size_t > offsetToClangLineColumn(llvm::StringRef Code, size_t Offset)