From 767b614fe357c8a26b38330cce0754c5427ba2f0 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sat, 18 Oct 2014 06:19:36 +0000 Subject: [PATCH] [libclang] Allow code-completion when pointing at the end-of-file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220135 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Lex/Preprocessor.cpp | 29 ++++++++++++++--------------- test/Index/Inputs/complete-at-EOF.c | 3 +++ test/Index/complete-at-EOF.c | 5 +++++ 3 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 test/Index/Inputs/complete-at-EOF.c create mode 100644 test/Index/complete-at-EOF.c diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 400cdb469d..c15a6ab956 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -384,21 +384,20 @@ bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, } Position += CompleteColumn - 1; - - // Insert '\0' at the code-completion point. - if (Position < Buffer->getBufferEnd()) { - CodeCompletionFile = File; - CodeCompletionOffset = Position - Buffer->getBufferStart(); - - std::unique_ptr NewBuffer = - MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1, - Buffer->getBufferIdentifier()); - char *NewBuf = const_cast(NewBuffer->getBufferStart()); - char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf); - *NewPos = '\0'; - std::copy(Position, Buffer->getBufferEnd(), NewPos+1); - SourceMgr.overrideFileContents(File, std::move(NewBuffer)); - } + if (Position > Buffer->getBufferEnd()) + Position = Buffer->getBufferEnd(); + + CodeCompletionFile = File; + CodeCompletionOffset = Position - Buffer->getBufferStart(); + + std::unique_ptr NewBuffer = + MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1, + Buffer->getBufferIdentifier()); + char *NewBuf = const_cast(NewBuffer->getBufferStart()); + char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf); + *NewPos = '\0'; + std::copy(Position, Buffer->getBufferEnd(), NewPos+1); + SourceMgr.overrideFileContents(File, std::move(NewBuffer)); return false; } diff --git a/test/Index/Inputs/complete-at-EOF.c b/test/Index/Inputs/complete-at-EOF.c new file mode 100644 index 0000000000..8207f72055 --- /dev/null +++ b/test/Index/Inputs/complete-at-EOF.c @@ -0,0 +1,3 @@ +#define CAKE 1 + +typedef int foo; diff --git a/test/Index/complete-at-EOF.c b/test/Index/complete-at-EOF.c new file mode 100644 index 0000000000..ff39233f0a --- /dev/null +++ b/test/Index/complete-at-EOF.c @@ -0,0 +1,5 @@ + +// RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test \ +// RUN: -code-completion-at=%S/Inputs/complete-at-EOF.c:4:1 %S/Inputs/complete-at-EOF.c | FileCheck -check-prefix=CHECK-EOF %s +// CHECK-EOF: macro definition:{TypedText CAKE} +// CHECK-EOF: TypedefDecl:{TypedText foo} -- 2.40.0