From patchwork Fri Sep 23 12:49:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 76857 Delivered-To: patch@linaro.org Received: by 10.140.106.72 with SMTP id d66csp553103qgf; Fri, 23 Sep 2016 05:52:50 -0700 (PDT) X-Received: by 10.66.132.100 with SMTP id ot4mr12391460pab.84.1474635170660; Fri, 23 Sep 2016 05:52:50 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b24si7870795pfj.11.2016.09.23.05.52.50; Fri, 23 Sep 2016 05:52:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966112AbcIWMwr (ORCPT + 27 others); Fri, 23 Sep 2016 08:52:47 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:18957 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759389AbcIWMu5 (ORCPT ); Fri, 23 Sep 2016 08:50:57 -0400 Received: from 172.24.1.136 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.136]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DRL70269; Fri, 23 Sep 2016 20:50:22 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml431-hub.china.huawei.com (10.82.67.208) with Microsoft SMTP Server id 14.3.235.1; Fri, 23 Sep 2016 20:50:11 +0800 From: Wang Nan To: , CC: , , , Wang Nan , Arnaldo Carvalho de Melo , He Kuang , Jiri Olsa Subject: [PATCH 07/14] perf clang: Use real file system for #include Date: Fri, 23 Sep 2016 12:49:54 +0000 Message-ID: <1474635001-153850-8-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1474635001-153850-1-git-send-email-wangnan0@huawei.com> References: <1474635001-153850-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0203.57E5250E.009A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: dcad854cf2ba8971e40ff3b809fb525e Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Utilize clang's OverlayFileSystem facility to enable VFS passed to CompilerInstance can access files in real file system, so '#include' directive can be used. Add a new getModuleFromSource for real file. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: He Kuang Cc: Jiri Olsa --- tools/perf/util/c++/clang.cpp | 43 +++++++++++++++++++++++++++++++------------ tools/perf/util/c++/clang.h | 3 +++ 2 files changed, 34 insertions(+), 12 deletions(-) -- 1.8.3.4 diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index d0cba4c..f032ea8 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp @@ -26,14 +26,6 @@ static std::unique_ptr LLVMCtx; using namespace clang; -static vfs::InMemoryFileSystem * -buildVFS(StringRef& Name, StringRef& Content) -{ - vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true); - VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); - return VFS; -} - static CompilerInvocation * createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) { @@ -59,17 +51,17 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) return CI; } -std::unique_ptr -getModuleFromSource(StringRef Name, StringRef Content) +static std::unique_ptr +getModuleFromSource(StringRef Path, + IntrusiveRefCntPtr VFS) { CompilerInstance Clang; Clang.createDiagnostics(); - IntrusiveRefCntPtr VFS = buildVFS(Name, Content); Clang.setVirtualFileSystem(&*VFS); IntrusiveRefCntPtr CI = - createCompilerInvocation(Name, Clang.getDiagnostics()); + createCompilerInvocation(Path, Clang.getDiagnostics()); Clang.setInvocation(&*CI); std::unique_ptr Act(new EmitLLVMOnlyAction(&*LLVMCtx)); @@ -79,6 +71,33 @@ getModuleFromSource(StringRef Name, StringRef Content) return Act->takeModule(); } +std::unique_ptr +getModuleFromSource(StringRef Name, StringRef Content) +{ + using namespace vfs; + + llvm::IntrusiveRefCntPtr OverlayFS( + new OverlayFileSystem(getRealFileSystem())); + llvm::IntrusiveRefCntPtr MemFS( + new InMemoryFileSystem(true)); + + /* + * pushOverlay helps setting working dir for MemFS. Must call + * before addFile. + */ + OverlayFS->pushOverlay(MemFS); + MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); + + return getModuleFromSource(Name, OverlayFS); +} + +std::unique_ptr +getModuleFromSource(StringRef Path) +{ + IntrusiveRefCntPtr VFS(vfs::getRealFileSystem()); + return getModuleFromSource(Path, VFS); +} + } extern "C" { diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h index f64483b..90aff01 100644 --- a/tools/perf/util/c++/clang.h +++ b/tools/perf/util/c++/clang.h @@ -12,5 +12,8 @@ using namespace llvm; std::unique_ptr getModuleFromSource(StringRef Name, StringRef Content); +std::unique_ptr +getModuleFromSource(StringRef Path); + } #endif