blob: 76771d0d5c92ad3f2053e5da178c74f8a9173812 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/Diagnostic.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000019
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
22Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
23 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000024
25 // Get IdentifierInfo objects for known functions for which we
26 // do extra checking.
27 IdentifierTable& IT = PP.getIdentifierTable();
28
29 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
30 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
31 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
32 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
33 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
34 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
35 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
36 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
37 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
38 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Chris Lattner4b009652007-07-25 00:24:17 +000039}
40
41//===----------------------------------------------------------------------===//
42// Helper functions.
43//===----------------------------------------------------------------------===//
44
45bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
46 PP.getDiagnostics().Report(Loc, DiagID);
47 return true;
48}
49
50bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
51 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
52 return true;
53}
54
55bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
56 const std::string &Msg2) {
57 std::string MsgArr[] = { Msg1, Msg2 };
58 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
59 return true;
60}
61
62bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
63 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
64 return true;
65}
66
67bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
68 SourceRange Range) {
69 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
70 return true;
71}
72
73bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
74 const std::string &Msg2, SourceRange Range) {
75 std::string MsgArr[] = { Msg1, Msg2 };
76 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
77 return true;
78}
79
80bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
81 SourceRange R1, SourceRange R2) {
82 SourceRange RangeArr[] = { R1, R2 };
83 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
84 return true;
85}
86
87bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
88 SourceRange R1, SourceRange R2) {
89 SourceRange RangeArr[] = { R1, R2 };
90 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
91 return true;
92}
93
94bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
95 const std::string &Msg2, SourceRange R1, SourceRange R2) {
96 std::string MsgArr[] = { Msg1, Msg2 };
97 SourceRange RangeArr[] = { R1, R2 };
98 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
99 return true;
100}
101
102const LangOptions &Sema::getLangOptions() const {
103 return PP.getLangOptions();
104}