1 //===- Parser.cpp - Top-Level TableGen Parser implementation --------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/TableGen/Parser.h"
11 #include "llvm/Support/MemoryBuffer.h"
12 #include "llvm/TableGen/Record.h"
16 bool llvm::TableGenParseFile(SourceMgr
&InputSrcMgr
, RecordKeeper
&Records
) {
17 // Initialize the global TableGen source manager by temporarily taking control
18 // of the input buffer in `SrcMgr`. This is kind of a hack, but allows for
19 // preserving TableGen's current awkward diagnostic behavior. If we can remove
20 // this reliance, we could drop all of this.
22 SrcMgr
.takeSourceBuffersFrom(InputSrcMgr
);
23 SrcMgr
.setIncludeDirs(InputSrcMgr
.getIncludeDirs());
24 SrcMgr
.setDiagHandler(InputSrcMgr
.getDiagHandler(),
25 InputSrcMgr
.getDiagContext());
27 // Setup the record keeper and try to parse the file.
28 auto *MainFileBuffer
= SrcMgr
.getMemoryBuffer(SrcMgr
.getMainFileID());
29 Records
.saveInputFilename(MainFileBuffer
->getBufferIdentifier().str());
31 TGParser
Parser(SrcMgr
, /*Macros=*/std::nullopt
, Records
,
32 /*NoWarnOnUnusedTemplateArgs=*/false,
33 /*TrackReferenceLocs=*/true);
34 bool ParseResult
= Parser
.ParseFile();
36 // After parsing, reclaim the source manager buffers from TableGen's global
38 InputSrcMgr
.takeSourceBuffersFrom(SrcMgr
);