使用 C/C++,我们可以得到 argv[0]:
printf("%s\n",argv[0])
Run Code Online (Sandbox Code Playgroud)
在 C# 中,args 以 argv[1] 开头。
非波纹管 API 提供了 argv[0],至少在 Linux 下:
AppDomain.CurrentDomain.FriendlyName: only gives the name, no path
Process.GetCurrentProcess().ProcessName: gives wrong result with symbol link, and no path
Process.GetCurrentProcess().MainModule.FileName: gives wrong result with symbol link, and the path is always absolute
Run Code Online (Sandbox Code Playgroud)
仅供参考:在具有上述 C/C++ 代码的 Linux 下(其结果在这里被视为黄金标准),它打印用于调用程序的确切路径(绝对或相对),如果您通过任何符号调用程序链接时,会打印符号链接的名称而不是实际程序。
我问这个问题是因为我尝试在 Ubuntu 下使用 C# 编写一个包装程序,它应该通过 argv[0] 以完全透明,以防包装程序的行为取决于 argv[0]。
使用 Clang LibTooling 的最小源,这是一种非常常见的方式:
#include "pch.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "clang/Driver/Options.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include <iostream>
using namespace std;
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;
class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
public:
explicit ExampleVisitor(CompilerInstance *CI) {}
};
class ExampleASTConsumer : public ASTConsumer {
private:
CompilerInstance *CI;
public:
explicit ExampleASTConsumer(CompilerInstance *CI) : CI(CI) …Run Code Online (Sandbox Code Playgroud) TL; 博士;
如何从 callExpr -> arg_0 -> DeclRefExpr 获取用于常量大小数组声明大小的宏名称。
详细问题说明:
最近我开始着手一项挑战,它需要源到源转换工具来修改带有附加参数的特定函数调用。Reasearching about how I can acheive 向我介绍了这个惊人的工具集 Clang。我一直在学习如何使用 libtooling 中提供的不同工具来实现我的目标。但现在我遇到了一个问题,在这里寻求你的帮助。
考虑下面的程序(我的源代码),我的目标是使用安全版本的 strcpy_s 重写对 strcpy 函数的所有调用,并在新函数调用中添加一个附加参数,即目标指针最大大小。所以,对于下面的程序,我重构的调用就像 strcpy_s(inStr, STR_MAX, argv[1]);
我编写了一个 RecursiveVisitor 类并检查 VisitCallExpr 方法中的所有函数调用,以获取 dest arg 的最大大小,我正在获取第一个 agrument 的 VarDecl 并尝试获取大小(ConstArrayType)。由于源文件已经过预处理,我将 2049 视为大小,但在这种情况下我需要的是宏 STR_MAX。我怎么能得到呢?(使用此信息创建替换,然后使用 RefactoringTool 替换它们)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STR_MAX 2049
int main(int argc, char **argv){
char inStr[STR_MAX];
if(argc>1){
//Clang tool required to transaform the below call into strncpy_s(inStr, STR_MAX, argv[1], strlen(argv[1]));
strcpy(inStr, argv[1]);
} else { …Run Code Online (Sandbox Code Playgroud) 使用C#LINQ,我们能否仅对一系列List元素进行OrderBy并将其他元素保留在原位?
例如,输入列表为{“ a”,“ b”,“ c”,“ d”,“ e”},想象的OrderByDescending类似于:
OrderByDescending(delegate d,int start_index,int end_index)
l=l.OrderByDescending(x=>x,1,3).ToList();
Run Code Online (Sandbox Code Playgroud)
结果为:{“ a”,“ d”,“ c”,“ b”,“ e”}
如果没有此功能,我需要拆分/ LINQ Orderby /重新加入,这会失去LINQ的精神。
AOSP10 中的一些代码似乎违反了 ODR:
来源 1:
struct ExtentsParam
{
void init (const OT::cff1::accelerator_t *_cff)
{
path_open = false;
cff = _cff;
bounds.init ();
}
void start_path () { path_open = true; }
void end_path () { path_open = false; }
bool is_path_open () const { return path_open; }
bool path_open;
Bounds bounds;
const OT::cff1::accelerator_t *cff;
};
Run Code Online (Sandbox Code Playgroud)
来自:https : //android.googlesource.com/platform/external/harfbuzz_ng/+/refs/heads/android10-gsi/src/hb-ot-cff1-table.cc
来源2:
struct ExtentsParam
{
void init ()
{
path_open = false;
min_x.set_int (0x7FFFFFFF);
min_y.set_int (0x7FFFFFFF);
max_x.set_int (-0x80000000);
max_y.set_int (-0x80000000);
}
void …Run Code Online (Sandbox Code Playgroud)