1//===- Parser.cpp - Top-Level TableGen Parser implementation --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/TableGen/Parser.h"
10#include "TGParser.h"
11#include "llvm/Support/MemoryBuffer.h"
12#include "llvm/Support/VirtualFileSystem.h"
13#include "llvm/TableGen/Record.h"
14
15using namespace llvm;
16
17bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
18 // Initialize the global TableGen source manager by temporarily taking control
19 // of the input buffer in `SrcMgr`. This is kind of a hack, but allows for
20 // preserving TableGen's current awkward diagnostic behavior. If we can remove
21 // this reliance, we could drop all of this.
22 SrcMgr = SourceMgr();
23 SrcMgr.takeSourceBuffersFrom(SrcMgr&: InputSrcMgr);
24 SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
25 SrcMgr.setVirtualFileSystem(InputSrcMgr.getVirtualFileSystem());
26 SrcMgr.setDiagHandler(DH: InputSrcMgr.getDiagHandler(),
27 Ctx: InputSrcMgr.getDiagContext());
28
29 // Setup the record keeper and try to parse the file.
30 auto *MainFileBuffer = SrcMgr.getMemoryBuffer(i: SrcMgr.getMainFileID());
31 Records.saveInputFilename(Filename: MainFileBuffer->getBufferIdentifier().str());
32
33 TGParser Parser(SrcMgr, /*Macros=*/{}, Records,
34 /*NoWarnOnUnusedTemplateArgs=*/false,
35 /*TrackReferenceLocs=*/true);
36 bool ParseResult = Parser.ParseFile();
37
38 // After parsing, reclaim the source manager buffers from TableGen's global
39 // manager.
40 InputSrcMgr.takeSourceBuffersFrom(SrcMgr);
41 SrcMgr = SourceMgr();
42 return ParseResult;
43}
44