[libclang] Don't pad the main buffer for the preamble.

Padding does not seem to be useful currently, and it leads to bogus location if an error
points to the end of the file.

rdar://15836513

llvm-svn: 203370
This commit is contained in:
Argyrios Kyrtzidis 2014-03-09 04:24:57 +00:00
parent 67101a52de
commit b255ee91a7
4 changed files with 32 additions and 44 deletions

View File

@ -262,10 +262,6 @@ private:
/// a line after skipping the preamble. /// a line after skipping the preamble.
bool PreambleEndsAtStartOfLine; bool PreambleEndsAtStartOfLine;
/// \brief The size of the source buffer that we've reserved for the main
/// file within the precompiled preamble.
unsigned PreambleReservedSize;
/// \brief Keeps track of the files that were used when computing the /// \brief Keeps track of the files that were used when computing the
/// preamble, with both their buffer size and their modification time. /// preamble, with both their buffer size and their modification time.
/// ///

View File

@ -1278,20 +1278,6 @@ ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
MaxLines)); MaxLines));
} }
static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
unsigned NewSize,
StringRef NewName) {
llvm::MemoryBuffer *Result
= llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
memcpy(const_cast<char*>(Result->getBufferStart()),
Old->getBufferStart(), Old->getBufferSize());
memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
' ', NewSize - Old->getBufferSize() - 1);
const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
return Result;
}
ASTUnit::PreambleFileHash ASTUnit::PreambleFileHash
ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) { ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) {
PreambleFileHash Result; PreambleFileHash Result;
@ -1427,7 +1413,6 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
// new main file. // new main file.
if (Preamble.size() == NewPreamble.second.first && if (Preamble.size() == NewPreamble.second.first &&
PreambleEndsAtStartOfLine == NewPreamble.second.second && PreambleEndsAtStartOfLine == NewPreamble.second.second &&
NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(), memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
NewPreamble.second.first) == 0) { NewPreamble.second.first) == 0) {
// The preamble has not changed. We may be able to re-use the precompiled // The preamble has not changed. We may be able to re-use the precompiled
@ -1500,11 +1485,8 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
PreambleInvocation->getDiagnosticOpts()); PreambleInvocation->getDiagnosticOpts());
getDiagnostics().setNumWarnings(NumWarningsInPreamble); getDiagnostics().setNumWarnings(NumWarningsInPreamble);
// Create a version of the main file buffer that is padded to return llvm::MemoryBuffer::getMemBufferCopy(
// buffer size we reserved when creating the preamble. NewPreamble.first->getBuffer(), FrontendOpts.Inputs[0].getFile());
return CreatePaddedMainFileBuffer(NewPreamble.first,
PreambleReservedSize,
FrontendOpts.Inputs[0].getFile());
} }
} }
@ -1545,16 +1527,6 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
SimpleTimer PreambleTimer(WantTiming); SimpleTimer PreambleTimer(WantTiming);
PreambleTimer.setOutput("Precompiling preamble"); PreambleTimer.setOutput("Precompiling preamble");
// Create a new buffer that stores the preamble. The buffer also contains
// extra space for the original contents of the file (which will be present
// when we actually parse the file) along with more room in case the file
// grows.
PreambleReservedSize = NewPreamble.first->getBufferSize();
if (PreambleReservedSize < 4096)
PreambleReservedSize = 8191;
else
PreambleReservedSize *= 2;
// Save the preamble text for later; we'll need to compare against it for // Save the preamble text for later; we'll need to compare against it for
// subsequent reparses. // subsequent reparses.
StringRef MainFilename = FrontendOpts.Inputs[0].getFile(); StringRef MainFilename = FrontendOpts.Inputs[0].getFile();
@ -1566,13 +1538,8 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
delete PreambleBuffer; delete PreambleBuffer;
PreambleBuffer PreambleBuffer
= llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, = llvm::MemoryBuffer::getMemBufferCopy(
FrontendOpts.Inputs[0].getFile()); NewPreamble.first->getBuffer().slice(0, Preamble.size()), MainFilename);
memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
NewPreamble.first->getBufferStart(), Preamble.size());
memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
' ', PreambleReservedSize - Preamble.size() - 1);
const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
// Remap the main source file to the preamble buffer. // Remap the main source file to the preamble buffer.
StringRef MainFilePath = FrontendOpts.Inputs[0].getFile(); StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
@ -1721,9 +1688,8 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
PreambleTopLevelHashValue = CurrentTopLevelHashValue; PreambleTopLevelHashValue = CurrentTopLevelHashValue;
} }
return CreatePaddedMainFileBuffer(NewPreamble.first, return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.first->getBuffer(),
PreambleReservedSize, MainFilename);
FrontendOpts.Inputs[0].getFile());
} }
void ASTUnit::RealizeTopLevelDeclsFromPreamble() { void ASTUnit::RealizeTopLevelDeclsFromPreamble() {

View File

@ -0,0 +1,12 @@
// RUN: mkdir -p %t
// RUN: touch %t/header.h
// RUN: env CINDEXTEST_EDITING=1 c-index-test -test-load-source-reparse 2 local %s -I %t 2> %t.err.txt > %t.out.txt
// RUN: cat %t.err.txt >> %t.out.txt
// RUN: FileCheck -input-file=%t.out.txt %s
// CHECK: preamble-reparse-warn-end-of-file.c:[[FnLine:.*]]:6: FunctionDecl=test:[[FnLine]]:6
// CHECK: preamble-reparse-warn-end-of-file.c:[[FnLine]]:14: error: expected '}'
// CHECK: preamble-reparse-warn-end-of-file.c:[[FnLine]]:14: error: expected '}'
#include "header.h"
void test() {

View File

@ -0,0 +1,14 @@
// RUN: mkdir -p %t
// RUN: touch %t/header.h
// RUN: env CINDEXTEST_EDITING=1 c-index-test -test-load-source-reparse 2 local %s -I %t 2> %t.err.txt > %t.out.txt
// RUN: cat %t.err.txt >> %t.out.txt
// RUN: FileCheck -input-file=%t.out.txt %s
// CHECK: preamble-reparse-warn-macro.c:14:9: warning: 'MAC' macro redefined
// CHECK-NEXT: Number FIX-ITs = 0
// CHECK-NEXT: preamble-reparse-warn-macro.c:10:9: note: previous definition is here
#define MAC 1
#include "header.h"
void test();
#define MAC 2