Once I use an IDE to compile my C++ code, this IDE seems know everything of my code:
I could find the most original definition of any element in my code, no matter how deep it is from usage to definition.

the IDE seems know:
1.my code logic
2.my code structure

How IDE could do this?

Recommended Answers

All 4 Replies

By parsing the code, and making a lookup table of the symbols. The most trivial way to implment this is with regex's (which would probably only take a few LOC). A more powerfull way is to lexically analyse the code into tokens.

ctags is a project which can tag 41 languages (and you can add custom languages with regex's).

Ain't software great? You can use older versions of a compiler to write/build a new version of the same. You can use software that analyzes other software to analyze itself. Look up Ouroboros! :-) https://en.wikipedia.org/wiki/Ouroboros

you can use turboo

These code-completion or introspection tools that most IDEs have are essentially implementing the first half of a complete compiler (which we would call the "front-end" of the compiler). Think about it, a compiler needs to look at all your code and understand every piece of it, i.e., what every identifier means, what every function call refers to, what every class or type is and where it's defined, etc... And once it did that, it can (1) verify that you obey all the rules of the language, (2) optimize things down (i.e., cut down on the extra fat), and (3) generate the machine code for the final program. Well, typically, the IDEs will have a kind of reduced compiler that stops just before doing those three things, and therefore, leaving the IDE with a kind of complete in-memory representation of your code, i.e., a complete "understanding" of your code. This is typically in the form of an AST (Abstract Syntax Tree).

Traditionally, IDEs have developed their code introspection tools on their own, often starting with a simply parser that can highlight the syntax elements (keywords, types, variables, literal values, etc.), and then building it up to a full semantic analyser (that "understands" the code). However, recently, with the Clang project, people have, for once, built a compiler that is modular enough to allow it to be used only in parts, and some IDEs are using that instead (like XCode, and soon KDevelop too). So, there's your answer, IDEs use either a compiler that's stripped down or an almost-compiler piece of software, that only does the first part of the job, i.e., "understanding" the code, but not the second part (generating the program / machine code).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.