How to get function name using FunctionDecl *D in clang - clang

In one of my checker, i am using FunctionDecl class to get the function declaration. Now i want to get the name of the function for which i enter into the checkASTDecl method. As we know that in checkASTDecl() we get pointer of class FunctionDecl. So, can any one help me with way to get the name of function for which i entered into checkASTDecl.
Here is the sample code i have written:
namespace {
class FuncPrototypeChecker : public Checker<check::ASTDecl<FunctioeDecl> > {
mutable OwningPtr<BugType> TernaryOperatorBug;
public:
void checkASTDecl(const FunctionDecl *D,
AnalysisManager &mgr, BugReporter &BR) const;
};
}
void FuncPrototypeChecker::checkASTDecl(const FunctionDecl *D,
AnalysisManager &mgr,
BugReporter &BR) const {
/* Get the name of the function from FunctionDecl *D */
}
I want to get the name of the function for which i entered the method FuncPrototypeChecker::checkASTDecl(). Please help me with the way i can achieve it.
Thanks in advance.

In fact, clang has a class to store names of decls (not just a FunctionDecl), called DeclarationNameInfo (see clang api for DeclarationNameInfo)
You can get a DeclarationNameInfo instance for a FunctionDecl using the API call:
functionDecl->getNameInfo();
and the get the name as a string by:
functionDecl->getNameInfo().getAsString();
The result returned will be a std::string.
There is more information about the FunctionDecl API available in clang api for FunctionDecl.

Related

dart import to define <T>

I wish to check any list pass into checkList function. What import I should use for <T>. VS Code no suggest a Quick Fix for this.
The name 'T' isn't a type so it can't be used as a type argument.
Try correcting the name to an existing type, or defining a type named 'T'.
If you want to type safe you should do this
void checkList<T>(List<T> list){
// ...
}
If you want to be able to check any list with any contents, you should use:
void checkListObject(List<Object> list) {
// ...
}
If you want to enforce that the list has a concrete type, but you don't know what that is ahead of time, you can pass a type parameter to the function:
void checkListT<T>(List<T> list) {
// ...
}
This has the following behaviour:
checkListObject(["hello", 123]); // allowed
checkListObject([123, 234]); // allowed
checkListT(["hello", 123]); // allowed, T is Object
checkListT<String>(["hello", "world"]); // allowed, T is String
checkListT<String>([123, 234]); // not allowed, T is String but given List<int>
checkListT<String>([123, "hello"]); // not allowed, T is String but given List<Object>
You can use Object instead of T.
void checkList(List<Object> list){
}

JNA - Access Method in DLL

Im new to JNA. Im trying to access a method inside a DLL. I get the following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetACSStatus': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at com.sun.proxy.$Proxy0.GetACSStatus(Unknown Source)
at TestJNA.main(TestJNA.java:17)
Here is the code:
public class TestJNA {
public interface simpleDLLTest extends Library {
simpleDLLTest INSTANCE = (simpleDLLTest) Native.loadLibrary("IMV1", simpleDLLTest.class);
public NativeLong GetACSStatus();
}
public static void main(String[] args) {
simpleDLLTest sdll = simpleDLLTest.INSTANCE;
NativeLong result1 = sdll.GetACSStatus(); // calling function
System.out.println("GetACSStatus(): " + result1);
}
}
Please help.
You need to compile your code with extern "C" so that the symbols are exported without C++ name mangling.
Alternatively you can use the name from the symbol table as the function lookup name (you would need to use a FunctionMapperto get the special symbols).

Define function signature for a class member in Dart

I would like to specify the signature of a function used as a class field.
Here is an example:
class Space<PointType>
{
// num distance(PointType, PointType); This does not work
final distance; // This works but dystance types are not defined
Space(num this.distance(PointType, PointType));
}
I know I can use typedef to define a callback interface. However this does not seem to work within generics.
Any suggestions?
You can use generics with typedef. In your case :
typedef num ComputeDistance<E>(E p1, E p2);
class Space<PointType> {
final ComputeDistance<PointType> distance;
Space(this.distance);
}
You can use a typedef to declare the signature of a function used in a class field. I am not entirely sure I follow your specific example, so I'll keep the discussion generic.
Here is the syntax for using a typedef:
typedef functionReturnType nameOfTypedef(ParamType paramName);
Here is a concrete example:
typedef String MyFuncType(int x, int y);
This example defines MyFuncType to return a String and take two int arguments.
class MyClass {
MyFuncType func; // Matches a func that returns a String and take 2 int arguments.
...
}
You can read a fuller discussion about using typedefs at https://github.com/dart-lang/cookbook/blob/basics/basics.asciidoc#using-typedef-to-declare-a-function-signature.

How to pass objective-c function as a callback to C function?

I want to call a c function from objective-c and pass objective-c function as a callback
the problem is this function has a callback as parameter, so I have to pass objective-c function as a call back to c function
here is the header of the c function
struct mg_context *mg_start(const struct mg_callbacks *callbacks,
void *user_data,
const char **configuration_options);
here is where I try to call it
- (void)serverstarted
{
NSLog(#"server started");
}
- (IBAction)startserver:(id)sender {
NSLog(#"server should start");
const char *options[] =
{
"document_root", "www",
"listening_ports", "8080",
NULL
};
mg_start(serverstarted(), NULL, options);
}
I have tried several ways to do it and searched the web to just get a clue how to do it but with not luck
here is the library I am incuding in my code
https://github.com/valenok/mongoose
Your chief problem is the first parameter to mg_start(), which is described in the declaration as const struct mg_callbacks *callbacks. You are trying pass a pointer to a function. (Actually you are trying to pass the result of a call to that function, which is even further from the mark.) That isn't what it says: it says a pointer to a struct (in particular, an mg_callbacks struct).
The example code at https://github.com/valenok/mongoose/blob/master/examples/hello.c shows you how to configure this struct. You have to create the struct and put the pointer to the callback function inside it. Then you pass the address of that struct.
Other problems with your code: your callback function itself is all wrong:
- (void)serverstarted
{
NSLog(#"server started");
}
What's wanted here is a C function declared like this: int begin_request_handler(struct mg_connection *conn), that is, it takes as parameter a pointer to an mg_connection struct. Your serverstarted not only doesn't take that parameter, it isn't even a C function! It's an Objective-C method, a totally different animal. Your use of the term "Objective-C function" in your title and your question is misleading; C has functions, Objective-C has methods. No Objective-C is going to be used in the code you'll be writing here.
What I suggest you do here is to copy the hello.c example slavishly at first. Then modify the content / names of things slowly and bit by bit to evolve it to your own code. Of course learning C would also help, but you can probably get by just by copying carefully.
As matt already said, you cannot pass an Objective-C method as callback where a C function
is expected. Objective-C methods are special functions, in particular the receiver ("self")
is implicitly passed as first argument to the function.
Therefore, to use an Objective-C method as request handler, you need an (intermediate) C function as handler and you have to pass self to that function, using the user_data argument. The C function can then call the Objective-C method:
// This is the Objective-C request handler method:
- (int)beginRequest:(struct mg_connection *)conn
{
// Your request handler ...
return 1;
}
// This is the intermediate C function:
static int begin_request_handler(struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
// Cast the "user_data" back to an instance pointer of your class:
YourClass *mySelf = (__bridge YourClass *)request_info->user_data;
// Call instance method:
return [mySelf beginRequest:conn];
}
- (IBAction)startserver:(id)sender
{
struct mg_callbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
const char *options[] =
{
"document_root", "www",
"listening_ports", "8080",
NULL
};
// Pass "self" as "user_data" argument:
mg_start(&callbacks, (__bridge void *)self, options);
}
Remarks:
If you don't use ARC (automatic reference counting) then you can omit the (__bridge ...)
casts.
You must ensure that the instance of your class ("self")
is not deallocated while the server is running. Otherwise the YourClass *mySelf
would be invalid when the request handler is called.

Binding a static global causes error in MonoTouch

I started with a functioning bindings project, but I needed to add a global int for a status flag and I can't get it to bind without error. I started with the sample code and can't get this to work.
The code I add to my bindings file is:
[Static]
interface CameraEffects {
[Field ("kCameraEffectsZoomFactorKey", "CameraLibrary")]
NSString ZoomFactorKey { get; }
}
I get three errors:
obj/Debug/ios/PDFExpert/CameraEffects.g.cs(34,94): error CS0117: `MonoTouch.Constants' does not contain a definition for `CameraLibraryLibrary'
obj/Debug/ios/PDFExpert/CameraEffects.g.cs(34,76): error CS1502: The best overloaded method match for `MonoTouch.ObjCRuntime.Dlfcn.dlopen(string, int)' has some invalid arguments
obj/Debug/ios/PDFExpert/CameraEffects.g.cs(34,76): error CS1503: Argument `#1' cannot convert `object' expression to type `string'
If I leave the library off it tried to assign it to another unknown constant. This seems really screwed up as it is strait from the documentation.
I guess this should be bound like this
[Static]
interface CameraEffects {
[Field ("kCameraEffectsZoomFactorKey", "__Internal")]
NSString ZoomFactorKey { get; }
}
This is due to on the final app, the executable and the libxxx.a will be linked and merged together so it should work.
Alex
Another option that allows both assignment and retrieval of the value is to use the internal marshalling that MonoTouch uses. I got this from a Xamarin support person, notice that this is for manipulating an int, but should be a pattern you can use if you get the right marshalling code.
public unsafe static partial class RDPDFGlobal
{
static readonly IntPtr __Internal_libraryHandle = Dlfcn.dlopen (null, 0);
public static int RDPDFFeatures {
get {
return Dlfcn.GetInt32 (__Internal_libraryHandle, "RDPDFKitEnabledFeatures");
}
set {
var indirect = Dlfcn.dlsym (__Internal_libraryHandle, "RDPDFKitEnabledFeatures");
if (indirect == IntPtr.Zero)
throw new Exception ("Field 'RDPDFKitEnabledFeatures' not found.");
Marshal.WriteInt32 (indirect, value);
}
}

Resources