Skip to content

Commit

Permalink
refactor/cleanup ClangExpressionParser::Parse
Browse files Browse the repository at this point in the history
This patches does the following:

+ fix return type: ClangExpressionParser::Parse returns unsigned, but was actually returning a signed value, num_errors.
+ use helper clang::TextDiagnosticBuffer::getNumErrors() instead of counting the errors ourself.
+ limit scoping of block-level automatic variables as much as practical.
+ remove reused multipurpose TextDiagnosticBuffer::const_iterator in favour of loop-scoped err, warn, and note variables in the diagnostic printing code.
+ refactor diagnostic printing loops to use a proper loop invariant.

Author: Luke Drummond <luke.drummond@codeplay.com>
Differential Revision: http://reviews.llvm.org/D17273

llvm-svn: 261345
  • Loading branch information
EwanC committed Feb 19, 2016
1 parent f56aeef commit 615a807
Showing 1 changed file with 20 additions and 33 deletions.
Expand Up @@ -435,47 +435,43 @@ ClangExpressionParser::~ClangExpressionParser()
}

unsigned
ClangExpressionParser::Parse (Stream &stream)
ClangExpressionParser::Parse(Stream &stream)
{
TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());

diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer *>(m_compiler->getDiagnostics().getClient());
diag_buf->FlushDiagnostics(m_compiler->getDiagnostics());

const char *expr_text = m_expr.Text();

clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
clang::SourceManager &source_mgr = m_compiler->getSourceManager();
bool created_main_file = false;
if (m_compiler->getCodeGenOpts().getDebugInfo() == codegenoptions::FullDebugInfo)
{
std::string temp_source_path;

int temp_fd = -1;
llvm::SmallString<PATH_MAX> result_path;
FileSpec tmpdir_file_spec;
if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
{
tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
temp_source_path = tmpdir_file_spec.GetPath();
std::string temp_source_path = tmpdir_file_spec.GetPath();
llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
}
else
{
llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
}

if (temp_fd != -1)
{
lldb_private::File file (temp_fd, true);
lldb_private::File file(temp_fd, true);
const size_t expr_text_len = strlen(expr_text);
size_t bytes_written = expr_text_len;
if (file.Write(expr_text, bytes_written).Success())
{
if (bytes_written == expr_text_len)
{
file.Close();
SourceMgr.setMainFileID(SourceMgr.createFileID(
m_file_manager->getFile(result_path),
SourceLocation(), SrcMgr::C_User));
source_mgr.setMainFileID(source_mgr.createFileID(m_file_manager->getFile(result_path),
SourceLocation(), SrcMgr::C_User));
created_main_file = true;
}
}
Expand All @@ -485,7 +481,7 @@ ClangExpressionParser::Parse (Stream &stream)
if (!created_main_file)
{
std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(memory_buffer)));
source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
}

diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Expand All @@ -510,34 +506,25 @@ ClangExpressionParser::Parse (Stream &stream)

diag_buf->EndSourceFile();

TextDiagnosticBuffer::const_iterator diag_iterator;
unsigned num_errors = diag_buf->getNumErrors();

int num_errors = 0;

if (m_pp_callbacks && m_pp_callbacks->hasErrors())
{
num_errors++;

stream.PutCString(m_pp_callbacks->getErrorString().c_str());
}

for (diag_iterator = diag_buf->warn_begin();
diag_iterator != diag_buf->warn_end();
++diag_iterator)
stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
for (TextDiagnosticBuffer::const_iterator warn = diag_buf->warn_begin(), warn_end = diag_buf->warn_end();
warn != warn_end; ++warn)
stream.Printf("warning: %s\n", warn->second.c_str());

for (diag_iterator = diag_buf->err_begin();
diag_iterator != diag_buf->err_end();
++diag_iterator)
{
num_errors++;
stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
}
for (TextDiagnosticBuffer::const_iterator err = diag_buf->err_begin(), err_end = diag_buf->err_end();
err != err_end; ++err)
stream.Printf("error: %s\n", err->second.c_str());

for (diag_iterator = diag_buf->note_begin();
diag_iterator != diag_buf->note_end();
++diag_iterator)
stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
for (TextDiagnosticBuffer::const_iterator note = diag_buf->note_begin(), note_end = diag_buf->note_end();
note != note_end; ++note)
stream.Printf("note: %s\n", note->second.c_str());

if (!num_errors)
{
Expand Down

0 comments on commit 615a807

Please sign in to comment.