Page 1 of 2

Is there a tool to ask the computer how it can understand code?

Posted: Thu Nov 08, 2018 5:25 am
by Uri Blass
I would like simply to be able to find easily what the computer needs to understand a code without many hours and today I need some hours even to understand a simple function like engine_info() in stockfish.

I take the following line in stockfish's code that is the first line in main.cpp line 37

Code: Select all

std::cout << engine_info() << std::endl;
I would like every line to have fathers that if the computer understand them it has no problem to understand the line and I want the computer to show me the fathers of every line if I ask it(I think that it should be something that can be automatically done by a compiler and I do not ask for explanation in human language)

I want information like the following about stockfish and the question is if there is a tool to give the information without searching for a long time in the code.

fathers of line 37 in main.cpp are
1)line 21 of main.cpp

Code: Select all

#include <iostream>
2)line 27 of main.cpp

Code: Select all

#include "tt.h"
3)line 24 of tt.h

Code: Select all

#include "misc.h"
4)line 32 of misc.h

Code: Select all

const std::string engine_info(bool to_uci = false);
5)lines 124-144 of misc.cpp

Code: Select all

const string engine_info(bool to_uci) {

  const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
  string month, day, year;
  stringstream ss, date(__DATE__); // From compiler, format is "Sep 21 2008"

  ss << "Stockfish " << Version << setfill('0');

  if (Version.empty())
  {
      date >> month >> day >> year;
      ss << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
  }

  ss << (Is64Bit ? " 64" : "")
     << (HasPext ? " BMI2" : (HasPopCnt ? " POPCNT" : ""))
     << (to_uci  ? "\nid author ": " by ")
     << "T. Romstad, M. Costalba, J. Kiiski, G. Linscott";

  return ss.str();
}
father of line 124 in misc.cpp is
line 27 of misc.cpp

Code: Select all

#include <string>

Re: Is there a tool to ask the computer how it can understand code?

Posted: Thu Nov 08, 2018 12:46 pm
by mar
What you want is called "goto definition" in IDEs/Editors which support this feature.

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 5:56 am
by tpoppins
e.g., in Code::Blocks right-clicking on a function's/variable's name offers the following options:
  • Find declaration of
  • Find implementation of
  • Find occurrences of
  • Find functions called by
  • Find functions calling
  • Find references of

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 8:07 am
by Uri Blass
thanks
another question.

Why pick definition of std gives me different result in stockfish and in a different code?

When I click pick definition of std in the first line of main.cpp in stockfish it show me the fstream code but when I pick the same in a different file that has part of stockfish it shows me the ostream code or the iostream code(I have 2 different projects that have a very small part of stockfish).

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 12:33 pm
by mar
Uri Blass wrote:
Fri Nov 09, 2018 8:07 am
Why pick definition of std gives me different result in stockfish and in a different code?

When I click pick definition of std in the first line of main.cpp in stockfish it show me the fstream code but when I pick the same in a different file that has part of stockfish it shows me the ostream code or the iostream code(I have 2 different projects that have a very small part of stockfish).
Probably because std is a namespace so it picks the next symbol? How would you jump to namespace anyway? Where does it "begin"? There would be many files.
You should learn a bit about C++ before trying to understand C++ code.

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 1:57 pm
by phhnguyen
Uri Blass wrote:
Fri Nov 09, 2018 8:07 am
thanks
another question.

Why pick definition of std gives me different result in stockfish and in a different code?

When I click pick definition of std in the first line of main.cpp in stockfish it show me the fstream code but when I pick the same in a different file that has part of stockfish it shows me the ostream code or the iostream code(I have 2 different projects that have a very small part of stockfish).
I guess because of imperfect IDEs, so complicated and bad design of standard library (yes, it has a bad reputation). A programmer should be disappointed if he moves from Java to work with std one since it lacks information and the hierarchy is so bad (compared with Java).

My way (sure it may be not a good way) to use std lib is that I treat it as a "blackbox" or simply as a set of C functions. It means I learn how to use them without looking deeper into their code. Thus I am quite rare to click on std or its functions for looking their definitions. Whenever I need to know which functions / includes or how to use I will google. I am sure human explanations / examples are much easier and quicker to understand than those code.

BTW, look like you are learning Stockfish / C++ from middle of nowhere. I suggest you to start yourself a short course about C++, write some simple programs, then spend time to read about general programming which is a bit hard for beginners but Stockfish is using heavily. For the first time, learn to use std library as a blackbox since Stockfish uses just only some.

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 8:19 pm
by Dann Corbit
mar wrote:
Fri Nov 09, 2018 12:33 pm
{snip}
You should learn a bit about C++ before trying to understand C++ code.
Don't those two things go hand in hand?

But it probably would be a good idea to read Bjarne Stroustrup's books.

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 10:19 pm
by Sven
phhnguyen wrote:
Fri Nov 09, 2018 1:57 pm
BTW, look like you are learning Stockfish / C++ from middle of nowhere. I suggest you to start yourself a short course about C++, write some simple programs, then spend time to read about general programming which is a bit hard for beginners but Stockfish is using heavily. For the first time, learn to use std library as a blackbox since Stockfish uses just only some.
I can't judge about Uri's skills in C++. But just as a side note I'd like to add that Uri is not a beginner in the area of programming in general (he is the author of Movei which is a 2600+ engine and was written in C a couple of years ago). Apart from that I fully agree to your other points regarding Stockfish and about understanding parts of the standard C++ library.

Re: Is there a tool to ask the computer how it can understand code?

Posted: Fri Nov 09, 2018 11:42 pm
by Uri Blass
I am the author of movei but I got a lot of help from other people with the non mathematical part of the program and I also started by copying some structures from tscp but not working chess code(and my move generator is clearly different than the move generator of other programs)

I do not like to start to learn C++ before trying to understand C++ code because of the following reasons:
1)I am not interested in things that are not needed for understanding the code(spending time on them means that I need to spend more time)
2)I prefer to learn something only when I need it(otherwise it is not an efficient way to use my time because I may read something that I do not need in the next month forget it and later I need to read it again).

Re: Is there a tool to ask the computer how it can understand code?

Posted: Sat Nov 10, 2018 11:01 am
by BeyondCritics
If you want to accomplish something efficiently, the first thing is obviously to get your tools straight. Think of all the time you have definitely lost by now, tinkering around for nothing!
Anyway: Any(!) modern IDE should do what you have asked for, including Visual Studio, Visual Studio Code and Eclipse.
Since hacking stockfish is definitely non commercial, you can get a free license from https://www.sourcetrail.com/, i like their product.
You can also try https://sourcegraph.com and maybe their chrome plugin.