In Clang, can I access the SourceManager when writing a custom ASTMatcher? - c++

I'm trying to upgrade my AutoFFI project by making it more elegant and use Clang's ASTMatchers more extensively. I'd like to create a matcher that filters on the file path that was specified. Is it possible to do such a thing, or do I need to add custom logic outside of the matcher for this to work? As far as I can see, there's no way to get the SourceManager and use it to create a FullSourceLoc, but maybe I'm missing something.
Some relevant links:
https://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html
https://github.com/llvm-mirror/clang/blob/f3b7928366f63b51ffc97e74f8afcff497c57e8d/include/clang/ASTMatchers/ASTMatchersMacros.h#L28
If someone could tell me whether this is a limitation to Clang's ASTMatcher API or not I'd be very grateful!

Never mind, I've found the answer by looking at the source of isExpansionInMainFile:
AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
auto &SourceManager = Finder->getASTContext().getSourceManager();
return SourceManager.isInMainFile(
SourceManager.getExpansionLoc(Node.getBeginLoc()));
}
Turns out I missed the getASTContext in MatchFinder, which holds on to the source manager.

Related

C++ generate new EC_KEY using OpenSSL 1.1.1n

I'm very new to this OpenSSL stuff and trying to learn my way through it. I want to generate an ec key but its keep failing to generate. I'm currently using OpenSSL 1.1.1n and here's a snippet based on understaning of EC_Key through documentation and from other people's example online:
EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp521r1);
if (!key)
{
ERROR
}
EC_GROUP* ecgroup = EC_GROUP_new_by_curve_name(NID_secp521r1);
if (ecgroup)
{
printf("ECGROUP IS NOT NULL!\n");
if (EC_KEY_set_group(key, ecgroup) != 1)
{
ERROR
}
}
if (EC_KEY_generate_key(key) != 1) //<-- fails at this function
{
ERROR
}
Maybe this piece of code is from an older version of OpenSSL but I don't see that function being mentioned in a change log. Any help would be really appreciated! Thanks in advance!
Edit:
Thanks for Jakob's answer I was able to get some more information about the failure by using ERR_get_error() function. The error I see now is
error:2406C06E:random number generator:RAND_DRBG_instantiate:error retrieving entropy
The manual states:
EC_KEY_generate_key() generates a new public and private key for the
supplied eckey object. eckey must have an EC_GROUP object associated
with it before calling this function. [...]
Did you associate an EC_GROUP with the key before calling the function? There is a function called EC_KEY_set_group() which can be used to add a group object. Such an object can be created with the EC_GROUP_new().
By the way is there a reason, why you are still using OpenSSL 1.1.1. I would recommend using the latest version of the library OpenSSL 3.0. Even if you only want to learn stuff, it is probably more useful, if you immediately learn the newer version.
Edit
I do not know, what is behind your ERROR macro, but it might be a good idea to learn the OpenSSL error handling system. There are functions like ERR_print_errors() that maybe could have given you a hint about what was going wrong.
Answering myself in case someone is having this same issue.
I was able to get around this problem by building OpenSSl with no-shared flag. I'm not sure how that is affecting the lib in getting an entropy but that's the parameter that made the EC_KEY_generate_key() work.
Here's my working configuration command:
perl Configure no-shared VC-WIN32

Alternative of CreateVideoSource() in webrtc

Hi all I had seen one webrtc old source code which has this method called CreateVideoSource() for adding streams after an CreateAudioTrack() call.
rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source =
peer_connection_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
media_source->GetVideoCapturer()),
NULL);
What's happening is whenever I try to build it gives an error for the above CreateVideoSource() that it is undefined. And the reason behind that is the latest webrtc-checkout has deprecated this.
So my question is, I wanted to know the alternative which they have introduced after deprecating this method. So can anyone tell me what the alternate approach is.
Okay, Finally I was able to come up with an alternative(i.e the current implementation of libwebrtc) after looking into bundle examples and tests.
Line 71 is a good point to start

Implementation of active appearance models

I'm having an internship in the field of computer vision, and i am really interested to know some details about the implementation of the Active Appearence Models aam-opencv that exists in the Google Code site.
In fact, i downloaded aam-opencv.tar.gz then built it with cmake and i solved some syntax problems but the only error that i am still having when i try to generate the solution is the following :
This function should return something:
aamImage* delaunay:: warpImageToMeanShape(aamImage*input)
{
}
I wonder if there is something missing in that function, or is it a compiler problem.
Please give me an answer or just guide me to complete the missing part of that function.
I would really appreciate if anyone kindly help me.
Thank you.
I suppose that is not used in code function, so it is not important what it return. Some C++ compilers allow to write such code and give only warning, another treat as errors:
ReturnType f()
{
}
looks like you use not the same compiler as author of source code. So just add something like:
aamImage* delaunay:: warpImageToMeanShape(aamImage*input)
{
return NULL;
}

Cocoa: get path to Contents/Resources folder in cocoa bundle

I am aware of how to get a specific file from the Resources folder in cocoa, i.e. :
NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImage = [myBundle pathForResource:#"Seagull" ofType:#"jpg"];
anyways, I'd like to have a simple function that gives me the path to the Resources folder so I can use it in c++ like this.
String getResourcePath()
{
return the correct path here
}
std::ofstream theFile;
theFile.open(getResourcePath()+"test.txt");
I guess I could manually combine the main bundle name with Contents/Resources, anyways I'd like to know if there is a more robust solution!
Thanks!
If you ever wonder if an Apple-provided class has a certain method, do look up Apple's own documentation before asking the question here at SO.
In this case, the answer is this method. But let me reiterate:
If you ever wonder if an Apple-provided class has a certain method, do look up Apple's own documentation before asking the question here at SO.
And, when you do so, go over all the methods in the documentation. That way, you'll learn what kind of methods are available.

What has to be Glib::init()'ed in order to use Glib::wrap?

So I'm trying to make use of a GtkSourceView in C++ using GtkSourceViewmm, whose documentation and level of support give me the impression that it hasn't been very carefully looked at in a long time. But I'm always an optimist :)
I'm trying to add a SourceView using some code similar to the following:
Glib::RefPtr<gtksourceview::SourceLanguageManager> source_language_manager = gtksourceview::SourceLanguageManager::create();
Glib::RefPtr<gtksourceview::SourceLanguage> source_language = Glib::wrap(gtk_source_language_manager_guess_language(source_language_manager->gobj(), file, NULL));
Glib::RefPtr<gtksourceview::SourceBuffer> source_buffer = gtksourceview::SourceBuffer::create(source_language);
gtksourceview::SourceView* = m_source_view = new gtksourceview::SourceView(source_buffer);
m_vbox.pack_start(*m_source_view);
Unfortunately, it spits out the warning
(algoviz:4992): glibmm-WARNING **:
Failed to wrap object of type
'GtkSourceLanguage'. Hint: this error
is commonly caused by failing to call
a library init() function.
and when I look at it in a debugger, indeed the second line above (the one with the Glib::wrap()) is returning NULL. I have no idea why this is, but I tried to heed the warning by adding Glib::init() to the begining of the program, but that didn't seem to help at all either.
I've tried Google'ing around, but have been unsuccessful. Does anyone know what Glib wants me to init in order to be able to make that wrap call? Or, even better, does anyone know of any working sample code that uses GtkSourceViewmm (not just regular GtkSourceView)? I haven't been able to find any actual sample code, not even on Google Code Search.
Thanks!
It turns out, perhaps not surprisingly, that what I needed to init was:
gtksourceview::init();
After this, I ran into another problem with one of the parameter to gtksourceview::SourceLanguageManager, but this was caused by a genuine bug which I subsequently reported and was promptly fixed. So everything's working great now!
I use gtkmm. Typically you have to initialize things with something like :
_GTKMain = new Gtk::Main(0, 0, false);
Of course do not forget :
delete _GTKMain;
Check here for details :
http://library.gnome.org/devel/gtkmm/2.19/classGtk_1_1Main.html
(Sorry but the link option does not work ...)