[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

@ -261,10 +261,6 @@ private:
/// Used to inform the lexer as to whether it's starting at the beginning of
/// a line after skipping the preamble.
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
/// preamble, with both their buffer size and their modification time.

View File

@ -1278,20 +1278,6 @@ ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
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::createForFile(off_t Size, time_t ModTime) {
PreambleFileHash Result;
@ -1427,7 +1413,6 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
// new main file.
if (Preamble.size() == NewPreamble.second.first &&
PreambleEndsAtStartOfLine == NewPreamble.second.second &&
NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
NewPreamble.second.first) == 0) {
// The preamble has not changed. We may be able to re-use the precompiled
@ -1500,11 +1485,8 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
PreambleInvocation->getDiagnosticOpts());
getDiagnostics().setNumWarnings(NumWarningsInPreamble);
// Create a version of the main file buffer that is padded to
// buffer size we reserved when creating the preamble.
return CreatePaddedMainFileBuffer(NewPreamble.first,
PreambleReservedSize,
FrontendOpts.Inputs[0].getFile());
return llvm::MemoryBuffer::getMemBufferCopy(
NewPreamble.first->getBuffer(), FrontendOpts.Inputs[0].getFile());
}
}
@ -1544,16 +1526,6 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
// We did not previously compute a preamble, or it can't be reused anyway.
SimpleTimer PreambleTimer(WantTiming);
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
// subsequent reparses.
@ -1566,13 +1538,8 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
delete PreambleBuffer;
PreambleBuffer
= llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
FrontendOpts.Inputs[0].getFile());
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';
= llvm::MemoryBuffer::getMemBufferCopy(
NewPreamble.first->getBuffer().slice(0, Preamble.size()), MainFilename);
// Remap the main source file to the preamble buffer.
StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
@ -1721,9 +1688,8 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
PreambleTopLevelHashValue = CurrentTopLevelHashValue;
}
return CreatePaddedMainFileBuffer(NewPreamble.first,
PreambleReservedSize,
FrontendOpts.Inputs[0].getFile());
return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.first->getBuffer(),
MainFilename);
}
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