blob: 1a012706927bdb839cfc9f32d988b2ec40b74e0f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner59907c42007-08-10 20:18:51 +000019
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
23 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner59907c42007-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");
Chris Lattner59907c42007-08-10 20:18:51 +000033 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremeneke0eb80a2007-08-10 21:13:51 +000034 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner59907c42007-08-10 20:18:51 +000035 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");
Reid Spencer5f016e22007-07-11 17:01:13 +000039}
40
Chris Lattner394a3fd2007-08-31 04:53:24 +000041void Sema::DeleteExpr(ExprTy *E) {
42 delete static_cast<Expr*>(E);
43}
44void Sema::DeleteStmt(StmtTy *S) {
45 delete static_cast<Stmt*>(S);
46}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048//===----------------------------------------------------------------------===//
49// Helper functions.
50//===----------------------------------------------------------------------===//
51
52bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
53 PP.getDiagnostics().Report(Loc, DiagID);
54 return true;
55}
56
57bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
58 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
59 return true;
60}
61
62bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
63 const std::string &Msg2) {
64 std::string MsgArr[] = { Msg1, Msg2 };
65 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
66 return true;
67}
68
69bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
70 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
71 return true;
72}
73
74bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
75 SourceRange Range) {
76 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
77 return true;
78}
79
80bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
81 const std::string &Msg2, SourceRange Range) {
82 std::string MsgArr[] = { Msg1, Msg2 };
83 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
84 return true;
85}
86
87bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
88 SourceRange R1, SourceRange R2) {
89 SourceRange RangeArr[] = { R1, R2 };
90 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
91 return true;
92}
93
94bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
95 SourceRange R1, SourceRange R2) {
96 SourceRange RangeArr[] = { R1, R2 };
97 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
98 return true;
99}
100
101bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
102 const std::string &Msg2, SourceRange R1, SourceRange R2) {
103 std::string MsgArr[] = { Msg1, Msg2 };
104 SourceRange RangeArr[] = { R1, R2 };
105 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
106 return true;
107}
108
109const LangOptions &Sema::getLangOptions() const {
110 return PP.getLangOptions();
111}