I am having trouble with clang code completion. My specific use case is generating intellisense suggestions in emacs from the clang completion results. But the trouble is with clang itself, specifically with header files.
This simple header demonstrates my problem:
~/Source/clang-test/test.h:
#include <string>
class Full_Name {
std::string m_firstName;
std::string m_lastName;
std::string m_fullName;
public:
Full_Name(std::string firstName, std::string lastName);
std::string to_string() const { return m_ }
};
Clang will generate completion suggestions if you tell it your cursor location. When the cursor is at Line 14, column 43 (right after “m_” in the to_string function, clang produces the following error:
Command:
clifford@Google-VM:~/Source$ clang -std=c++11 -fsyntax-only -x c++ -Xclang -code-completion-at=clang-test/test.h:14:43 clang-test/test.h
Output:
};
^
clang-test/test.h:3:17: note: to match this '{'
class Full_Name {
^
clang-test/test.h:14:42: error: use of undeclared identifier 'm'
std::string to_string() const { return m<U+0000>_ }
^
clang-test/test.h:14:43: error: expected '}'
std::string to_string() const { return m<U+0000>_ }
^
clang-test/test.h:14:33: note: to match this '{'
std::string to_string() const { return m<U+0000>_ }
^
clang-test/test.h:14:43: error: expected ';' after class
std::string to_string() const { return m<U+0000>_ }
^
;
4 errors generated.
However, changing the “m_” to “this->”, clang generates sensible results:
Command:
clifford@Google-VM:~/Source$ clang -std=c++11 -fsyntax-only -x c++ -Xclang -code-completion-at=clang-test/test.h:14:48 clang-test/test.h
Output:
COMPLETION: Full_Name : Full_Name::
COMPLETION: m_firstName : [#std::string#]m_firstName
COMPLETION: m_fullName : [#std::string#]m_fullName
COMPLETION: m_lastName : [#std::string#]m_lastName
COMPLETION: to_string : [#std::string#]to_string()[# const#]
Replacing the “m_” with “this->m_” produces the same error:
Command:
clifford@Google-VM:~/Source$ clang -std=c++11 -fsyntax-only -x c++ -Xclang -code-completion-at=clang-test/test.h:14:50 clang-test/test.h
Output:
clang-test/test.h:16:2: error: expected '}'
};
^
clang-test/test.h:3:17: note: to match this '{'
class Full_Name {
^
clang-test/test.h:14:48: error: no member named 'm_' in 'Full_Name'
std::string to_string() const { return this->m_<U+0000> }
~~~~ ^
clang-test/test.h:14:50: error: expected '}'
std::string to_string() const { return this->m_<U+0000> }
^
clang-test/test.h:14:33: note: to match this '{'
std::string to_string() const { return this->m_<U+0000> }
^
clang-test/test.h:14:50: error: expected ';' after class
std::string to_string() const { return this->m_<U+0000> }
^
;
4 errors generated.
The same error does not happen in a source file.
~/Source/clang-test/test.cpp:
#include <string>
#include <utility>
#include "test.h"
Full_Name::Full_Name(std::string firstName, std::string lastName) :
m_firstName(std::move(firstName)),
m_lastName(std::move(lastName)) {
m_fullName = m_firstName + " " + m_lastName;
}
std::string Full_Name::to_string() const {
return m_
}
Asking clang for completion suggestions right after “m_” (Line 15, column 12), produces sensible results:
Command:
clifford@Google-VM:~/Source$ clang -std=c++11 -fsyntax-only -x c++ -Xclang -code-completion-at=clang-test/test.cpp:15:12 clang-test/test.cpp
Output:
COMPLETION: m_firstName : [#std::string#]m_firstName
COMPLETION: m_fullName : [#std::string#]m_fullName
COMPLETION: m_lastName : [#std::string#]m_lastName
Based on the errors containing “<U+0000>” my guess is that clang is possibly confused about the header file encoding which is UTF-8; but if that is case, I don’t understand why the same error isn’t triggered in the source file or in the header file after “this->”.
I welcome any suggestions.