Developer forums (C::B DEVELOPMENT STRICTLY!) > CodeCompletion redesign
Clang command line support for codecompletion
ollydbg:
Here is my detailed test for codecompletion in clang (Windows)
prerequisite:
You need to download the package supplied by jens: clang_win32
Then add the path of clang.exe in your PATH.
test
create a file like:
E:\clang_win32\main.cpp
the file contains some code snippet like:
--- Code: ---
class AAA
{
public:
int aaa;
int bbb;
};
int myfunction()
{
AAA a;
a.
return 0;
}
--- End code ---
Now, you can see, the codecompletion list should generated after "a." .
You just run the command: pay attention to the :13:5, this this the line and column number of the caret after "a."
--- Code: ---clang -w -fsyntax-only -Xclang -code-completion-at=E:\clang_win32\main.cpp:13:5 E:\clang_win32\main.cpp
--- End code ---
Then here is the wonder result:
--- Code: ---E:\clang_win32>clang -w -fsyntax-only -Xclang -code-completion-at=E:\clang_win32
\main.cpp:13:5 E:\clang_win32\main.cpp
COMPLETION: AAA : AAA::
COMPLETION: aaa : [#int#]aaa
COMPLETION: bbb : [#int#]bbb
COMPLETION: operator= : [#AAA &#]operator=(<#const AAA &#>)
COMPLETION: ~AAA : [#void#]~AAA()
--- End code ---
:D
then main reference is eranif's codelite source (thanks eranif):
http://codelite.svn.sourceforge.net/viewvc/codelite/trunk/LiteEditor/clang_code_completion.cpp?revision=4207&view=markup
ollydbg:
about the pch. suppose I have two files: testcpp.h and main.cpp
testcpp.h
--- Code: ---
class BBB
{
public:
int f1();
int f2();
int f3();
};
--- End code ---
main.cpp
--- Code: ---
#include "testcpp.h"
int myfunction()
{
BBB a;
a.
return 0;
}
--- End code ---
Now,
first I need to generate the pch file using command:
--- Code: ---E:\clang_win32>clang++ -cc1 -x c++-header testcpp.h -emit-pch -o testcpp.h.pch
--- End code ---
Then, do the code completion command.
--- Code: ---E:\clang_win32>clang++ -cc1 -w -fsyntax-only -include-pch testcpp.h.pch -code-co
mpletion-at=E:\clang_win32\main.cpp:8:5 E:\clang_win32\main.cpp
In file included from E:\clang_win32\main.cpp:3:
E:\clang_win32\testcpp.h:3:7: error: redefinition of 'BBB'
class BBB
^
E:\clang_win32\testcpp.h:3:7: note: previous definition is here
class BBB
^
1 error generated.
COMPLETION: BBB : BBB::
COMPLETION: f1 : [#int#]f1()
COMPLETION: f2 : [#int#]f2()
COMPLETION: f3 : [#int#]f3()
--- End code ---
Strange that there is an error about "redefinition".
So, I just comment the #include line like, so the modified main.cpp
--- Code: ---
//#include "testcpp.h"
int myfunction()
{
BBB a;
a.
return 0;
}
--- End code ---
Now, everything works fine.
--- Code: ---E:\clang_win32>clang++ -cc1 -w -fsyntax-only -include-pch testcpp.h.pch -code-co
mpletion-at=E:\clang_win32\main.cpp:8:5 E:\clang_win32\main.cpp
COMPLETION: BBB : BBB::
COMPLETION: f1 : [#int#]f1()
COMPLETION: f2 : [#int#]f2()
COMPLETION: f3 : [#int#]f3()
--- End code ---
Any comments about how to solve the redefinition issue???
thanks.
ollydbg:
Sorry guys, I have made a mistake in my previous post, now add the include guard solved all the problem of redefinition.
modified testcpp.h
--- Code: ---
#ifndef TESTCPP_H
#define TESTCPP_H
class BBB
{
public:
int f1();
int f2();
int f3();
};
#endif
--- End code ---
@eranif
it seems the clang++ and PCH works fine. You can try it to see if it works much faster.
Jenna:
--- Quote from: ollydbg on October 27, 2010, 02:41:21 am ---@jens:
thanks for your time and effort to build the win32 version of clang for me, it works quite well.
--- End quote ---
Nice to hear, for me it's much easier (the most time) to do a cross-build for windows in my linux, than do the build on windows itself.
ollydbg:
I have just briefly read the Clang's source code about
lexer
parser
And I found that it is definitely all hand-written code (Note our CC's parser is also hand-written), but it is quite modular.
It's lexer's source code was located in
--- Code: ---llvm\tools\clang\include\clang\Lex
llvm\tools\clang\lib\Lex
--- End code ---
parser's source is located in
--- Code: ---llvm\tools\clang\include\clang\Parse
llvm\tools\clang\lib\Parse
--- End code ---
for example:
In the parser.cpp
--- Code: ---/// \brief Determine whether the current token, if it occurs after a
/// declarator, continues a declaration or declaration list.
bool Parser::isDeclarationAfterDeclarator() const {
return Tok.is(tok::equal) || // int X()= -> not a function def
Tok.is(tok::comma) || // int X(), -> not a function def
Tok.is(tok::semi) || // int X(); -> not a function def
Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
(getLang().CPlusPlus &&
Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
}
--- End code ---
there are even skip functions in lexer ( we have skip XXX functions in CC either) :D
--- Code: ---/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
/// them), skip over them and return the first non-escaped-newline found,
/// otherwise return P.
const char *Lexer::SkipEscapedNewLines(const char *P) {
while (1) {
const char *AfterEscape;
if (*P == '\\') {
AfterEscape = P+1;
} else if (*P == '?') {
// If not a trigraph for escape, bail out.
if (P[1] != '?' || P[2] != '/')
return P;
AfterEscape = P+3;
} else {
return P;
}
unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
if (NewLineSize == 0) return P;
P = AfterEscape+NewLineSize;
}
}
--- End code ---
BTW: seems all the tokens were handled as char type.(one byte). Hope our CC can use char type instead. because wxChar seems waste a lot of space. (Unicode wxChar seems equal two bytes, not sure how long in X86-64 system).
Another similaity is that Clang do the pre-processors and lexer in the same stage, this is the conceptional same as in CC's Tokenizer.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version