Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit d99ef53

Browse files
committed
Add a new libclang completion API to get brief documentation comment that is
attached to a declaration in the completion string. Since extracting comments isn't free, a new code completion option is introduced. A new code completion option that enables including brief comments into CodeCompletionString should be a, err, code completion option. But because ASTUnit caches global declarations during parsing before even completion consumer is created, the option is duplicated as a translation unit option (in both libclang and ASTUnit, like the option to cache code completion results). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159539 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f45f234 commit d99ef53

21 files changed

+322
-102
lines changed

include/clang-c/Index.h

+23-2
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,14 @@ enum CXTranslationUnit_Flags {
10581058
* This option can be used to search for declarations/definitions while
10591059
* ignoring the usages.
10601060
*/
1061-
CXTranslationUnit_SkipFunctionBodies = 0x40
1061+
CXTranslationUnit_SkipFunctionBodies = 0x40,
1062+
1063+
/**
1064+
* \brief Used to indicate that brief documentation comments should be
1065+
* included into the set of code completions returned from this translation
1066+
* unit.
1067+
*/
1068+
CXTranslationUnit_IncludeBriefCommentsInCodeCompletion = 0x80
10621069
};
10631070

10641071
/**
@@ -3786,6 +3793,14 @@ clang_getCompletionAnnotation(CXCompletionString completion_string,
37863793
CINDEX_LINKAGE CXString
37873794
clang_getCompletionParent(CXCompletionString completion_string,
37883795
enum CXCursorKind *kind);
3796+
3797+
/**
3798+
* \brief Retrieve the brief documentation comment attached to the declaration
3799+
* that corresponds to the given completion string.
3800+
*/
3801+
CINDEX_LINKAGE CXString
3802+
clang_getCompletionBriefComment(CXCompletionString completion_string);
3803+
37893804
/**
37903805
* \brief Retrieve a completion string for an arbitrary declaration or macro
37913806
* definition cursor.
@@ -3836,7 +3851,13 @@ enum CXCodeComplete_Flags {
38363851
* \brief Whether to include code patterns for language constructs
38373852
* within the set of code completions, e.g., for loops.
38383853
*/
3839-
CXCodeComplete_IncludeCodePatterns = 0x02
3854+
CXCodeComplete_IncludeCodePatterns = 0x02,
3855+
3856+
/**
3857+
* \brief Whether to include brief documentation within the set of code
3858+
* completions returned.
3859+
*/
3860+
CXCodeComplete_IncludeBriefComments = 0x04
38403861
};
38413862

38423863
/**

include/clang/AST/RawCommentList.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class RawComment {
9292
unsigned getBeginLine(const SourceManager &SM) const;
9393
unsigned getEndLine(const SourceManager &SM) const;
9494

95-
StringRef getBriefText(const ASTContext &Context) const {
95+
const char *getBriefText(const ASTContext &Context) const {
9696
if (BriefTextValid)
9797
return BriefText;
9898

@@ -103,7 +103,7 @@ class RawComment {
103103
SourceRange Range;
104104

105105
mutable StringRef RawText;
106-
mutable StringRef BriefText;
106+
mutable const char *BriefText;
107107

108108
mutable bool RawTextValid : 1; ///< True if RawText is valid
109109
mutable bool BriefTextValid : 1; ///< True if BriefText is valid
@@ -129,7 +129,7 @@ class RawComment {
129129

130130
StringRef getRawTextSlow(const SourceManager &SourceMgr) const;
131131

132-
StringRef extractBriefText(const ASTContext &Context) const;
132+
const char *extractBriefText(const ASTContext &Context) const;
133133

134134
friend class ASTReader;
135135
};

include/clang/Driver/CC1Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ def code_completion_patterns : Flag<"-code-completion-patterns">,
269269
HelpText<"Include code patterns in code-completion results">;
270270
def no_code_completion_globals : Flag<"-no-code-completion-globals">,
271271
HelpText<"Do not include global declarations in code-completion results.">;
272+
def code_completion_brief_comments : Flag<"-code-completion-brief-comments">,
273+
HelpText<"Include brief documentation comments in code-completion results.">;
272274
def disable_free : Flag<"-disable-free">,
273275
HelpText<"Disable freeing of memory on exit">;
274276
def load : Separate<"-load">, MetaVarName<"<dsopath>">,

include/clang/Frontend/ASTUnit.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ class ASTUnit : public ModuleLoader {
248248
std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
249249

250250
/// \brief Whether we should be caching code-completion results.
251-
bool ShouldCacheCodeCompletionResults;
251+
bool ShouldCacheCodeCompletionResults : 1;
252+
253+
/// \brief Whether to include brief documentation within the set of code
254+
/// completions cached.
255+
bool IncludeBriefCommentsInCodeCompletion : 1;
252256

253257
/// \brief The language options used when we load an AST file.
254258
LangOptions ASTFileLangOpts;
@@ -681,6 +685,7 @@ class ASTUnit : public ModuleLoader {
681685
bool CaptureDiagnostics = false,
682686
bool PrecompilePreamble = false,
683687
bool CacheCodeCompletionResults = false,
688+
bool IncludeBriefCommentsInCodeCompletion = false,
684689
OwningPtr<ASTUnit> *ErrAST = 0);
685690

686691
/// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
@@ -700,7 +705,8 @@ class ASTUnit : public ModuleLoader {
700705
bool CaptureDiagnostics = false,
701706
bool PrecompilePreamble = false,
702707
TranslationUnitKind TUKind = TU_Complete,
703-
bool CacheCodeCompletionResults = false);
708+
bool CacheCodeCompletionResults = false,
709+
bool IncludeBriefCommentsInCodeCompletion = false);
704710

705711
/// LoadFromCommandLine - Create an ASTUnit from a vector of command line
706712
/// arguments, which must specify exactly one source file.
@@ -732,6 +738,7 @@ class ASTUnit : public ModuleLoader {
732738
bool PrecompilePreamble = false,
733739
TranslationUnitKind TUKind = TU_Complete,
734740
bool CacheCodeCompletionResults = false,
741+
bool IncludeBriefCommentsInCodeCompletion = false,
735742
bool AllowPCHWithCompilerErrors = false,
736743
bool SkipFunctionBodies = false,
737744
OwningPtr<ASTUnit> *ErrAST = 0);
@@ -759,11 +766,15 @@ class ASTUnit : public ModuleLoader {
759766
/// \param IncludeCodePatterns Whether to include code patterns (such as a
760767
/// for loop) in the code-completion results.
761768
///
769+
/// \param IncludeBriefComments Whether to include brief documentation within
770+
/// the set of code completions returned.
771+
///
762772
/// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
763773
/// OwnedBuffers parameters are all disgusting hacks. They will go away.
764774
void CodeComplete(StringRef File, unsigned Line, unsigned Column,
765775
RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
766776
bool IncludeMacros, bool IncludeCodePatterns,
777+
bool IncludeBriefComments,
767778
CodeCompleteConsumer &Consumer,
768779
DiagnosticsEngine &Diag, LangOptions &LangOpts,
769780
SourceManager &SourceMgr, FileManager &FileMgr,

include/clang/Frontend/CompilerInstance.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ class CompilerInstance : public ModuleLoader {
560560
static CodeCompleteConsumer *
561561
createCodeCompletionConsumer(Preprocessor &PP, const std::string &Filename,
562562
unsigned Line, unsigned Column,
563-
bool ShowMacros,
564-
bool ShowCodePatterns, bool ShowGlobals,
563+
const CodeCompleteOptions &Opts,
565564
raw_ostream &OS);
566565

567566
/// \brief Create the Sema object to be used for parsing.

include/clang/Frontend/FrontendOptions.h

+4-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define LLVM_CLANG_FRONTEND_FRONTENDOPTIONS_H
1212

1313
#include "clang/Frontend/CommandLineSourceLoc.h"
14+
#include "clang/Sema/CodeCompleteOptions.h"
1415
#include "llvm/ADT/StringRef.h"
1516
#include <string>
1617
#include <vector>
@@ -84,7 +85,7 @@ struct FrontendInputFile {
8485
FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false)
8586
: File(File.str()), Kind(Kind), IsSystem(IsSystem) { }
8687
};
87-
88+
8889
/// FrontendOptions - Options for controlling the behavior of the frontend.
8990
class FrontendOptions {
9091
public:
@@ -93,12 +94,6 @@ class FrontendOptions {
9394
/// instruct the AST writer to create
9495
/// relocatable PCH files.
9596
unsigned ShowHelp : 1; ///< Show the -help text.
96-
unsigned ShowMacrosInCodeCompletion : 1; ///< Show macros in code completion
97-
/// results.
98-
unsigned ShowCodePatternsInCodeCompletion : 1; ///< Show code patterns in code
99-
/// completion results.
100-
unsigned ShowGlobalSymbolsInCodeCompletion : 1; ///< Show top-level decls in
101-
/// code completion results.
10297
unsigned ShowStats : 1; ///< Show frontend performance
10398
/// metrics and statistics.
10499
unsigned ShowTimers : 1; ///< Show timers for individual
@@ -116,6 +111,8 @@ class FrontendOptions {
116111
/// not need them (e.g. with code
117112
/// completion).
118113

114+
CodeCompleteOptions CodeCompleteOpts;
115+
119116
enum {
120117
ARCMT_None,
121118
ARCMT_Check,
@@ -183,9 +180,6 @@ class FrontendOptions {
183180
ActionName = "";
184181
RelocatablePCH = 0;
185182
ShowHelp = 0;
186-
ShowMacrosInCodeCompletion = 0;
187-
ShowCodePatternsInCodeCompletion = 0;
188-
ShowGlobalSymbolsInCodeCompletion = 1;
189183
ShowStats = 0;
190184
ShowTimers = 0;
191185
ShowVersion = 0;

include/clang/Sema/CodeCompleteConsumer.h

+42-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "clang/AST/Type.h"
1717
#include "clang/AST/CanonicalType.h"
18+
#include "clang/Sema/CodeCompleteOptions.h"
1819
#include "llvm/ADT/SmallVector.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/Support/Allocator.h"
@@ -444,14 +445,19 @@ class CodeCompletionString {
444445

445446
/// \brief The name of the parent context.
446447
StringRef ParentName;
448+
449+
/// \brief A brief documentation comment attached to the declaration of
450+
/// entity being completed by this result.
451+
const char *BriefComment;
447452

448453
CodeCompletionString(const CodeCompletionString &); // DO NOT IMPLEMENT
449454
CodeCompletionString &operator=(const CodeCompletionString &); // DITTO
450455

451456
CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
452457
unsigned Priority, CXAvailabilityKind Availability,
453458
const char **Annotations, unsigned NumAnnotations,
454-
CXCursorKind ParentKind, StringRef ParentName);
459+
CXCursorKind ParentKind, StringRef ParentName,
460+
const char *BriefComment);
455461
~CodeCompletionString() { }
456462

457463
friend class CodeCompletionBuilder;
@@ -493,6 +499,10 @@ class CodeCompletionString {
493499
StringRef getParentContextName() const {
494500
return ParentName;
495501
}
502+
503+
const char *getBriefComment() const {
504+
return BriefComment;
505+
}
496506

497507
/// \brief Retrieve a string representation of the code completion string,
498508
/// which is mainly useful for debugging.
@@ -569,6 +579,7 @@ class CodeCompletionBuilder {
569579
CXAvailabilityKind Availability;
570580
CXCursorKind ParentKind;
571581
StringRef ParentName;
582+
const char *BriefComment;
572583

573584
/// \brief The chunks stored in this string.
574585
SmallVector<Chunk, 4> Chunks;
@@ -580,14 +591,14 @@ class CodeCompletionBuilder {
580591
CodeCompletionTUInfo &CCTUInfo)
581592
: Allocator(Allocator), CCTUInfo(CCTUInfo),
582593
Priority(0), Availability(CXAvailability_Available),
583-
ParentKind(CXCursor_NotImplemented) { }
594+
ParentKind(CXCursor_NotImplemented), BriefComment(NULL) { }
584595

585596
CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
586597
CodeCompletionTUInfo &CCTUInfo,
587598
unsigned Priority, CXAvailabilityKind Availability)
588599
: Allocator(Allocator), CCTUInfo(CCTUInfo),
589600
Priority(Priority), Availability(Availability),
590-
ParentKind(CXCursor_NotImplemented) { }
601+
ParentKind(CXCursor_NotImplemented), BriefComment(NULL) { }
591602

592603
/// \brief Retrieve the allocator into which the code completion
593604
/// strings should be allocated.
@@ -628,6 +639,8 @@ class CodeCompletionBuilder {
628639

629640
/// \brief Add the parent context information to this code completion.
630641
void addParentContext(DeclContext *DC);
642+
643+
void addBriefComment(StringRef Comment);
631644

632645
CXCursorKind getParentKind() const { return ParentKind; }
633646
StringRef getParentName() const { return ParentName; }
@@ -780,11 +793,13 @@ class CodeCompletionResult {
780793
/// string itself.
781794
CodeCompletionString *CreateCodeCompletionString(Sema &S,
782795
CodeCompletionAllocator &Allocator,
783-
CodeCompletionTUInfo &CCTUInfo);
796+
CodeCompletionTUInfo &CCTUInfo,
797+
bool IncludeBriefComments);
784798
CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
785799
Preprocessor &PP,
786800
CodeCompletionAllocator &Allocator,
787-
CodeCompletionTUInfo &CCTUInfo);
801+
CodeCompletionTUInfo &CCTUInfo,
802+
bool IncludeBriefComments);
788803

789804
/// \brief Determine a base priority for the given declaration.
790805
static unsigned getPriorityFromDecl(NamedDecl *ND);
@@ -818,16 +833,7 @@ raw_ostream &operator<<(raw_ostream &OS,
818833
/// information.
819834
class CodeCompleteConsumer {
820835
protected:
821-
/// \brief Whether to include macros in the code-completion results.
822-
bool IncludeMacros;
823-
824-
/// \brief Whether to include code patterns (such as for loops) within
825-
/// the completion results.
826-
bool IncludeCodePatterns;
827-
828-
/// \brief Whether to include global (top-level) declarations and names in
829-
/// the completion results.
830-
bool IncludeGlobals;
836+
const CodeCompleteOptions CodeCompleteOpts;
831837

832838
/// \brief Whether the output format for the code-completion consumer is
833839
/// binary.
@@ -900,22 +906,31 @@ class CodeCompleteConsumer {
900906
CodeCompletionTUInfo &CCTUInfo) const;
901907
};
902908

903-
CodeCompleteConsumer() : IncludeMacros(false), IncludeCodePatterns(false),
904-
IncludeGlobals(true), OutputIsBinary(false) { }
905-
906-
CodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
907-
bool IncludeGlobals, bool OutputIsBinary)
908-
: IncludeMacros(IncludeMacros), IncludeCodePatterns(IncludeCodePatterns),
909-
IncludeGlobals(IncludeGlobals), OutputIsBinary(OutputIsBinary) { }
909+
CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
910+
bool OutputIsBinary)
911+
: CodeCompleteOpts(CodeCompleteOpts), OutputIsBinary(OutputIsBinary)
912+
{ }
910913

911914
/// \brief Whether the code-completion consumer wants to see macros.
912-
bool includeMacros() const { return IncludeMacros; }
915+
bool includeMacros() const {
916+
return CodeCompleteOpts.IncludeMacros;
917+
}
913918

914919
/// \brief Whether the code-completion consumer wants to see code patterns.
915-
bool includeCodePatterns() const { return IncludeCodePatterns; }
920+
bool includeCodePatterns() const {
921+
return CodeCompleteOpts.IncludeCodePatterns;
922+
}
916923

917924
/// \brief Whether to include global (top-level) declaration results.
918-
bool includeGlobals() const { return IncludeGlobals; }
925+
bool includeGlobals() const {
926+
return CodeCompleteOpts.IncludeGlobals;
927+
}
928+
929+
/// \brief Whether to include brief documentation comments within the set of
930+
/// code completions returned.
931+
bool includeBriefComments() const {
932+
return CodeCompleteOpts.IncludeBriefComments;
933+
}
919934

920935
/// \brief Determine whether the output of this consumer is binary.
921936
bool isOutputBinary() const { return OutputIsBinary; }
@@ -962,11 +977,9 @@ class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
962977
public:
963978
/// \brief Create a new printing code-completion consumer that prints its
964979
/// results to the given raw output stream.
965-
PrintingCodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
966-
bool IncludeGlobals,
980+
PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
967981
raw_ostream &OS)
968-
: CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
969-
false), OS(OS),
982+
: CodeCompleteConsumer(CodeCompleteOpts, false), OS(OS),
970983
CCTUInfo(new GlobalCodeCompletionAllocator) {}
971984

972985
/// \brief Prints the finalized code-completion results.
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===---- CodeCompleteOptions.h - Code Completion Options -------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_SEMA_CODECOMPLETEOPTIONS_H
11+
#define LLVM_CLANG_SEMA_CODECOMPLETEOPTIONS_H
12+
13+
/// Options controlling the behavior of code completion.
14+
class CodeCompleteOptions {
15+
public:
16+
///< Show macros in code completion results.
17+
unsigned IncludeMacros : 1;
18+
19+
///< Show code patterns in code completion results.
20+
unsigned IncludeCodePatterns : 1;
21+
22+
///< Show top-level decls in code completion results.
23+
unsigned IncludeGlobals : 1;
24+
25+
///< Show brief documentation comments in code completion results.
26+
unsigned IncludeBriefComments : 1;
27+
28+
CodeCompleteOptions() :
29+
IncludeMacros(0),
30+
IncludeCodePatterns(0),
31+
IncludeGlobals(1),
32+
IncludeBriefComments(0)
33+
{ }
34+
};
35+
36+
#endif
37+

0 commit comments

Comments
 (0)