[Lldb-commits] [lldb] r261345 - refactor/cleanup ClangExpressionParser::Parse

Ewan Crawford via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 19 09:55:11 PST 2016


Author: ewancrawford
Date: Fri Feb 19 11:55:10 2016
New Revision: 261345

URL: http://llvm.org/viewvc/llvm-project?rev=261345&view=rev
Log:
refactor/cleanup ClangExpressionParser::Parse

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 at codeplay.com>
Differential Revision: http://reviews.llvm.org/D17273

Modified:
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp?rev=261345&r1=261344&r2=261345&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp Fri Feb 19 11:55:10 2016
@@ -435,37 +435,34 @@ ClangExpressionParser::~ClangExpressionP
 }
 
 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())
@@ -473,9 +470,8 @@ ClangExpressionParser::Parse (Stream &st
                 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;
                 }
             }
@@ -485,7 +481,7 @@ ClangExpressionParser::Parse (Stream &st
     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());
@@ -510,34 +506,25 @@ ClangExpressionParser::Parse (Stream &st
 
     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 (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 (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 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 (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 (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)
     {




More information about the lldb-commits mailing list