0

So far I have found that using grep I can find the header names in a C program.

Regular expression to extract header name from c file

But in the above case the accepted answer uses grep command with some flags. But I want to use this regex in regex_search() of C++ programmatically. But I am unable to convert the regex in the above answer to fit in C++. I want to know how can I convert the above regex to be used in C++.

regex expr(".*#include.*(<|\")\\K.*(?=>|\")");

I have written above regex but it doesn't give the expected output. Basically I read the C file as string and use C++ regex search to extract header names data. Below code explains what I am trying to do...


#include<bits/stdc++.h>

using namespace std;

void searchContent(string content) {
     smatch match;
     regex expr(".*#include.*(<|\")\\K.*(?=>|\")");

     while (regex_search(content, match, expr)) {
          for (auto x : match)
               cout << x << " ";

           cout << std::endl;
           content = match.suffix().str();
     }
}


int main(int argc, char** argv)
{

  std::ifstream ifs("/home/ultraproton/PlacementPrep/C++/regex/testfile.c");
  std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );


  searchContent(content);

  return 0;
}

Aditya kumar
  • 76
  • 1
  • 11
  • How about `/* #include */` note `/*` can be couple lines before so don't try to fix it by regex. – Slava May 30 '20 at 19:22
  • I recommend using a *lexer* or a parser. A regular expression for what you want may become very complicated to handle all cases. – Thomas Matthews May 30 '20 at 19:27
  • @Slava With grep the accepted answer in the above link will work even if the header inclusion is commented. – Aditya kumar May 30 '20 at 19:28
  • @ThomasMatthews I am basically working in clang 8 to write a checker for header files for which I need header names but didn't find any class in it to extract header related data. If you can help it will be really nice. – Aditya kumar May 30 '20 at 19:29
  • "With grep the accepted answer in the above link will work" but the problem is it does not work, as commented out header should not be included. Another example `const char * foo = "#include ";` there is no inclusion but your regexp would match it. – Slava May 30 '20 at 20:57
  • " I am basically working in clang 8 to write a checker for header files" why not to use clang with -MD and -MF flags? It will tell you which headers it uses for compilation. – Slava May 30 '20 at 21:01
  • @Slava it will work for me but I just want the names of the header files included in the current file like . It gives me whole information in a file format like this.... /usr/include/stdio.h .. /usr/include/x86_64-linux-gnu/bits/libc-header-start.h ... /usr/include/features.h .... /usr/include/stdc-predef.h .... /usr/include/x86_64-linux-gnu/sys/cdefs.h ..... /usr/include/x86_64-linux-gnu/bits/wordsize.h ..... /usr/include/x86_64-linux-gnu/bits/lo – Aditya kumar May 31 '20 at 07:50

0 Answers0